Ask Your Question
0

select ROI with mouse and display video. Strange problem

asked 2012-07-14 00:13:18 -0600

ararr gravatar image

updated 2012-07-14 00:14:35 -0600

I need to display a video file and then select with mouse the region that I need and then in the same or another window to continue displaying the video but only in the specific region that I selected.

I am able to do it with the following code, but only if I have webcam as video input. when I take input a video file it can no do it. I have searched around and I have not been able to detect the problem any ideas why my code does not work with video file and works only with webcam input?

how can I do it for video??

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>

IplImage* frame, * img1;
CvPoint point;
int drag = 0;
CvCapture *capture = 0;
int key = 0;

 void mouseHandler(int event, int x, int y, int flags, void* param)
{
/* user press left button */
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point = cvPoint(x, y);
drag = 1;
}
/* user drag the mouse */
if (event == CV_EVENT_MOUSEMOVE && drag)
{
img1 = cvCloneImage(frame);
cvRectangle(img1,point,cvPoint(x, y),CV_RGB(255, 0, 0),1,8,0);
cvShowImage("result", img1);
}
/* user release left button */
if (event == CV_EVENT_LBUTTONUP && drag)
{
cvSetImageROI(frame,cvRect(point.x,point.y,x-point.x,y-point.y));
cvShowImage("result", frame);
drag = 0;
}

/* user click right button: reset all */
if (event == CV_EVENT_RBUTTONUP)
{
drag = 0;
}
}

int main(int argc, char *argv[])
{
capture = cvCaptureFromCAM( 0 ); /*here when I write cvCaptureFromAVI it does not work */
if ( !capture ) {
printf("Cannot open initialize webcam!\n" );
exit(0);
}

/* create a window for the video */ 
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

while( key != 'q' )
{
int c;
frame = cvQueryFrame( capture );
cvSetMouseCallback("result", mouseHandler, NULL);
c = cvWaitKey(10);
if( (char) c== 'r' ){ cvResetImageROI(frame);}
cvShowImage("result", frame);
}
cvDestroyWindow("result");
cvReleaseImage(&frame);
cvReleaseImage(&img1);
return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-07-14 04:36:06 -0600

AlexanderShishkov gravatar image

updated 2012-07-14 06:17:52 -0600

You should set roi for each frame, also you have problems with 'q' key in your code. Please don't release frame IplImage, you should not do it, when you using capturing.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>

IplImage* frame, * img1;
CvPoint point;
int drag = 0;
CvCapture *capture = 0;
int key = 0;
CvRect rect;

void mouseHandler(int event, int x, int y, int flags, void* param)
{
    /* user press left button */
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        point = cvPoint(x, y);
        drag = 1;
    }
    /* user drag the mouse */
    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        img1 = cvCloneImage(frame);
        cvRectangle(img1,point,cvPoint(x, y),CV_RGB(255, 0, 0),1,8,0);
        cvShowImage("result", img1);
    }
    /* user release left button */
    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        rect = cvRect(point.x,point.y,x-point.x,y-point.y);
        cvSetImageROI(frame,rect);
        cvShowImage("result", frame);
        drag = 0;
    }

    /* user click right button: reset all */
    if (event == CV_EVENT_RBUTTONUP)
    {
        drag = 0;
    }
}

int main(int argc, char *argv[])
{
    capture = cvCaptureFromCAM( 0 ); 
    if ( !capture ) {
        printf("Cannot open initialize webcam!\n" );
        exit(0);
    }

    /* create a window for the video */ 
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

    while( key != 'q' )
    {
        frame = cvQueryFrame( capture );
        if (rect.width>0)
            cvSetImageROI(frame,rect);
        cvSetMouseCallback("result", mouseHandler, NULL);
        key = cvWaitKey(10);
        if( (char) key== 'r' ){ rect = cvRect(0,0,0,0); cvResetImageROI(frame);}
        cvShowImage("result", frame);
    }
    cvDestroyWindow("result");
    cvReleaseImage(&img1);
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-07-14 00:13:18 -0600

Seen: 8,696 times

Last updated: Jul 14 '12