通过计算两个不同形状的距离获得形状上下文错误 [英] Shape Context Error by calculating distance of two different shapes
问题描述
简而言之,我想通过形状上下文"匹配来比较不同的道路标记.
In short form, I want to compare different road markings by "shape context" matching.
我的第一个问题可以在下面看到:从OpenData匹配道路标记的形状
My first question you can see under: Matching shapes of road marking from OpenData
我解决了第一个问题,但现在出现了一个新错误.这是我的代码:
I solved my first problem, but now it appears a new error. Here is my code:
import cv2
import numpy as np
# read data
datapath = "/Users/output/test/";
a = cv2.imread(datapath+"template_orig.png");
b = cv2.imread(datapath+"template.png");
imgray_a = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
ret_a,thresh_a = cv2.threshold(imgray_a,127,255,0)
imgray_b = cv2.cvtColor(b,cv2.COLOR_BGR2GRAY)
ret_b,thresh_b = cv2.threshold(imgray_b,127,255,0)
# find contours
_, ca, _ = cv2.findContours(thresh_a, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
_, cb, _ = cv2.findContours(thresh_b, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
print(np.shape(ca[0]), np.shape(cb[0]))
# generate distance --> Hausdorff OR ShapeContext
hd = cv2.createHausdorffDistanceExtractor()
sd = cv2.createShapeContextDistanceExtractor()
d1 = hd.computeDistance(ca[0],cb[0])
d2 = sd.computeDistance(ca[0],cb[0])
print(d1, " ", d2)
当我比较 a 时(原始转向箭头) 与 b (提取转向箭头)没有问题,但是当我比较 a 与 c (用于测试形状匹配"算法的其他方法)出现以下错误:
When I compare a (original turn arrow) with b (extracted turn arrow) there are no problems, but when I compare a with c (anything other to test "shape matching" algorithm) the following error appear:
OpenCV错误:gemImpl,文件/Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp中的断言失败(类型== CV_64FC2), 第1218行
OpenCV Error: Assertion failed (type == CV_64FC2) in gemmImpl, file /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp, line 1218
跟踪(最近一次通话最近):文件"/test_shape.py",第74行,在 d2 = sd.computeDistance(ca [0],cb [0])
Traceback (most recent call last): File "/test_shape.py", line 74, in d2 = sd.computeDistance(ca[0],cb[0])
cv2.error:/Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp:1218: 错误:函数gemmImpl中(-215)类型== CV_64FC2
cv2.error: /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp:1218: error: (-215) type == CV_64FC2 in function gemmImpl
仅在生成形状上下文"距离的函数中发生错误,而不在生成"Hausdorff"距离的函数中发生
The error only occurs with the function of generating distance of "shape context" and not with the function of generating distance of "Hausdorff"
推荐答案
好吧,我认为问题是 a 和 b 或 a 和 c 的像素高度不同( a :131 x 32像素, b/c :29 x 18像素) .当我将 b 或 c 的图像尺寸更改为更高的分辨率(如131 x 81 px)时,错误消失了,并且正在计算形状上下文"的距离.
Ok, I think the problem was, that a and b or a and c don't have the same height of pixels (a: 131 x 32px, b / c: 29 x 18 px). When I change the size of the image of b or c to a higher resolution like 131 x 81 px the error disappear and the distance of "shape context" is being computed.
这篇关于通过计算两个不同形状的距离获得形状上下文错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!