使用openCV在avi视频上绘制框

时间:2012-08-07 00:04:08

标签: c opencv

嗨,我在openCV中还是新手......  我试图读取avi视频,然后在一定数量的帧后显示帧,以允许用户在intrest(ROI)区域周围绘制一个框 因此,用户将给出视频的名称和用于跳过帧的编号 跳过此帧数后,显示下一帧并让用户绘制一个框并保存像素角协调

问题是......我不知道我的代码中的问题在哪里...我不能画一个盒子(我看不到盒子)

任何帮助将不胜感激

这是我的代码......我正在使用openCV 2.4.0

CvRect框; 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;

}

0 个答案:

没有答案