标签:绘制 event form == mouse amp height bre raw
#include<opencv.hpp> using namespace cv;
RNG rngs{ 12345 };//生成颜色的随机数 Rect rectangles; bool DrawingBox = false; //鼠标回调函数,根据不同的鼠标事件进行不同的操作 void on_Mouse(int event, int x, int y, int flags, void* param) {
Mat& image = *(Mat*)param;
switch (event){ //鼠标移动 case EVENT_MOUSEMOVE: if (DrawingBox) {//“是否绘制”标识符为真,记录矩形长宽 rectangles.width = x - rectangles.x; rectangles.height = y - rectangles.y; } break; //左键按下 case EVENT_LBUTTONDOWN: DrawingBox = true; rectangles = Rect(x, y, 0, 0);//记录起始点 break; //左键抬起 case EVENT_LBUTTONUP: DrawingBox = false; if (rectangles.width < 0) { rectangles.x += rectangles.width; rectangles.width *= -1; } if (rectangles.height < 0) { rectangles.y += rectangles.height; rectangles.height *= -1; } rectangle(image, rectangles, Scalar(rngs.uniform(0, 255), rngs.uniform(0, 255), rngs.uniform(0, 255))); break; } }
int main() { //准备参数 rectangles = Rect(-1, -1, 0, 0); Mat srcImage(600,800,CV_8UC3), temImage; srcImage.copyTo(temImage); srcImage = Scalar::all(0); //设置鼠标操作回调函数 namedWindow("鼠标操作"); setMouseCallback("鼠标操作", on_Mouse, (void*)&srcImage); while (true){ srcImage.copyTo(temImage); if(DrawingBox) rectangle(temImage, rectangles, Scalar(rngs.uniform(0, 255), rngs.uniform(0, 255), rngs.uniform(0, 255))); imshow("鼠标操作", temImage); if (waitKey(10) == 27) break; } }
标签:绘制 event form == mouse amp height bre raw
原文地址:https://www.cnblogs.com/bjxqmy/p/11914601.html