所以我要做的是使用霍夫变换来查找图像上的线条,然后将线条绘制到图像中。
但我收到的错误是:
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()
答案 0 :(得分:1)
出现此问题是因为cv2.line()
期望pt1
和pt2
是由每个x
和y
坐标仅包含一个元素的元组。如果您在示例中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)