所以我试着编写一个函数,让我们称之为foo,它采用二进制图像的路径,沿着它获取Hough线,然后返回按照霍夫沿着多少个白色像素排序的线条线。这是我到目前为止的代码,但它在" if(图像[(x1 +(i stepx)),(y1 +(i stepy))]。any())崩溃:"带有无效索引的行。你们看到我能做些什么来解决这个bug或者知道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
答案 0 :(得分:3)
我最后通过使用OpenCV的行迭代器来解决它,如下所示,我正在尝试重写我的行参数函数以便更好。
def lineWhiteness(line, image):
(pt1, pt2) = lineParams(line, len(image))
count = 0
li = cv.InitLineIterator(cv.fromarray(image), pt1, pt2)
for (r, g, b) in li:
if (r or g or b):
count += 1
return count