void QuickDemo::elementsizechange_open_demo(Mat& image) {
//开操作
Mat dst;
int elementsize = 3;
Mat kernel = getStructuringElement(MORPH_RECT, Size(elementsize * 2 + 1, elementsize * 2 + 1));//保证是奇数
morphologyEx(image, dst, MORPH_OPEN, kernel);
imshow("OPEN", dst);
}
void QuickDemo::elementsizechange_close_demo(Mat& image) {
//闭操作
Mat dst;
int elementsize = 3;
Mat kernel = getStructuringElement(MORPH_RECT, Size(elementsize * 2 + 1, elementsize * 2 + 1));
morphologyEx(image, dst, MORPH_CLOSE, kernel);
imshow("CLOSE", dst);
}
void QuickDemo::elementsizechange_gradient_demo(Mat& image) {
//形态学梯度
Mat dst;
int elementsize = 3;
Mat kernel = getStructuringElement(MORPH_RECT, Size(elementsize * 2 + 1, elementsize * 2 + 1));
morphologyEx(image, dst, MORPH_GRADIENT, kernel);
imshow("GRADIENT", dst);
}
void QuickDemo::elementsizechange_tophat_demo(Mat& image) {
//顶帽操作
Mat dst;
int elementsize = 3;
Mat kernel = getStructuringElement(MORPH_RECT, Size(elementsize * 2 + 1, elementsize * 2 + 1));
morphologyEx(image, dst, MORPH_TOPHAT, kernel);
imshow("TOPHAT", dst);
}
void QuickDemo::elementsizechange_blackhat_demo(Mat& image) {
Mat dst;
int elementsize = 3;
Mat kernel = getStructuringElement(MORPH_RECT, Size(elementsize * 2 + 1, elementsize * 2 + 1));
morphologyEx(image, dst, MORPH_BLACKHAT, kernel);
imshow("BLACKHAT", dst);
}
模板匹配如下所示:
void QuickDemo::template_match_demo(Mat* pTo, Mat* pTemplate, Mat* src) {
//模板匹配
int i, j, m, n;
double dSumT;//模板元素的平均和
double dSumS;//图像子区域元素的平均和
double dSumST;//图像子区域和模板的点积
double R;//响应值
double MaxR;//记录当前的最大响应
//最大响应出现的位置
int nMaxX;
int nMaxY;
int nHeight = src->rows;
int nwidth = src->cols;
//模板的高和宽
int nTplHeight = pTemplate->rows;
int nTplwidth = pTemplate->cols;
//计算dSumT
dSumT = 0;
for (m = 0; m < nTplHeight; m++) {
for (n = 0; n < nTplwidth; n++) {
//模板图像第m行,第n个像素的灰度值
int nGray = *pTemplate->ptr(m, n);
dSumT += (double)nGray * nGray;
}
}
//找到图像中最大响应的出现位置
MaxR = 0;
for (i = 0; i < nHeight - nTplHeight + 1; i++) {
for (j = 0; j < nwidth - nTplwidth + 1; j++) {
dSumST = 0;
dSumS = 0;
for (m = 0; m < nTplHeight; m++) {
for (n = 0; n < nTplwidth; n++) {
// 原图像第i+m行,第j+n列象素的灰度值
int nGraySrc = *src->ptr(i + m, j + n);
//模板图像第m行,第n个像素的灰度值
int nGrayTp1 = *pTemplate->ptr(m, n);
dSumS += (double)nGraySrc * nGraySrc;
dSumST += (double)nGraySrc * nGrayTp1;
}
}
R = dSumS / (sqrt(dSumS) * sqrt(dSumT));//计算相应响应
//与最大相似性比较
if (R > MaxR) {
MaxR = R;
nMaxX = j;
nMaxY = i;
}
}
//将找到的最佳匹配区域复制到模板图像
for (int m=0; m < nTplHeight; m++) {
for (n = 0; n < nTplwidth; n++) {
int nGray = *src->ptr(nMaxY + m, nMaxX + n);
//pTo->setTo(nMaxX + n, nMaxY + m, RGB(nGray, nGray, nGray));
pTo->at(nMaxY + m, nMaxX + n) = nGray;
}
}
}
}
main文件如下所示:
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("D:\\testImage\\1.jpg",0);
Mat template_image = imread("D:\\testImage\\12.jpg", 0);
Mat pt = src;
if (src.empty()) {
printf("could not load image...");
return -1;
}
//如果图像过于大,不能完全显示整张图像可以使用namedWindow()可以实现对图像的自由缩放
pt.data = new BYTE[src.cols * src.rows];
memset(pt.data, 255, src.cols * src.rows);
QuickDemo qd;
qd.template_match_demo(&pt, &template_image, &src);
imshow("S", src);
imshow("T", template_image);
imshow("P", pt);
imwrite("S.jpg", src);
imwrite("T.jpg", template_image);
imwrite("P.jpg", pt);
waitKey(0);
destroyAllWindows();
return 0;
}