gpt4 book ai didi

python - 计算 OpenCV 中一条线上的白色像素数

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:52 25 4
gpt4 key购买 nike

所以我正在尝试编写一个函数,我们称它为 foo,它采用二进制图像的路径,沿着它获取 Hough 线,然后返回按照 Hough 线上有多少白色像素排序的线。这是我到目前为止的代码,但它在“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

最佳答案

我最终通过使用 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

关于python - 计算 OpenCV 中一条线上的白色像素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com