该代码通过鼠标选取左上角和右下角两个点,并基于这两个点实现ROI区域的选取,ROI区域内所有像素值被赋予左上角选取点的像素值。
源代码
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include<opencv.hpp>
#include<cmath>
using namespace cv;
using namespace std;
cv::Mat org_, dst_, img_, tmp_;
int pointValue;
void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号
{
static Point pre_pt = (-1, -1);//初始坐标
static Point cur_pt = (-1, -1);//实时坐标
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆
{
pointValue=0;
org_.copyTo(img_);//将原始图片复制到img中
cout << img_.rows << " " << img_.cols << endl;
sprintf(temp, "(%d,%d)", x, y);
pre_pt = Point(x, y);
pointValue = img_.at<Vec3b>(pre_pt)[0];
cout << pointValue<<endl;
/* cout << org.at<Vec3b>(pre_pt)[1];
cout << org.at<Vec3b>(pre_pt)[2];*/
//初始坐标
//putText(org_, temp, pre_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255), 1, 8);//在窗口上显示坐标
//circle(org_, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);//划圆
imshow("img", org_);
}
else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数
{
img_.copyTo(tmp_);//将img复制到临时图像tmp上,用于显示实时坐标
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));//只是实时显示鼠标移动的坐标
imshow("img", org_);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动,则在图像上划矩形
{
img_.copyTo(tmp_);
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
rectangle(tmp_, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//在临时图像上实时显示鼠标拖动时形成的矩形
imshow("img", tmp_);
}
else if (event == CV_EVENT_LBUTTONUP)//左键松开,将在图像上划矩形
{
org_.copyTo(img_);
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
int height_ = abs(cur_pt.x - pre_pt.x);
int width_ = abs(cur_pt.y - pre_pt.y);
//Mat imageBlue, imageGreen, imageRed;
Mat imageBlue = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
Mat imageGreen = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
Mat imageRed = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
Mat mergeImage;
Mat test0,test1,test2;
vector<Mat> channelrgb;
Mat mer;
//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
//circle(org_, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
split(img_, channelrgb);
imageBlue = channelrgb.at(0);
imageGreen = channelrgb.at(1);
imageRed = channelrgb.at(2);
cout << imageRed.rows<<" "<<imageRed.cols<< endl;
if ((cur_pt.x > pre_pt.x) && (cur_pt.y > pre_pt.y))
{
for (int i = 0; i < (cur_pt.x - pre_pt.x); i++)
{
for (int j = 0; j <(cur_pt.y - pre_pt.y); j++)
{
imageBlue.at<uchar>(j + pre_pt.y, i + pre_pt.x) = pointValue;
imageGreen.at<uchar>(j + pre_pt.y, i + pre_pt.x) = pointValue;
imageRed.at<uchar>(j + pre_pt.y, i + pre_pt.x) = pointValue;
}
}
}
channelrgb.at(0) = imageBlue;
channelrgb.at(1) = imageGreen;
channelrgb.at(2) = imageRed;
test0 = channelrgb.at(0);
test1 = channelrgb.at(1);
test2 = channelrgb.at(2);
merge(channelrgb, org_);
cout << "cui" << endl;
//截取矩形包围的图像,并保存到dst中
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
if (width == 0 || height == 0)
{
printf("width == 0 || height == 0");
return;
}
dst_ = org_(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
//namedWindow("merge", 0);
//imshow("merge", org_);
imwrite("C:\\Users\\new\\Desktop\\test.bmp", org_);
waitKey(0);
}
}
void main()
{
org_ = imread("C:/Users/new/Desktop/1.bmp");
namedWindow("img",0);//定义一个img窗口
setMouseCallback("img", on_mouse, 0);//调用回调函数
//imshow("img", img_);
cv::waitKey(0);
}