使用openCV在avi视频上绘制盒子

嗨,我仍然是新的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;

}

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

上一篇: draw box on avi video using openCV

下一篇: drawing your own buffered image on frame