Ask Your Question

thearduinoguy's profile - activity

2016-11-20 19:57:37 -0600 received badge  Taxonomist
2014-07-20 01:57:08 -0600 asked a question Strange behavior with reading key input

I have a following code and a function key which checks the keyboard input and adjust the value of a variable process_flag so that different processes can be done. However, pressing 't' on the keyboard does not yield any result. The key press is detected. However, the value of process_flag remains unaltered. Any suggestions??

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>


using namespace cv;
using namespace std;

VideoCapture xps(0);

int low = 0; //for thresholding
int centroid[2] = { 0, 0 };//centroid of eye
int calib_x[4]; // x coordinate of eye centroid when caliberating
int calib_y[4];//y coordinates of eye centroid when caliberating
int largest_contour_index = 0;// store the index of the largest contour 
int drag = 0;// mouse roi
int circle_x = 0;// for blobs
int circle_y = 0;
int mouse_x = 0; // store mouse coordinates
int mouse_y = 0;
int process_flag = 0; //for checking ketboard inputs
int x = 0;// for a process control

Mat cap_img;//original image
Mat thr; // thresholded image
Mat dst; // image with largest contour
Mat roi;// final cropped image
Mat blank(750, 1000, CV_8UC3, Scalar(255, 255, 255));//for blobs



Rect roi_rect;
Rect bounding_rect;//bounding rectnagle over eye
Point point(0, 0); //for mouse tracking when establlishing roi





void show_blobs(); //function for blobs
void find_centroid(); //to find centroid
void show(); //to show images
void set_roi(); //to select the roi
void key();   





void key()
{
    char keys = waitKey(1);

    if (keys == 'r')
    {
        process_flag = 1;
    }



    else if (keys == 'y')
    {
        process_flag = 3;
    }

    else if (keys == 'u')
    {
        process_flag = 0;
    }

    else if (keys == 'i')
    {
        process_flag = 4;
    }

    else if (keys == 't')
    {
        process_flag == 2;
    }

    cout << process_flag << endl;

}

void set_roi(int event, int x, int y, int flags, void* param)

{


    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        point = Point(x, y);
        drag = 1;
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {

        mouse_x = x;
        mouse_y = y;
        roi_rect.width = mouse_y - point.y;
        roi_rect.height = mouse_x - point.x;

    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        //
        drag = 0;

        //
    }


    if (event == CV_EVENT_RBUTTONUP)
    {
        drag = 0;
    }

}


void show_blobs()
{ 
    namedWindow("blank", 1);


    for (int i = 0; i < 4; i++)
    {
        if (i == 0)
        {
            circle_x = 10;
            circle_y = 10;
        }
        else if (i == 1)
        {
            circle_x = 990;
            circle_y = 10;

        }

        else if (i == 2)
        {
            circle_x = 990;
            circle_y = 740;

        }

        else
        {
            circle_x = 10;
            circle_y = 740;
        }

        circle(blank, Point(circle_x, circle_y), 10, Scalar(0, 0, 255), -1);
        imshow("blank", blank);
        waitKey(3000);
        find_centroid();
        calib_x[i] = centroid[0];
        calib_y[i] = centroid[1];

    }

}





void find_centroid()
{ 

    int centroid_x = 0;
    int centroid_y = 0;
    int largest_area = -1;

    xps.read(cap_img);
    cap_img = cap_img(roi_rect); 

    vector<vector<Point>> contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    thr.create(cap_img.rows, cap_img.cols,CV_8UC3);// holds thresholded image
    dst.create(cap_img.rows, cap_img.cols,CV_8UC3);// contours drawn on this image

    cvtColor(cap_img, thr, COLOR_BGR2GRAY); //Convert to gray

    threshold(thr, thr, low, 255, THRESH_BINARY_INV);

    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image

    for (int i = 0; i< contours.size(); i++) // iterate through each contour.
    {
        double a = contourArea(contours[i], false);  //  Find the area of contour

        if (a>largest_area)
        {

            largest_area = a;
            largest_contour_index = i;                //Store the index of largest contour
            bounding_rect = boundingRect ...
(more)
2014-07-18 04:41:32 -0600 asked a question App crashes when trying to establish Region of Interest

I was trying to create an application in which the mouse can be used to draw a rectangle to set the roi of the image captured from webcam. However, the application crashes when I draw the rectangle. The issue is not with the drawing of the rectangle but the line of code which crops the roi because things work fine when I remove that line. Somebody please help me resolve the issue.

int drag = 0;
int mouse_x = 0;
int mouse_y = 0;
Point point(0,0);
Mat original;
//Mat roi(Scalar(0,0,255));
VideoCapture xps(0);
Mat img;

void set_roi(int event, int x,int y, int flags,void* param)

{
    original = img.clone();

    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        point = Point(x, y);
        drag = 1;
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        //cout << "x " << x << "y " << y << "" << endl;
        mouse_x = x;
        mouse_y = y;
    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        //
        drag = 0;
    }


    if (event == CV_EVENT_RBUTTONUP)
    {
        drag = 0;
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    Mat roi;
    namedWindow("select roi", 1);
    namedWindow("roi", 1);
    setMouseCallback("select roi", set_roi, NULL);

    while (1)
    {

        xps.read(img);
        original = img.clone();
        roi = img.clone();
        rectangle(original, point, Point(mouse_x, mouse_y), Scalar(255, 0, 0), 1, 8, 0);
        roi = roi(Rect(point.x, point.y, mouse_x - point.x, mouse_y - point.y));
        imshow("Read image", img);
        imshow("select roi",original);
        imshow("roi", roi);
        waitKey(1);
    }
    return 0;
}
2014-07-10 05:21:35 -0600 commented question Application crashes with external camera

Okay...it was a problem with drawing contours. The error occurs when attempting to call draw contours even when nothing was found.

2014-07-10 03:08:35 -0600 asked a question Application crashes with external camera

I was using contours for object identification. The code worked well with images and I modified the code to identify objects in real time with camera input. Things work well with my laptop's integrated cam but crashes after a few seconds when using an external camera. The external camera worked fine with a few other applications I developed using opencv. The camera is a 20MP camera. Please look at the code and help me figure out what might be wrong. My processor is good enough to handle images with high resolutions. It seems that the app crashes when I introduce an object in front of the cam which was not there before when the app started up.

include <iostream>

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

using namespace cv; using namespace std; int main() { int largest_area = 0; int largest_contour_index = 0; Rect bounding_rect; int x = 0; int y = 0; VideoCapture xps(0); Mat src; while (1) {

    xps.read(src);


    vector<vector<Point>> contours; // Vector for storing contour
    vector<Vec4i> hierarchy;


    Mat thr(src.rows, src.cols, CV_8UC1);
    Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
    cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
    threshold(thr, thr, 80, 255, THRESH_BINARY_INV);
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 

    for (int i = 0; i< contours.size(); i++) // iterate through each contour.
    {
        double a = contourArea(contours[i], false);  //  Find the area of contour
        if (a>largest_area)
        {
            largest_area = a;
            largest_contour_index = i;                
            bounding_rect = boundingRect(contours[i]); 
        }

    }


    Scalar color(255, 255, 255);
    drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); 
    rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);

    x = bounding_rect.x + bounding_rect.width / 2;
    y = bounding_rect.y + bounding_rect.height / 2;

    circle(src, Point(x, y), 1, Scalar(0, 0, 255));

    imshow("src", src);
    imshow("largest Contour", dst);
    waitKey(2);
}

}

2014-07-06 06:48:21 -0600 asked a question Ellipse as structuring element

Hello, I was trying to learn object tracking by looking at an existing code. I understand what erosion and dilation. From the following piece of code, I understand that an ellipse is used as structuring element:

erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

//morphological closing (removes small holes from the foreground)
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

In this case, we are tracking a circular object. Is it because of that an ellipse is used as structuring element? How do you choose the right structuring element?

2014-07-02 02:17:16 -0600 asked a question 00,01 and 10 in image moments

I am new to Open CV and was trying to find the centroid of an object. I understnad the moments object. But what do these mean

double dM01 = oMoments.m01; double dM10 = oMoments.m10; double dArea = oMoments.m00;

Can someone explain how the x and y coordinates are obtained by doing the following computation: int posX = dM10 / dArea; int posY = dM01 / dArea;