任務:利用鼠標在給定圖像上選取矩形,第一作爲模板,之後作爲匹配圖像。
參考: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);
}