#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
static std::vector<std::vector<cv::Point>> vctvctPoint;
cv::Mat org = cv::Mat(750,1000,CV_8U,cv::Scalar(0)), img = cv::Mat(750, 1000, CV_8UC3, cv::Scalar(255,255,255,255)), tmp;
void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号
{
static std::vector<cv::Point> vctPoint;
static cv::Point ptStart = (-1, -1);
static cv::Point cur_pt = (-1, -1);
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)
{
sprintf(temp, "(%d,%d)", x, y);
ptStart = cv::Point(x, y);
vctPoint.push_back(ptStart);
img.copyTo(tmp);
cv::circle(tmp, ptStart, 2, cv::Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
cv::putText(tmp, temp, ptStart, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0), 1, 8);
imshow("img", tmp);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
sprintf(temp, "(%d,%d)", x, y);
cur_pt = cv::Point(x, y);
cv::line(img, vctPoint.back(), cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0);
vctPoint.push_back(cur_pt);
img.copyTo(tmp);
circle(tmp, ptStart, 2, cv::Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
putText(tmp, temp, cur_pt, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0));
imshow("img", tmp);
}
else if (event == CV_EVENT_LBUTTONUP)
{
vctvctPoint.push_back(vctPoint);
sprintf(temp, "(%d,%d)", x, y);
cur_pt = cv::Point(x, y);
cv::line(img, ptStart, cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0);
img.copyTo(tmp);
circle(tmp, ptStart, 2, cv::Scalar(255, 0, 255, 0), CV_FILLED, CV_AA, 0);
imshow("img", tmp);
}
cv::waitKey(0);
}
int main()
{
namedWindow("img");//定义一个img窗口
setMouseCallback("img", on_mouse, 0);//调用回调函数
imshow("img", img);
cv::waitKey(0);
}