App crashes when trying to establish Region of Interest

asked 2014-07-18 04:41:32 -0500

thearduinoguy gravatar image

updated 2014-07-18 04:47:16 -0500

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;
}
edit retag flag offensive close merge delete

Comments

1

if your mouse is left of the previous point, you get negative width for the rect. (same prob for y)

berak gravatar imageberak ( 2014-07-18 05:28:25 -0500 )edit