(附代码)干货 | OpenCV实现边缘模板匹配算法
目标检测与深度学习
共 8739字,需浏览 18分钟
·
2021-08-13 00:45
点击左上方蓝字关注我们
OpenCV中自带的模板匹配算法,完全是像素基本的模板匹配,特别容易受到光照影响,光照稍微有所不同,该方法就会歇菜了!搞得很多OpenCV初学者刚学习到该方法时候很开心,一用该方法马上很伤心,悲喜交加,充分感受到了理想与现实的距离,不过没关系,这里介绍一种新的模板匹配算法,主要是基于图像边缘梯度,它对图像光照与像素迁移都有很强的抗干扰能力,据说Halcon的模板匹配就是基于此的加速版本,在工业应用场景中已经得到广泛使用。
该算法主要是基于图像梯度,实现基于梯度级别的NCC模板匹配,基于Sobel梯度算子得到dx, dy, magnitude
通过Canny算法得到边缘图像、基于轮廓发现得到所有的轮廓点集,基于每个点计算该点的dx、dy、magnitude(dxy)三个值。生成模板信息。然后对输入的图像进行Sobel梯度图像之后,根据模型信息进行匹配,这样的好处有两个:
梯度对光照有很强的抗干扰能力,对模板匹配的抗光照干扰
基于梯度匹配,可以对目标图像上出现的微小像素迁移进行抵消。
梯度图像计算
Mat gx, gy;
Sobel(gray, gx, CV_32F, 1, 0);
Sobel(gray, gy, CV_32F, 0, 1);
Mat magnitude, direction;
cartToPolar(gx, gy, magnitude, direction);
long contoursLength = 0;
double magnitudeTemp = 0;
int originx = contours[0][0].x;
int originy = contours[0][0].y;
模板生成
// 提取dx\dy\mag\log信息
vector<vector<ptin>> contoursInfo;
// 提取相对坐标位置
vector<vector<Point>> contoursRelative;
// 开始提取
for (int i = 0; i < contours.size(); i++) {
int n = contours[i].size();
contoursLength += n;
contoursInfo.push_back(vector<ptin>(n));
vector<Point> points(n);
for (int j = 0; j < n; j++) {
int x = contours[i][j].x;
int y = contours[i][j].y;
points[j].x = x - originx;
points[j].y = y - originy;
ptin pointInfo;
pointInfo.DerivativeX = gx.at<float>(y, x);
pointInfo.DerivativeY = gy.at<float>(y, x);
magnitudeTemp = magnitude.at<float>(y, x);
pointInfo.Magnitude = magnitudeTemp;
if (magnitudeTemp != 0)
pointInfo.MagnitudeN = 1 / magnitudeTemp;
contoursInfo[i][j] = pointInfo;
}
contoursRelative.push_back(points);
}
计算目标图像梯度
// 计算目标图像梯度
Mat grayImage;
cvtColor(src, grayImage, COLOR_BGR2GRAY);
Mat gradx, grady;
Sobel(grayImage, gradx, CV_32F, 1, 0);
Sobel(grayImage, grady, CV_32F, 0, 1);
Mat mag, angle;
cartToPolar(gradx, grady, mag, angle);
NCC模板匹配
double partialScore = 0;
double resultScore = 0;
int resultX = 0;
int resultY = 0;
double start = (double)getTickCount();
for (int row = 0; row < grayImage.rows; row++) {
for (int col = 0; col < grayImage.cols; col++) {
double sum = 0;
long num = 0;
for (int m = 0; m < contoursRelative.size(); m++) {
for (int n = 0; n < contoursRelative[m].size(); n++) {
num += 1;
int curX = col + contoursRelative[m][n].x;
int curY = row + contoursRelative[m][n].y;
if (curX < 0 || curY < 0 || curX > grayImage.cols - 1 || curY > grayImage.rows - 1) {
continue;
}
// 目标边缘梯度
double sdx = gradx.at<float>(curY, curX);
double sdy = grady.at<float>(curY, curX);
// 模板边缘梯度
double tdx = contoursInfo[m][n].DerivativeX;
double tdy = contoursInfo[m][n].DerivativeY;
// 计算匹配
if ((sdy != 0 || sdx != 0) && (tdx != 0 || tdy != 0))
{
double nMagnitude = mag.at<float>(curY, curX);
if (nMagnitude != 0)
sum += (sdx * tdx + sdy * tdy) * contoursInfo[m][n].MagnitudeN / nMagnitude;
}
// 任意节点score之和必须大于最小阈值
partialScore = sum / num;
if (partialScore < min((minScore - 1) + (nGreediness * num), nMinScore * num))
break;
}
}
// 保存匹配起始点
if (partialScore > resultScore)
{
resultScore = partialScore;
resultX = col;
resultY = row;
}
}
}
正常光照
光照非常暗
改进:
不需要全局匹配,可以对目标图像先做一个小梯度阈值,然后再进行匹配,提升速度、构造目标图像金字塔,实现多分辨率模板匹配支持!
觉得不错点【好看】支持一下!
参考:
https://www.codeproject.com/articles/99457/edge-based-template-matching
END
整理不易,点赞三连↓
评论