从OpenData匹配道路标记的形状

时间:2017-10-17 11:18:57

标签: python python-2.7 python-3.x opencv shape-context

现在我正致力于从OpenData生成道路标记。因为我是编程中的新手,所以我使用Python。我处理正射影像并将它们转换为二进制照片(仅黑白像素)。之后,我得到这样的照片:

下一步是识别给定示例图片上的各种道路标记。我认为这应该可以通过"形状上下文匹配"。

所以我写了这段代码(a a是原来的转弯箭头而b b是从示例图片中提取的转弯箭头)来比较这里各种转弯箭头与原来的转弯箭头:

import cv2
import numpy as np

# read data
datapath = "/Users/output/test/";
a = cv2.imread(datapath+"template_orig.png",0);
b = cv2.imread(datapath+"template.png",0);

# find contours
ca = cv2.findContours(a, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
cb = cv2.findContours(b, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
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

但是当我想执行我的代码时,它会说:

cv2.error:/Users/travis/build/skvark/opencv-python/opencv/modules/shape/src/haus_dis.cpp:139:错误:(-215)(set1.channels()= = 2)&&函数computeDistance

中的(set1.cols> 0)

进一步解决" a"是: 32 x 131 px 以及" b"的解决方案是: 18 x 29 px

感谢您的努力:)

修改

我将代码更改为以下内容:

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 的比较结果如下: d1 = 28.4604988098,d2 = 0.320339113474

但是当我将 a 与例如进行比较时 c )程序因以下错误而停止:

cv2.error:/Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matmul.cpp:1218:错误:(-215)type == CV_64FC2 in function gemmImpl

0 个答案:

没有答案