highgui: add ROI selector

This commit is contained in:
Vladislav Sovrasov
2017-04-19 14:08:37 +03:00
parent 2922738b6d
commit ad7cf58450
5 changed files with 243 additions and 56 deletions
@@ -159,7 +159,7 @@ int main(int argc, char **argv)
cout << "Please select a bounding box, and press any key to continue." << endl;
vector<Point2f> bb;
cv::Rect2d uBox = selectROI(video_name, frame);
cv::Rect uBox = cv::selectROI(video_name, frame);
bb.push_back(cv::Point2f(static_cast<float>(uBox.x), static_cast<float>(uBox.y)));
bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y)));
bb.push_back(cv::Point2f(static_cast<float>(uBox.x+uBox.width), static_cast<float>(uBox.y+uBox.height)));
@@ -56,59 +56,4 @@ vector<Point2f> Points(vector<KeyPoint> keypoints)
}
return res;
}
Rect2d selectROI(const String &video_name, const Mat &frame)
{
struct Data
{
Point center;
Rect2d box;
static void mouseHandler(int event, int x, int y, int flags, void *param)
{
Data *data = (Data*)param;
switch( event )
{
// start to select the bounding box
case EVENT_LBUTTONDOWN:
data->box = cvRect( x, y, 0, 0 );
data->center = Point2f((float)x,(float)y);
break;
// update the selected bounding box
case EVENT_MOUSEMOVE:
if(flags == 1)
{
data->box.width = 2 * (x - data->center.x);
data->box.height = 2 * (y - data->center.y);
data->box.x = data->center.x - data->box.width / 2.0;
data->box.y = data->center.y - data->box.height / 2.0;
}
break;
// cleaning up the selected bounding box
case EVENT_LBUTTONUP:
if( data->box.width < 0 )
{
data->box.x += data->box.width;
data->box.width *= -1;
}
if( data->box.height < 0 )
{
data->box.y += data->box.height;
data->box.height *= -1;
}
break;
}
}
} data;
setMouseCallback(video_name, Data::mouseHandler, &data);
while(waitKey(1) < 0)
{
Mat draw = frame.clone();
rectangle(draw, data.box, Scalar(255,0,0), 2, 1);
imshow(video_name, draw);
}
return data.box;
}
#endif // UTILS_H