我正在使用opencv-python计算两个图像之间的形状上下文距离。这是我的代码:
import cv2
import numpy as np
# images loaded as grayscale images
im1 = cv2.imread("first.bmp", 0)
im2 = cv2.imread("second.bmp", 0)
# converting to binary images
_, im1 = cv2.threshold(im1, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
_, im2 = cv2.threshold(im2, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# finding contours!!
_, cntr1, _ = cv2.findContours(im1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
_, cntr2, _ = cv2.findContours(im2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# In the next few lines I combine different contours founded in each image
# because the method that computes the distance needs single contour and
# would gives an error if given a set of contours
temp = cntr1[0]
for i in range(1, len(cntr1)):
temp = np.vstack([temp, cntr1[i]])
temp2 = cntr2[0]
for i in range(1, len(cntr2)):
temp2 = np.vstack([temp2, cntr2[i]])
# creating shape context distance extractor object, using it to compute
# distance and printing result
scde = cv2.createShapeContextDistanceExtractor()
distance = scde.computeDistance(temp, temp2)
print(distance)
现在,只要两个图像相对相似,一切似乎就可以了。例如image a和image b的距离为0.2716,这是非常低的,因此我们可以得出结论,这些图像的形状相同。 但是当图像的差异更大时,例如图像a和image c(水平翻转的图像),则会出现此错误:
cv2.error:OpenCV(3.4.2)C:\ projects \ opencv-python \ opencv \ modules \ core \ src \ matmul.cpp:1218:error:(-215:Assertion failed)type ==(((函数'cv :: gemmImpl'中的(6)&((1 << 3)-1))+((((2)-1)<< 3))
此错误是什么意思?我该怎么解决?
顺便说一句,组合轮廓的代码对我提供的图片没有影响,因为所有图片都只有一个轮廓。您只需删除这些行并在倒数第二行中使用distance = scde.computeDistance(cntr1[0],cntr2[0])
。
*真是个了不起的词