gpt4 book ai didi

python - 在OpenCV Python中画线时出错

转载 作者:行者123 更新时间:2023-12-02 17:52:22 25 4
gpt4 key购买 nike

所以我想做的是使用霍夫变换在图像上找到线,然后将线绘制到图像中。

但我收到一条错误消息:

Traceback (most recent call last): 
File "houghLines.py", line 31, in <module>
main() File "houghLines.py", line 13, in main
a = hough(image)
File "houghLines.py", line 29, in hough
(0,0,255))
TypeError: only length-1 arrays can be converted to Python scalars

这是我的代码:
import cv2
import os
import math
def main():

directory = os.path.dirname(__file__)
picLoc = os.path.join(directory, "../video-image/1m50s.png")

image = cv2.imread(picLoc)
print "sending image to houghLines.py"

a = hough(image)
cv2.imshow("", a)
cv2.waitKey(0)
cv2.destroyAllChildren()

def hough(image):
canny = cv2.Canny(image, 50, 200)
color_image = cv2.cvtColor(canny, cv2.COLOR_GRAY2BGR)
houghLines = cv2.HoughLinesP(canny, 1, math.pi/180, 50)
for x in range(len(houghLines)):
print x
pt1 = (houghLines[x][0], houghLines[x][1])
pt2 = (houghLines[x][2], houghLines[x][3])
cv2.line(color_image, pt1, pt2, (0,0,255), 3)
return color_image

main()

最佳答案

发生此问题的原因是cv2.line()期望pt1pt2为仅由单个元素组成的元组,每个xy坐标。如果在示例中使用print pt1,您将很快发现情况并非如此。
cv2.HoughLinesP()返回仅包含一个元素的numpy数组。此元素包含四个点的列表,它们是行的起点和终点。知道这一点,正确的实现如下:

    for line in houghLines[0]:
pt1 = tuple(line[:2])
print pt1
pt2 = tuple(line[-2:])
cv2.line(color_image, pt1, pt2, (0,0,255), 3)

关于python - 在OpenCV Python中画线时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17573925/

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