干貨 | 基于特征的圖像配準(zhǔn)用于缺陷檢測
點(diǎn)擊上方“小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時間送達(dá)
基于特征的圖像配準(zhǔn),具有非常廣泛的應(yīng)用,大致流程可以如下:

經(jīng)典的特征匹配算法有SIFT、SURF、ORB等,這三種方法在OpenCV里面都已實(shí)現(xiàn)。SURF基本就是SIFT的全面升級版,有 SURF基本就不用考慮SIFT,而ORB的強(qiáng)點(diǎn)在于計(jì)算時間,以下具體比較:
計(jì)算速度:ORB>>SURF>>SIFT(各差一個量級)
旋轉(zhuǎn)魯棒性:SURF>ORB~SIFT(~表示差不多)
模糊魯棒性:SURF>ORB~SIFT
尺度變換魯棒性:SURF>SIFT>ORB(ORB并不具備尺度變換性)
所以結(jié)論就是,如果對計(jì)算實(shí)時性要求非常高,可選用ORB算法,但基本要保證正對拍攝;如果對穩(wěn)定性要求稍高,可以選擇SURF;基本不用SIFT。此外補(bǔ)充一點(diǎn),自從OpenCV3.x開始,受到SIFT跟SURF專利授權(quán)的影響,OpenCV正式的發(fā)布版本中已經(jīng)移除了SIFT跟SURF算法。ORB特征提取算法是基于FAST跟BRIEF算法改進(jìn)的組合算法,其中FAST實(shí)現(xiàn)關(guān)鍵點(diǎn)/特征點(diǎn)的檢測,在此基礎(chǔ)上基于幾何矩添加方向?qū)傩?,BRIEF實(shí)現(xiàn)描述子生成,添加旋轉(zhuǎn)不變性支持。
ORB特征匹配速度快的一個原因之一就是使用字符串向量的描述子,避免了浮點(diǎn)數(shù)計(jì)算。字符串描述子匹配上可以采用漢明距離或者LSH改進(jìn)算法實(shí)現(xiàn),相比浮點(diǎn)數(shù)計(jì)算L2距離進(jìn)一步降低了計(jì)算量。所以在一般情況下建議使用ORB特征匹配,如果效果不好再嘗試AKAZE/SURF/SIFT等其它特征匹配算法。
兩幅圖像之間的基于特征匹配的透視變換矩陣求解通常被稱為圖像對齊或者配準(zhǔn)?;谔卣鞯钠ヅ淇梢院芎脤?shí)現(xiàn)圖像對齊或者配準(zhǔn),首先需要獲取兩張圖像的特征關(guān)鍵點(diǎn)與特征描述子,然后通過暴力匹配或者FLANN匹配尋找匹配度高的相關(guān)特征點(diǎn)。最后基于這些相關(guān)特征點(diǎn)估算它們之間的單應(yīng)性矩陣,通過單應(yīng)性矩陣實(shí)現(xiàn)透視變換,完成圖像對齊與配準(zhǔn)。OpenCV中有兩個函數(shù)可以獲得單映射變換矩陣,分別為:
- findHomography- getPerspectiveTransform
兩者之間的區(qū)別在于getPerspectiveTransform只會拿4個點(diǎn)去計(jì)算,findHomography則會拿一堆點(diǎn)(>=4)去計(jì)算。
下面是一個簡單的代碼演示,基于特征對齊,實(shí)現(xiàn)基于分差的缺陷檢測。

用基于ORB特征的匹配結(jié)果,如下圖所示,可以看到有一些錯誤的匹配點(diǎn)

基于ORB特征實(shí)現(xiàn)圖像相關(guān)特征點(diǎn)匹配的代碼實(shí)現(xiàn)如下:
constint MAX_FEATURES = 5000;
constfloat GOOD_MATCH_PERCENT = 0.45f;
//im1為待配準(zhǔn)圖片
//im2為模板圖片
//im1Reg為配準(zhǔn)后的圖片
//h為單應(yīng)性矩陣
void alignImages(Mat&im1, Mat&im2, Mat&im1Reg, Mat&h)
{
// 將圖像轉(zhuǎn)為灰度圖
Mat im1Gray, im2Gray;
cvtColor(im1, im1Gray, COLOR_BGR2GRAY);
cvtColor(im2, im2Gray, COLOR_BGR2GRAY);
// 存儲特征與特征描述子的變量
std::vector<KeyPoint> keypoints1, keypoints2;
Mat descriptors1, descriptors2;
// 檢測ORB特征計(jì)算特征描述子.
Ptr<Feature2D> 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<DMatch> matches;
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
matcher->match(descriptors1, descriptors2, matches, Mat());
// Sort matches by score
std::sort(matches.begin(), matches.end());
//基于GMS的特征匹配算法
//vector<DMatch> matchesAll, matchesGMS;
//BFMatcher matcher(NORM_HAMMING);
//std::vector<DMatch> 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;
// 移除不好的匹配點(diǎn)
constint numGoodMatches = matches.size() * GOOD_MATCH_PERCENT;
matches.erase(matches.begin() + numGoodMatches, matches.end());
// 畫匹配點(diǎn)
Mat imMatches;
drawMatches(im1, keypoints1, im2, keypoints2, matches, imMatches);
imwrite("matches.jpg", imMatches);
// 存儲好的匹配點(diǎn)
std::vector<Point2f> 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);
}
// 找出最優(yōu)單映射變換矩陣h
h= findHomography(points1, points2, RANSAC);
// 利用h矩陣進(jìn)行透視變換
warpPerspective(im1, im1Reg, h, im2.size());
}Grid-based Motion Statistics(GMS)通過網(wǎng)格劃分、運(yùn)動統(tǒng)計(jì)特性的方法可以迅速剔除錯誤匹配,以此來提高匹配的穩(wěn)定性。ORB+GMS的匹配效果如下,可見錯誤的匹配點(diǎn)少了很多。

配準(zhǔn)后的圖如下圖所示:

將配準(zhǔn)后的圖與基準(zhǔn)模板圖做差分,效果如下:

進(jìn)行形態(tài)學(xué)操作,

找出缺陷,比較大的缺陷可以找出來,較小的缺陷還是不能找出來。

這部分的代碼實(shí)現(xiàn)如下:
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); //轉(zhuǎn)化為單通道灰度圖
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());//腐蝕
dilate(currentframe, currentframe, Mat());//膨脹
dilate(currentframe, currentframe, Mat());//膨脹
imshow("moving area", currentframe); //顯示圖像
vector<vector<Point>> v;
vector<Vec4i> hierarchy;
Mat result;
Rect rect;
findContours(currentframe, v, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
for (int i = 0; i < hierarchy.size(); i++)
{
rect = boundingRect(v.at(i));
if (rect.area() > 1)
{
rectangle(imReg, rect, Scalar(0, 0, 255), 2);
}
}
imwrite("res1.jpg", imReg);
imshow("moving area1", imReg);
waitKey(0);
}交流群
歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動駕駛、計(jì)算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會逐漸細(xì)分),請掃描下面微信號加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進(jìn)入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~

