#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
Mat image;
Point corner1, corner2;
Rect box;
bool ldown = false, lup = false;
void mouse_CallBack(int event, int x, int y, int flags, void *)
{
if (event == EVENT_LBUTTONDOWN)
{
ldown = true;
corner1.x = x;
corner1.y = y;
cout << "corner 1 recorded at" << corner1 << endl;
}
if (event == EVENT_LBUTTONUP)
{
lup = true;
corner2.x = x;
corner2.y = y;
cout << "corner 2 recorded at" << corner2 << endl;
}
if (ldown == true && lup == false)
{
Point pt;
pt.x = x;
pt.y = y;
Mat local_img = image.clone();
rectangle(local_img, corner1, pt, Scalar(225, 0, 255)); //Scalar函数什么意思呢
imshow("原图像", local_img);
}
if (ldown == true && lup == true)
{
box.width = abs(corner1.x - corner2.x);
box.height = abs(corner1.y - corner2.y);
box.x = min(corner1.x, corner2.x);
box.y = min(corner1.y, corner2.y);
Mat crop(image, box);
namedWindow("截取图像");
imshow("截取图像", crop);
ldown = false;
lup = false;
}
}
int main()
{
image = imread("f:\\图片\\小包总.jpg");
namedWindow("原图像");
setMouseCallback("原图像", mouse_CallBack);
imshow("原图像",image);
waitKey(0);
return 0;
}
数字图像处理3:鼠标控件
最新推荐文章于 2022-10-24 20:47:52 发布