OpenCV|OpenCV 基于距离变换的高精度轮廓匹配

轮廓匹配在定位测量应用中对其匹配的精度有更高的要求,通常的像素级的匹配结果难以满足其要求。本文给出了一种具有亚像素精度的快速轮廓匹配定位方法,其进行数学计算的基础为二值图像的距离变换。
二值图像距离变换的概念由Rosenfeld和Pfaltz于1966年其论文中提出,目前广泛应用于计算机图形学,计算机视觉及GIS空间分析等领域,其基本含义是计算一个图像中非零像素点到最近的零像素点的距离,也就是对每一各非零像素点计算其到零像素点的最短距离,并将该距离值赋值给该非零像素位置,从而将一幅二值图像变换为一幅距离图像。OpenCV通过cv::distanceTransform()函数,给出了该功能的快速实现方法。并通过参数的方式给出了距离的几种定义方式,如欧式距离、马氏距离、倒角距离等。
高精度轮廓匹配的算法实现步骤:
假设待匹配的两个轮廓分别为A和B
(1)将轮廓A栅格化并生成为一幅二值图像Ia,其中轮廓点为255,背景点为0;
(2)将二值图Ia进行距离变换得到距离图Id;
(3)通过一个变换描述轮廓B和轮廓A之间的关系,如平移变换、刚体变换、仿射变换等;
(4)通过一定的搜索策略或者最优化方法搜索轮廓B在距离图Id中的最佳变换参数,并输出。
在上述过程中(4)是最关键的一步,具体的搜索策略可考虑使用Powell搜索、分支定界搜索或者Gauss-Newton方法。下面给出了使用Gauss-Newton方法实现求解平移变换的代码,仅供参考。其它复杂的变换可参考此过程完成。
1 数据栅格化,并距离变换

int GetRasterDistanceImage(cv::Point2f* refPoints, int pntsNum, double resolution, int& offX, int& offY, cv::Mat& rasterDistImg) { double maxX, minX, maxY, minY; int i; for (i = 0; i < pntsNum; ++ i) { maxX = refPoints[i].x; minX = refPoints[i].x; maxY = refPoints[i].y; minY = refPoints[i].y; break; } for ( ; i < pntsNum; ++ i) { if (refPoints[i].x > maxX) maxX = refPoints[i].x; if (refPoints[i].x < minX) minX = refPoints[i].x; if (refPoints[i].y > maxY) maxY = refPoints[i].y; if (refPoints[i].y < minY) minY = refPoints[i].y; } int nX = int((maxX - minX) / resolution) + 200; int nY = int((maxY - minY) / resolution) + 200; cv::Mat rasterImg; rasterImg.create(nY, nX, CV_8UC1); memset(rasterImg.data, 0xFF, nY * nX); double realCenterX = (maxX + minX) / 2.0f; double realCenterY = (maxY + minY) / 2.0f; int offsetX = nX / 2 - int(realCenterX / resolution + 0.5f); int offsetY = nY / 2 - int(realCenterY / resolution + 0.5f); uchar* dat = (uchar*)(rasterImg.data); for (i = 0; i < pntsNum; ++ i) { if (validateMask[i] == 0) continue; int x = (int)floor(refPoints[i].x / resolution + 0.5f) + offsetX; int y = nY - ((int)floor(refPoints[i].y / resolution + 0.5f) + offsetY); *(dat + nX * y + x) = 0; } cv::distanceTransform(rasterImg, rasterDistImg, CV_DIST_C, 3); offX = offsetX; offY = offsetY; return 1; }


2高精度轮廓匹配,并输出平移量 offX,offY
void MatchReferencePoints(cv::Mat& rasterDistImg, cv::Point2f* rasterPoints, int pointsNum , double& offX, double& offY) { offX = 0.0f; offY = 0.0f; cv::Mat imgB = rasterDistImg; double* pL = new double[pointsNum]; double* pA = new double[pointsNum * 2]; double a_b[2] = {0}; double deltaX[2]; double AL[2]; double MM[4]; double va0, va1, va2, va3, va4; double sx, sy; double pError = 0.0f; for (int i = 0; i < 100; ++ i) { double error = 0.0f; for (int j = 0; j < pointsNum; ++ j) { sx = myPoints[j].x + a_b[0]; sy = myPoints[j].y + a_b[1]; va0 = GetDataValue(imgB, sx, sy); va1 = GetDataValue(imgB, sx + 0.5, sy); va2 = GetDataValue(imgB, sx - 0.5, sy); va3 = GetDataValue(imgB, sx, sy + 0.5); va4 = GetDataValue(imgB, sx, sy - 0.5); pA[j] = va1 - va2; pA[j + pointsNum] = va3 - va4; pL[j] = 0.0f - va0; error += pL[j] * pL[j]; }if (i == 0) pError = error; else if (error > pError) break; pError = error; int nOff_M = 0; for(int j = 0; j < 2; ++ j){ for(int jj = 0; jj < 2; ++ jj){ MM[nOff_M + jj] = 0.0; for(int kk = 0; kk < pointsNum; ++ kk) MM[nOff_M + jj] += pA[j * pointsNum + kk] * pA[jj * pointsNum + kk]; }// M = A * A(T) nOff_M += 2; }cv::Mat cvMatM(2, 2, CV_64FC1, MM); cv::invert(cvMatM, cvMatM); for (int j = 0; j < 2; ++ j) { AL[j] = 0.0f; for (int kk = 0; kk < pointsNum; ++ kk) AL[j] += pA[j * pointsNum + kk] * pL[kk]; }for (int j = 0; j < 2; ++ j) { deltaX[j] = 0.0f; for (int jj = 0; jj < 2; ++ jj) deltaX[j] += MM[j * 2+ jj] * AL[jj]; }int s = 0; for (; s < 2; ++ s) { if (fabs(deltaX[s]) > 0.0001) break; }if (s == 2) break; a_b[0] += deltaX[0]; a_b[1] += deltaX[1]; } offX = a_b[0]; offY = a_b[1]; delete[] pL; delete[] pA; delete[] myPoints; double temp = sqrt(pError / pointsNum); }



【OpenCV|OpenCV 基于距离变换的高精度轮廓匹配】

    推荐阅读