draw box on avi video using openCV

Hi I am still new in openCV ... I am trying to read avi video then display frames after certain number of frames to allow the user draw a box around the region of intrest (ROI) so the user will give the name of the video and the number that used to skip the frames after skip this number of frames display the next frame and let the user draw a box and save the pixel corner coordinares

the problem is ... I do not know where is the problem in my code ... I cannot draw a box (I cannot see the box)

any help will be appreciate

this is my code ... I am using openCV 2.4.0

CvRect box; bool drawing_box = false;

void draw_box( IplImage* img, CvRect rect ) { cvRectangle( img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height), cvScalar(255, 255,255),1 ); }

void my_mouse_callback( int event, int x, int y, int flags, void* param ) { IplImage* image = (IplImage*) param;

switch( event )
{
    case CV_EVENT_MOUSEMOVE: 
        if( drawing_box )
        {
            box.width = x-box.x;
            box.height = y-box.y;
        }
        break;

    case CV_EVENT_LBUTTONDOWN:
        drawing_box = true;
        box = cvRect( x, y, 0, 0 );
        break;

    case CV_EVENT_LBUTTONUP:
        drawing_box = false;
        if( box.width < 0 )
        {
            box.x += box.width;
            box.width *= -1;
        }
        if( box.height < 0 )
        {
            box.y += box.height;
            box.height *= -1;
        }
        draw_box( image, box );
        break;
}

}

int main (int argc, char * const argv[]) {

int n; 
char name [50]; 
box = cvRect(-1,-1,0,0);

if (argc==1)
{
    printf("nEnter the name of the video:");
    scanf("%s",name); 
    printf("nEnter number of frames:");
    scanf("%d",&n);

} else if (argc == 3)
{
    strcpy(name, argv[1]);
    n = atoi (argv[2]);
}
else 
{
    printf("To run this program you should enter the name of the program at least, or you can enter the name of the program then the file name then the number of frames ");
    return 0; 
}


// GET video
CvCapture* capture = cvCreateFileCapture( name );
if (!capture ) 
{
    printf( "Unable to read input video." );
    return 0;
}


int nframes = cvGetCaptureProperty( capture,CV_CAP_PROP_FRAME_COUNT);

//mouse data 
int ** points= new int*[nframes]; //x1, y1 , x2 , y2  
for (int i=0 ; i< nframes; i++)
    points[i]=new int [4]; 

// Read frame
IplImage* frame = cvQueryFrame( capture );

int c = 1; 
int sum = 1; 
char k ; 
cvNamedWindow("Draw box around the ROI ", CV_WINDOW_AUTOSIZE);

while(c <= nframes) 
{

    if(c == sum)
    {
        cvSetMouseCallback( "Draw box around the ROI ", my_mouse_callback, (void*) frame);


        cvShowImage("Draw box around the ROI ", frame);
        if( drawing_box ) 
        {
            draw_box( frame, box );
            cvShowImage("Draw box around the ROI ", frame);
            cvWaitKey( 15 ); 
        }

        points[c-1][0]= box.x  ;//x1
        points[c-1][1]= box.y ;//y1
        points[c-1][2]= box.x+box.width ;//x2
        points[c-1][3]= box.y+box.height ;//y2

        sum += n; 
    }

    frame = cvQueryFrame( capture );
    c++; 


    k = cvWaitKey(33);
    if( k == 27 ) 
        break;

}

// CLEAN everything
cvReleaseImage( &frame );
cvReleaseCapture( &capture );
cvDestroyWindow( "Draw box around the ROI" );

return 0;

}

链接地址: http://www.djcxy.com/p/35406.html

上一篇: OpenCV和连接摄像头?

下一篇: 使用openCV在avi视频上绘制盒子