需求,拖动鼠标产生矩形,截取ROI区域。 此绘制方式和大部分的方式不同。传统使用矩形框绘制必须要起点(sp)大于终点(ep),因此绘制矩形框只能如下。 这是由于在OpenCV中矩形框的语法为Rect(x,y,dx,dy)。其中x,y为矩形的起点,dx,dy为矩形的长和宽。也就是说矩形框必须知道起点和矩形框的高和宽。 为了解决这个问题,采用了line函数绘制矩形框,具体代码如下:
#include<stdio.h>
#include<opencv2/opencv.hpp>
#include<string>
#include"imf.h"
using namespace std;
Point sp(-1, -1);
Point ep(-1, -1);
Mat temp;
static void on_draw(int event, int x, int y, int flags, void* userdata) {
Mat image = *((Mat*)userdata);
if (event == EVENT_LBUTTONDOWN) {
sp.x = x;
sp.y = y;
std::cout << "start point:" << sp << std::endl;
}
else if (event == EVENT_LBUTTONUP) {
ep.x = x;
ep.y = y;
int dx = ep.x - sp.x;
int dy = ep.y - sp.y;
temp.copyTo(image);
Rect box(min(sp.x, ep.x), min(sp.y, ep.y), abs(dx), abs(dy));
imshow("ROI区域", image(box));
line(image, Point(sp.x, sp.y), Point(ep.x, sp.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(sp.x, sp.y), Point(sp.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(ep.x, sp.y), Point(ep.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(sp.x, ep.y), Point(ep.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
imshow("鼠标绘制", image);
sp.x = -1;
sp.y = -1;
std::cout << "end point" << ep << std::endl;
}
else if (event == EVENT_MOUSEMOVE) {
if (sp.x > 0 && sp.y > 0) {
ep.x = x;
ep.y = y;
temp.copyTo(image);
line(image, Point(sp.x, sp.y), Point(ep.x, sp.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(sp.x, sp.y), Point(sp.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(ep.x, sp.y), Point(ep.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
line(image, Point(sp.x, ep.y), Point(ep.x, ep.y), Scalar(0, 0, 255), 3, 8, 0);
imshow("鼠标绘制", image);
}
}
}
void mouse_drawing_demo(Mat& image) {
namedWindow("鼠标绘制", WINDOW_FREERATIO);
setMouseCallback("鼠标绘制", on_draw, (void*)(&image));
imshow("鼠标绘制", image);
temp = image.clone();
}
int main() {
Mission M;
Mat src = imread("D:/image/N.jpg");
if (src.empty()) {
printf("could not load the image\n");
return -1;
}
M.mouse_drawing_demo(src);
waitKey(0);
return 0;
}
效果展示
|