Hu矩比较
我尝试比较两张图片,并使用胡矩(Hu moment)来比较从这些图片中提取的轮廓:第一张图片 和 第二张图片。第二张图片和第一张图片是一样的,只是旋转了,我本来期待得到相同的胡矩结果,但它们有一点不同。
第一张图片的胡矩结果:
[[ 6.82589151e-01]
[ 2.06816713e-01]
[ 1.09088295e-01]
[ 5.30020870e-03]
[ -5.85888607e-05]
[ -6.85171823e-04]
[ -1.13181280e-04]]
第二张图片的胡矩结果:
[[ 6.71793060e-01]
[ 1.97521128e-01]
[ 9.15619847e-02]
[ 9.60179567e-03]
[ -2.44655863e-04]
[ -2.68791106e-03]
[ -1.45592441e-04]]
在这个视频里:http://www.youtube.com/watch?v=O-hCEXi3ymU,在第4分钟的时候,我看到他得到了完全相同的结果。我哪里出错了呢?
这是我的代码:
nomeimg = "Sassatelli 1984 ruotato.jpg"
#nomeimg = "Sassatelli 1984 n. 165 mod1.jpg"
img = cv2.imread(nomeimg)
gray = cv2.imread(nomeimg,0)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(4,4))
imgbnbin = thresh
imgbnbin = cv2.dilate(imgbnbin, element)
#find contour
contours,hierarchy=cv2.findContours(imgbnbin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#Elimination small contours
Areacontours = list()
area = cv2.contourArea(contours[i])
if (area > 90 ):
Areacontours.append(contours[i])
contours = Areacontours
print('found objects')
print(len(contours))
#contorus[3] for sing in first image
#contours[0] for sign in second image
print("humoments")
mom = cv2.moments(contours[0])
Humoments = cv2.HuMoments(mom)
print(Humoments)
1 个回答
15
我觉得你的数字可能没问题,它们之间的差异比较小。正如你链接的视频中那位先生所说(大约在3分钟处):
为了得到一些有意义的答案,我们进行对数变换。
所以如果我们对你上面提供的数据使用 -np.sign(a)*np.log10(np.abs(a))
,我们会得到:
第一张图片:
[[ 0.16584062]
[ 0.68441437]
[ 0.96222185]
[ 2.27570703]
[-4.23218495]
[-3.16420051]
[-3.9462254 ]]
第二张图片:
[[ 0.17276449]
[ 0.70438644]
[ 1.0382848 ]
[ 2.01764754]
[-3.61144437]
[-2.57058511]
[-3.83686117]]
它们不完全相同是正常的。你一开始使用的是栅格化的图像,然后经过很多处理才得到一些轮廓,这些轮廓你再传入。
根据 opencv 文档:
在栅格图像的情况下,原始图像和变换后的图像计算出的 Hu 不变性是有点不同的。