【发布时间】:2014-05-22 01:54:07
【问题描述】:
所以我正在尝试编写一个函数,我们称之为 foo,它采用二进制图像的路径,获取沿它的霍夫线,然后返回按霍夫线沿线的白色像素数量排序的线。这是我到目前为止的代码,但它在“if(image[(x1+(istepx)),(y1+(istepy))].any()):”行崩溃了索引无效。你们知道我可以做些什么来修复这个错误或知道 OpenCV 中内置的函数可以做我想做的事吗?
def lineParams(line, length):
(dist, angl) = line
a = math.cos(angl)
b = math.sin(angl)
x0 = a * dist
y0 = b * dist
pt1 = (int(x0 - length * b), int(y0 + length * a))
pt2 = (int(x0 + length * b), int(y0 - length * a))
return (pt1, pt2)
def lineWhiteness(line, image):
(pt1, pt2) = lineParams(line, len(image))
count = 0
(x1, y1) = pt1
(x2, y2) = pt2
stepx = (x2 - x1) / 100
stepy = (y2 - y1) / 100
for i in xrange(1, 100):
#print image[(x1 + i * stepx), (y1 + i * stepy)]
if(image[(x1 + (i * stepx)), (y1 + (i * stepy))].any()):
count = count + 1
return count
def foo(path, display):
edges = CannyEdge(path, False)
lines = cv2.HoughLines(edges, rho, theta, threshold)
image = cv2.imread(path)
lines = lines[0]
lines = sorted(lines, key=lambda l: lineWhiteness(l, image))
return lines
【问题讨论】:
-
您使用的是 Python 2 还是 Python 3?因为在 Python 3 中,我相信
/是 not 整数除法(或者如果你在 Python 2 程序中说from __future__ import division),所以你最终会尝试索引非整数位置。此外,另一个需要考虑的问题是长度小于 100 的行将具有stepx=0和stepy=0。另外,请确保pt1和pt2都在图像的范围内。 -
Python 2.7 并将 (istepx) 和 (istepy) 转换为 ints 会导致相同的错误。
标签: python opencv computer-vision