鼠标选择点生成像素坐标位置
#include<opencv2/opencv.hpp>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include<iostream>
using namespace std;
using namespace cv;
vector<Point> mousePoints;
Point points;
void on_mouse(int EVENT, int x, int y, int flags, void* userdata)
{
Mat hh;
hh = *(Mat*)userdata;
Point p(x, y);
switch (EVENT)
{
case EVENT_LBUTTONDOWN:
{
points.x = x;
points.y = y;
mousePoints.push_back(points);
circle(hh, points, 1, Scalar(255, 255, 255), -1);
imshow("mouseCallback", hh);
}
break;
}
}
int selectpoint(cv::Mat srcMat)
{
cv::Mat selectMat;
if (!srcMat.empty()) {
srcMat.copyTo(selectMat);
}
else {
std::cout << "failed to read image!:" << std::endl;
return -1;
}
namedWindow("mouseCallback");
imshow("mouseCallback", selectMat);
setMouseCallback("mouseCallback", on_mouse, &selectMat);
waitKey(0);
destroyAllWindows();
}
int main()
{
Mat src = imread("C:/Users/17482/Desktop/222.jpg");
int a = selectpoint(src);
if (a != -1)
{
for (int i = 0; i < mousePoints.size(); i++)
{
cout << "点" << i + 1 << "的x坐标为:" << mousePoints[i].x << "\ty坐标为:" << mousePoints[i].y << endl;
}
}
else
{
cout << "失败" << endl;
}
system("pause");
return 0;
}