opencv19:轮廓凸包,距离与匹配
目標
在本章中,將學習
- 凸起缺陷檢測
- 尋找點到多邊形的最短距離
- 匹配不同的形狀
理論和代碼
凸性缺陷(Convexity Defects)
上一節學習了如何尋找輪廓, 凸包上的任何偏差都可以被認為是有凸性缺陷。
OpenCV中有一個函數來尋找凸包,cv2.convexityDefects()。
注意
必須在發現凸包時,傳遞returnPoints= False, 返回的是cnt中點的下標,如果是True時候,返回的是點的坐標。
cv2.convexityDefects返回一個數組,其中每行包含這些值[起點、終點、最遠點、到最遠點的近似距離]。可以用圖像把它形象化。畫一條連接起點和終點的線,然后在最遠處畫一個圓。記住,返回的前三個值是cnt的索引。必須從cnt中獲取這些值。
import cv2 import numpy as np img = cv2.imread('star.png') img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img_gray, 127, 255,0) contours,hierarchy = cv2.findContours(thresh,2,1) cnt = contours[0] hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) for i in range(defects.shape[0]):s,e,f,d = defects[i,0]start = tuple(cnt[s][0])end = tuple(cnt[e][0])far = tuple(cnt[f][0])cv2.line(img,start,end,[0,255,0],2)cv2.circle(img,far,5,[0,0,255],-1) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()點到多邊形測試
cv2.pointPolygonTest函數找出圖像中一點到輪廓線的最短距離。它返回的是符號距離距離(signed distance),點在輪廓線外時為負,點在輪廓線內時為正,點在輪廓線上時為零。
可以檢查點(50,50)如下:
dist = cv2.pointPolygonTest(cnt,(50,50),True) # -38.8329756778952在函數中,第三個參數是measureDist。如果它是True,它會找到有符號的距離。如果為False,則查找該點是在輪廓線內部還是外部(分別返回 1、-1和0)
注意
如果不想知道具體距離,請確保第三個參數為False,因為計算距離是一個耗時的過程, 設置為False可使速度提高2-3倍。
形狀匹配
OpenCV附帶一個函數cv2.matchShapes(),該函數能夠比較兩個形狀或兩個輪廓,并返回一個顯示相似性的度量。結果越低,匹配越好。它是根據矩值(hu-moment)計算出來的。不同的測量方法在文檔中有解釋。
# 形狀匹配 import cv2 import numpy as npimg1 = cv2.imread('star.png', 0) img2 = cv2.imread('star2.png', 0) img3 = cv2.imread('approx.png', 0) ret, thresh = cv2.threshold(img1, 127, 250, 0) ret2, thresh2 = cv2.threshold(img2, 127, 250, 0) ret3, thresh3 = cv2.threshold(img3, 127, 250, 0)contours1, hierarchy1 = cv2.findContours(thresh, 2, 1) cnt1 = contours1[0]contours2, hierarchy2 = cv2.findContours(thresh2, 2, 1) cnt2 = contours2[0]contours3, hierarchy3 = cv2.findContours(thresh3, 2, 1) cnt3 = contours3[0]ret1 = cv2.matchShapes(cnt1, cnt2, 1, 0.0) print(ret1) # 0.0101502123705427ret2 = cv2.matchShapes(cnt2, cnt3, 1, 0.0) print(ret2) # 0.9202466744178138嘗試過匹配下面給出的不同形狀的形狀:
得到以下結果:
- 匹配的圖像A與本身= 0.0
- 匹配圖像A與圖像B =0.0101502123705427
- 匹配圖像A與圖像C = 0.9202466744178138
可以看到,即使是圖像旋轉也不會對其匹配度產生比較大的影響
說明
Hu Moments(或者相反Hu moment invariants)是一種使用不變的圖像轉換的中央矩計算的一組7個數字。前6個moments被證明是不變的變換,縮放和旋轉和反轉。第7個moment表示符號變化。
附加資源
- https://docs.opencv.org/4.1.2/d5/d45/tutorial_py_contours_more_functions.html
- https://www.jianshu.com/p/42eb9b3957d5
- https://www.cnblogs.com/jclian91/p/9728488.html
- https://learnopencv.com/shape-matching-using-hu-moments-c-python/
總結
以上是生活随笔為你收集整理的opencv19:轮廓凸包,距离与匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iphone11返回上一级手势怎么设置_
- 下一篇: POI 处理word 文档中 文本框模板