环境W10+vs2017+Opencv3.2
通过鼠标四点获取一个矩形
void GetRectTwoPoint::CornerGetPoint()
{
namedWindow("findCornerImage");
imshow("findCornerImage", sourceImage);
GlobalRecImage = sourceImage.clone();
setMouseCallback("findCornerImage", FindCornerMouseEvent, 0); //根据鼠标获取感兴趣的区域
waitKey(0);
}
void FindCornerMouseEvent(int event, int x, int y, int flags, void* data)
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
startPoint = Point(x, y);
if (!got_beigin_point)
{
foundPoint.push_back(startPoint);
foundPointMouse.push_back(startPoint);
got_beigin_point = true;
}
break;
case CV_EVENT_MOUSEMOVE:
// if (got_beigin_point)
// {
// endPoint = Point(x, y);
// }
break;
case CV_EVENT_RBUTTONDOWN:
got_end_point = true;
endPoint = Point(x, y);
foundPoint.push_back(endPoint);
foundPointMouse.push_back(endPoint);
break;
case CV_EVENT_MBUTTONDOWN:
imwrite("C:\\Users\\user\\Desktop\\simon涂胶项目\\涂胶Opencv\\x64\\Debug\\image\\1.jpg", GlobalRecImage);
WriteFourPoint();
break;
default:
break;
}
if (got_end_point)
{
line(GlobalRecImage, startPoint, endPoint, Scalar(0, 0, 255), 5);
got_end_point = false;
imshow("findCornerImage", GlobalRecImage);
waitKey(0);
}
}