我有视频,当程序运行时,视频的第一帧作为图像,允许用户在图像上绘制矩形,绘制矩形后,用户必须右键单击图像进行确认矩形.当鼠标右键单击时,图像消失,视频开始播放,并在其上绘制矩形.
我能够完美地绘制矩形,但我无法将该矩形设置为 ROI.
我想要做的是将该矩形设置为感兴趣区域 (ROI),以便对该 ROI 进行一些图像处理.我无法将我绘制的矩形设置为 ROI.
我正在使用 OpenCV 和 Visual Studio 2010.稍后我将尝试将此程序集成到 QT Creator 中.
任何帮助将不胜感激.
提前致谢.
我的完整代码如下:
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> #include<opencv2opencv.hpp> #include <opencv2highguihighgui.hpp> #include <opencv/highgui.h> #include <opencv/cxcore.h> #include <opencvcvaux.h> using namespace cv; using namespace std; void my_mouse_callback( int event,int x,int y,int flags,void* param ); bool destroy=false; CvRect box; IplImage* image; IplImage* frame2; 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(0,255),2); CvRect rect2=cvRect(box.x,box.y,box.width,box.height); //cvSetImageROI(image,rect2); //here I wanted to set the drawn rect as ROI } // Implement mouse callback void my_mouse_callback( int event,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 ); 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; case CV_EVENT_RBUTTONUP: destroy=true; } } int main() { const char* name = "Box Example"; cvNamedwindow( name ); box = cvRect(0,1,1); CvCapture* capture = cvCreateFileCapture( "C:\video.mp4" ); image = cvQueryFrame( capture ); IplImage* temp = cvCloneImage( image ); // Set up the callback cvSetMouseCallback( name,my_mouse_callback,(void*) image); //IplImage *img2 = cvCreateImage(cvGetSize(temp),temp->depth,temp->nChannels); //cvNot(temp,temp); /* copy subimage */ //cvCopy(temp,temp,NULL); // Main loop while( 1 ) { if(destroy) {cvDestroyWindow(name); break;} cvCopyImage( image,temp ); if( drawing_box ) draw_box( temp,box ); cvMoveWindow(name,200,100); cvShowImage( name,temp ); if( cvWaitKey( 15 )==27 ) break; } //cvReleaseImage( &image ); cvReleaseImage( &temp ); cvDestroyWindow( name ); cvNamedwindow( "Example2",CV_WINDOW_AUTOSIZE ); cvMoveWindow("Example2",150,150); while(1) { frame2 = cvQueryFrame( capture ); draw_box(frame2,box); if( !frame2 ) break; cvShowImage( "Example2",frame2 ); char c = cvWaitKey(33); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "Example2" ); return 0; }