函数cv2.matchShape()可以比较两个形状或轮廓的相似度。如果返回值越小,匹配越好。它是根据Hu矩来计算的,Hu矩是归一化中心矩的线性组合,之所以这么做是为了能够获取代表图像的某个特征的矩函数,这些矩函数对某些变换如缩放,旋转,镜像映射(除了h1)具有不变性。
double cv::matchShapes ( InputArray contour1,
InputArray contour2,
int method,
double parameter
)
Python:
cv.matchShapes( contour1, contour2, method, parameter ) -> retval
比较一下上面这三张图:
In [4]: img1 = cv2.imread(r'E:\comprehensive_library\Intelligent_material_online\lib\retrieval\data\base\ornament_1.png
...: ',0)
In [9]: img2 = cv2.imread(r'E:\comprehensive_library\Intelligent_material_online\lib\retrieval\data\base\jd2.jpg',0)
In [10]: img3 = cv2.imread(r'E:\comprehensive_library\Intelligent_material_online\lib\retrieval\data\base\mizhi1.png',0
...: )
In [17]: ret,thresh1=cv2.threshold(img1,240,255,0)
In [18]: cv2.namedWindow('thresh1')
...: cv2.imshow('thresh1',thresh1)
...: cv2.waitKey(0)
...: cv2.destroyWindow('thresh1')
...:
In [19]: ret,thresh2=cv2.threshold(img2,240,255,0)
In [20]: cv2.namedWindow('thresh2')
...: cv2.imshow('thresh2',thresh2)
...: cv2.waitKey(0)
...: cv2.destroyWindow('thresh2')
...:
In [21]: ret,thresh3=cv2.threshold(img3,240,255,0)
In [22]: cv2.namedWindow('thresh3')
...: cv2.imshow('thresh3',thresh1)
...: cv2.waitKey(0)
...: cv2.destroyWindow('thresh3')
...:
In [24]: contours,hierarchy = cv2.findContours(thresh1,2,1)
In [25]: cnt1 = contours[0]
In [26]: cnt1
Out[26]: array([[[ 64, 137]]], dtype=int32)
In [27]: contours1,hierarchy = cv2.findContours(thresh2,2,1)
In [28]: contours3,hierarchy = cv2.findContours(thresh3,2,1)
In [29]: cnt2=contours1[0]
In [30]: cnt2
Out[30]: array([[[291, 303]]], dtype=int32)
In [31]: cnt3=contours3[0]
In [33]: ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
In [34]: ret
Out[34]: 0.0
In [35]: ret = cv2.matchShapes(cnt1,cnt3,1,0.0)
In [36]: ret
Out[36]: 1.7976931348623157e+308
In [37]: ret = cv2.matchShapes(cnt1,cnt1,1,0.0)
In [38]: ret
Out[38]: 0.0
In [39]: ret = cv2.matchShapes(cnt3,cnt3,1,0.0)
In [40]: ret
Out[40]: 0.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)