python - 无法检测到车道中间
问题描述
我能够检测到车道,但我还必须检测到车道的中间/中心,即两条线的中心/中间。我尝试了 x1 和 x2 的中间,但它没有用。我想要第三条线,即中间线。我还附上了截图
def houghLines(cropped_canny):
return cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100,
np.array([]), minLineLength=70, maxLineGap=5)
def addWeighted(frame, line_image):
return cv2.addWeighted(frame, 0.8, line_image, 1, 1)
def display_lines(img,lines):
line_image = np.zeros_like(img)
if lines is not None:
print(len(lines))
for line in lines:
for x1, y1, x2, y2 in line:
#print(len(lines))
cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),5)
return line_image
cap = cv2.VideoCapture("videoplayback (online-video-cutter.com) (1).mp4")
while(cap.isOpened()):
_, frame = cap.read()
canny_image = canny(frame)
cropped_canny = region_of_interest(canny_image)
#cv2.imshow("cropped_canny",cropped_canny)
lines = houghLines(cropped_canny)
#averaged_lines = average_slope_intercept(frame, lines)
line_image = display_lines(frame, lines)
combo_image = addWeighted(frame, line_image)
cv2.imshow("result", combo_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
解决方案
我不知道,如果你能用opencv以某种方式检测到它,但你可以通过平均每条线的(x1,y1)和(x2,y2)坐标来计算它。
例如:
def display_midline(img, lines):
line_image = np.zero_like(img)
x1_avg, y1_avg = (0, 0)
x2_avg, y2_avg = (0, 0)
if lines is not None:
for line in lines:
for x1, y1, x2, y2 in line:
x1_avg += x1
y1_avg += y1
x2_avg += x2
y2_avg += y2
x1_avg = int(x1_avg / len(lines))
y1_avg = int(y1_avg / len(lines))
x2_avg = int(x2_avg / len(lines))
y2_avg = int(y2_avg / len(lines))
cv2.line(line_image, (x1_avg, y1_avg), (x2_avg, y2_avg), (0, 0, 255), 5)
return line_image
推荐阅读
- javascript - For 循环初始化程序捕获
- c++ - 使用cmake安装目标目录中存在的dll以安装目录
- mysql - “USING”语句MYSQL缺少右括号错误
- wordpress - 无法使用类别和子类别设置 WordPress 永久链接
- c++ - 带头节点的循环双向链表
- python - Python Web 抓取“无”
- python - 我可以在 python 中隐藏代码的某些部分吗?
- pdf - pe:documentViewer 不显示 PDF 中的数字签名
- json - 显示空值的字段值
- java - Spring boot java google app engine添加kotlin报错