我正在尝试基于鼠标事件在框架上绘制一个矩形,问题是我在播放视频时无法执行此操作,程序崩溃在这里是我的代码:
cv::Rect theBox;
bool drawing_box = false;
void draw_box(Mat* frame, cv::Rect rectangle){
cvRectangle( frame, cvPoint(theBox.x, theBox.y), cvPoint heBox.x+theBox.width,theBox.y+theBox.height),
cvScalar(0xff,0x00,0x00) );
} void mouse(int event,int x,int y,int flags,void * parameter){
Mat* image = (Mat*) parameter;
switch( event ){
case CV_EVENT_MOUSEMOVE:
if( drawing_box ){
theBox.width = x-theBox.x;
theBox.height = y-theBox.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
theBox = cvRect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( theBox.width < 0 ){
theBox.x += theBox.width;
theBox.width *= -1;
}
if( theBox.height < 0 ){
theBox.y += theBox.height;
theBox.height *= -1;
}
draw_box( image, theBox );
//cv::rectangle(parameter, theBox, CV_RGB(255,0,0));
break;
}
}
int main(){
cv::Mat frame;
int key = 0 ;
const char* name = "box";
theBox = Rect(-1,-1,0,0);
cv::VideoCapture cap(1);
if (!cap.isOpened()){
cout<< " capture isn't open " << endl;
return -1;
}
cv::namedWindow(name);
while (key != 27){
cap >> frame;
cv::imshow(name,frame);
cv::setMouseCallback(name,mouse,&frame);
key = cv::waitKey(1);
}
cv::destroyAllWindows();
return 0;
}
答案 0 :(得分:0)
当视频流正在进行时,显示器正在更新......同时尝试绘制矩形很困难......因为矩形是基于鼠标移动和鼠标的输入绘制的移动独立于流视频帧...因此矩形可能会被覆盖(尽管不应该崩溃)。您可以尝试在视频帧显示的每次迭代中在视频帧上绘制透明(基于鼠标移动事件的大小...)屏幕...