Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

select ROI with mouse and display video. Strange problem

I need to display a video file and then select with mouse the region that I need and and the 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 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;
}

select ROI with mouse and display video. Strange problem

I need to display a video file and then select with mouse the region that I need and and the 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 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;
}