Shape Context Error by calculating distance of two different shapes

▼魔方 西西 提交于 2019-12-02 11:58:43

问题


In short form, I want to compare different road markings by "shape context" matching.

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)

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 Error: Assertion failed (type == CV_64FC2) in gemmImpl, file /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp, line 1218

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: error: (-215) type == CV_64FC2 in function gemmImpl

The error only occurs with the function of generating distance of "shape context" and not with the function of generating distance of "Hausdorff"


回答1:


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.



来源:https://stackoverflow.com/questions/46808404/shape-context-error-by-calculating-distance-of-two-different-shapes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!