矩形框拖动
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>using namespace std;
using namespace cv;#define WINDOW_NAME "矩形拖動"Mat src_image;
Mat dst_image;
Rect rect;
Point pt;
bool flag_mouse_contained;void Mouse_Callback(int event, int x, int y, int flags, void* userdata)
{switch (event){case EVENT_LBUTTONDOWN:if (rect.contains(Point(x, y))){flag_mouse_contained = true;pt = Point(x, y);}break;case EVENT_MOUSEMOVE:if (flag_mouse_contained == true){rect.x += (x - pt.x); //當前鼠標坐標減去上一次回調事件的鼠標坐標rect.y += (y - pt.y);}break;case EVENT_LBUTTONUP:flag_mouse_contained = false;break;}pt = Point(x, y); //記錄當前鼠標坐標用于下一次回調事件
}int main(int argc, char** argv)
{flag_mouse_contained = false;src_image = Mat(256, 256, CV_32FC3, Scalar(255, 255, 255));namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);setMouseCallback(WINDOW_NAME, Mouse_Callback, 0);rect = Rect(50, 50, 50, 50);src_image.copyTo(dst_image); //類似于winform中的Paint事件,只修改dst_image,scr_image保持不變rectangle(dst_image, rect, Scalar(0, 0, 0));imshow(WINDOW_NAME, dst_image);while (1){if (flag_mouse_contained == true){src_image.copyTo(dst_image);rectangle(dst_image, rect, Scalar(0, 0, 0));imshow(WINDOW_NAME, dst_image);}if (waitKey(10) == 27) //等待事件必須大于0,如果小于等于0,則程序會進入無限等待
{return 0;}}//cvDrawRect()return 0;
}
?
轉載于:https://www.cnblogs.com/canyeweiwei/p/10504588.html
總結
- 上一篇: 记一次阿里面试题:都有哪些进程间通信方式
- 下一篇: 这样写代码,真是帅到没有朋友