下面是一个简单的代码演示,基于特征对齐,实现基于分差的缺陷检测。
用基于ORB特征的匹配结果,如下图所示,可以看到有一些错误的匹配点
基于ORB特征实现图像相关特征点匹配的代码实现如下:
constint MAX_FEATURES = 5000;
constfloat GOOD_MATCH_PERCENT = 0.45f;
//im1为待配准图片
//im2为模板图片
//im1Reg为配准后的图片
//h为单应性矩阵
void alignImages(Mat&im1, Mat&im2, Mat&im1Reg, Mat&h)
{
// 将图像转为灰度图
Mat im1Gray, im2Gray;
cvtColor(im1, im1Gray, COLOR_BGR2GRAY);
cvtColor(im2, im2Gray, COLOR_BGR2GRAY);
// 存储特征与特征描述子的变量
std::vector keypoints1, keypoints2;
Mat descriptors1, descriptors2;
// 检测ORB特征计算特征描述子.
Ptr orb = ORB::create(MAX_FEATURES);
orb->detectAndCompute(im1Gray, Mat(), keypoints1, descriptors1);
clock_t start, end;
start = clock();
orb->detectAndCompute(im2Gray, Mat(), keypoints2, descriptors2); //77ms
// 特征匹配.
std::vector matches;
Ptr matcher = DescriptorMatcher::create("BruteForce-Hamming");
matcher->match(descriptors1, descriptors2, matches, Mat());
// Sort matches by score
std::sort(matches.begin(), matches.end());
//基于GMS的特征匹配算法
//vector matchesAll, matchesGMS;
//BFMatcher matcher(NORM_HAMMING);
//std::vector matches;
//matcher.match(descriptors1, descriptors2, matchesAll);
//cout << "matchesAll: " << matchesAll.size() << endl;
//matchGMS(im1.size(), im2.size(), keypoints1, keypoints2, matchesAll, matches);
//std::sort(matches.begin(), matches.end());
end = clock();
cout << (float)(end - start) * 1000 / CLOCKS_PER_SEC<<"ms"<< endl;
// 移除不好的匹配点
constint numGoodMatches = matches.size() * GOOD_MATCH_PERCENT;
matches.erase(matches.begin() + numGoodMatches, matches.end());
// 画匹配点
Mat imMatches;
drawMatches(im1, keypoints1, im2, keypoints2, matches, imMatches);
imwrite("matches.jpg", imMatches);
// 存储好的匹配点
std::vector points1, points2;
for (size_t i = 0; i < matches.size(); i++)
{
points1.push_back(keypoints1[matches[i].queryIdx].pt);
points2.push_back(keypoints2[matches[i].trainIdx].pt);
}
// 找出最优单映射变换矩阵h
h= findHomography(points1, points2, RANSAC);
// 利用h矩阵进行透视变换
warpPerspective(im1, im1Reg, h, im2.size());
}
Grid-based Motion Statistics(GMS)通过网格划分、运动统计特性的方法可以迅速剔除错误匹配,以此来提高匹配的稳定性。ORB+GMS的匹配效果如下,可见错误的匹配点少了很多。
配准后的图如下图所示:
将配准后的图与基准模板图做差分,效果如下:
进行形态学操作,
找出缺陷,比较大的缺陷可以找出来,较小的缺陷还是不能找出来。
这部分的代码实现如下:
int main(intargc, char **argv)
{
// Read reference image
string refFilename("8.jpg");
cout <<"Reading reference image : "<< refFilename << endl;
Mat imReference = imread(refFilename);
// Read image to be aligned
string imFilename("7.jpg");
cout <<"Reading image to align : "<< imFilename << endl;
Mat im = imread(imFilename);
// Registered image will be resotred in imReg.
// The estimated homography will be stored in h.
Mat imReg, h;
// Align images
cout <<"Aligning images ..."<< endl;
alignImages(im, imReference, imReg, h);
// Write aligned image to disk.
string outFilename("aligned.jpg");
cout <<"Saving aligned image : "<< outFilename << endl;
imwrite(outFilename, imReg);
// Print estimated homography
cout <<"Estimated homography : \n"<< h << endl;
Mat currentframe, previousframe;
cvtColor(imReference, previousframe, COLOR_BGR2GRAY);
cvtColor(imReg, currentframe, COLOR_BGR2GRAY); //转化为单通道灰度图
absdiff(currentframe, previousframe, currentframe);//做差求绝对值
imshow("1", currentframe);
imwrite("re.jpg", currentframe);
threshold(currentframe, currentframe, 120, 255.0, THRESH_BINARY);
imwrite("re11.jpg", currentframe);
erode(currentframe, currentframe, Mat());//腐蚀