任务:利用鼠标在给定图像上选取矩形,第一作为模板,之后作为匹配图像。
参考:opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像
利用模板匹配计算两幅图像的相似度:
double GetMatchVal(Mat& src, Mat& tpl)
{
double dis = 0.0;
if (!src.data || !tpl.data)
return dis;
//判断两幅图像的大小关系,如果输入的原始图像比匹配图像要小,则将原始图像作为模板,原来的模板图像作为搜索图
//模板匹配算法中已经包含了这一段
if (src.rows < tpl.rows || src.cols < tpl.cols)
std::swap(src, tpl);
int result_cols = src.cols - tpl.cols + 1;
int result_rows = src.rows - tpl.rows + 1;
if (result_cols <= 0 || result_rows <= 0) {
return dis;
}
Mat result = Mat::zeros(result_rows, result_cols, CV_32FC1);
matchTemplate(src, tpl, result, CV_TM_CCOEFF_NORMED);
double minVal = 0.0, maxVal = 0.0;
Point minLoc, maxLoc;
minMaxLoc(result, NULL, &maxVal, NULL, &maxLoc);
result.release();
dis = maxVal;
return dis;
}
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\nonfree\features2d.hpp>
#include <iostream>
using namespace cv;
cv::Mat org, dst, img, tmp,model;
bool first = false;
double GetMatchVal(Mat& src, Mat& tpl)
{
double dis = 0.0;
if (!src.data || !tpl.data)
return dis;
//判断两幅图像的大小关系,如果输入的原始图像比匹配图像要小,则将原始图像作为模板,原来的模板图像作为搜索图
//模板匹配算法中已经包含了这一段
if (src.rows < tpl.rows || src.cols < tpl.cols)
std::swap(src, tpl);
int result_cols = src.cols - tpl.cols + 1;
int result_rows = src.rows - tpl.rows + 1;
if (result_cols <= 0 || result_rows <= 0) {
return dis;
}
Mat result = Mat::zeros(result_rows, result_cols, CV_32FC1);
matchTemplate(src, tpl, result, CV_TM_CCOEFF_NORMED);
double minVal = 0.0, maxVal = 0.0;
Point minLoc, maxLoc;
minMaxLoc(result, NULL, &maxVal, NULL, &maxLoc);
result.release();
dis = maxVal;
return dis;
}
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)//左键按下,读取初始坐标,并在图像上该点处划圆
{
org.copyTo(img);//将原始图片复制到img中
sprintf(temp, "(%d,%d)", x, y);
pre_pt = Point(x, y);
putText(img, temp, pre_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255), 1, 8);//在窗口上显示坐标
circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);//划圆
imshow("img", img);
}
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(tmp, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));//只是实时显示鼠标移动的坐标
imshow("img", tmp);
}
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(tmp, 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);
putText(img, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//根据初始点和结束点,将矩形画到img上
imshow("img", img);
img.copyTo(tmp);
//截取矩形包围的图像,并保存到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));
if (!first)
{
model = dst;
first = true;
imshow("model", model);
}
else
{
double distence = GetMatchVal(model, dst);
printf("与第1次选定的图像区域相似度为:%f\n", distence);//越大,相似度越大
}
namedWindow("dst");
imshow("dst", dst);
waitKey(0);
}
}
void main()
{
string name = "./image/1.png";
org = imread(name, 0);
org.copyTo(img);
org.copyTo(tmp);
namedWindow("img");//定义一个img窗口
setMouseCallback("img", on_mouse, 0);//调用回调函数
imshow("img", img);
cv::waitKey(0);
}