Skip to content

Instantly share code, notes, and snippets.

@1292765944
Last active May 7, 2016 15:57
Show Gist options
  • Save 1292765944/933e8604e7cb99fec56f to your computer and use it in GitHub Desktop.
Save 1292765944/933e8604e7cb99fec56f to your computer and use it in GitHub Desktop.
从鼠标获得矩形
cv::Rect target;
bool drawing_box = false;
bool gotBB = false;
// bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param) {
switch( event ){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
target.width = x-target.x;
target.height = y-target.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
target = cv::Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( target.width < 0 ){
target.x += target.width;
target.width *= -1;
}
if( target.height < 0 ){
target.y += target.height;
target.height *= -1;
}
gotBB = true;
break;
}
}
void getRoi(VideoCapture capture) {
cvNamedWindow("Tracking", 0);
cvSetMouseCallback("Tracking", mouseHandler, NULL);
Mat frame,first,grayImg;
capture >> frame;
frame.copyTo(first);
while(!gotBB)
{
first.copyTo(frame);
rectangle(frame,target,Scalar(255,0,0),2);
imshow("Tracking", frame);
if (cvWaitKey(20) == 27)
return;
}
cvSetMouseCallback("Tracker", NULL, NULL );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment