python - OpenCV 车道检测和着色
问题描述
我有一张道路图片https://imgur.com/a/whT90Yp并且我想检测车道并为两侧着色,就像这样https://i.stack.imgur.com/1NuD8.jpg,我做了一个代码在openCV中,但它不起作用,我不知道为什么:(
这是代码:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def lanesDetection(img):
im = cv.imread("highway4.jpg")
im = cv.cvtColor(im, cv.COLOR_BGR2RGB)
# print(img.shape)
height = img.shape[0]
width = img.shape[1]
region_of_interest_vertices = [
(200, height), (width/2, height/1.37), (width-300, height)
]
gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
edge = cv.Canny(gray_img, 50, 100, apertureSize=3)
cropped_image = region_of_interest(
edge, np.array([region_of_interest_vertices], np.int32))
lines = cv.HoughLinesP(cropped_image, rho=2, theta=np.pi/180,
threshold=50, lines=np.array([]), minLineLength=10, maxLineGap=30)
image_with_lines = draw_lines(img, lines)
# plt.imshow(image_with_lines)
# plt.show()
return image_with_lines
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
# channel_count = img.shape[2]
match_mask_color = (255)
cv.fillPoly(mask, vertices, match_mask_color)
masked_image = cv.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines):
img = np.copy(img)
blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
for line in lines:
for x1, y1, x2, y2 in line:
cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0)
return img
cap.release()
cv.destroyAllWindows()
你能帮我修一下吗?
解决方案
我拿走了你的代码并让它运行起来:我做的一些事情是:
- 修复了文件中空格和制表符的混合使用
- 更改函数以接受文件名(您已将其硬编码)
- 在末尾添加了 7 行(您的代码没有调用函数或显示结果图像)
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def lanesDetection(imgname):
img = cv.imread(imgname)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
# print(img.shape)
height = img.shape[0]
width = img.shape[1]
region_of_interest_vertices = [(200, height), (width/2, height/1.37), (width-300, height)]
gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
edge = cv.Canny(gray_img, 50, 100, apertureSize=3)
cropped_image = region_of_interest(edge, np.array([region_of_interest_vertices], np.int32))
lines = cv.HoughLinesP(cropped_image, rho=2, theta=np.pi/180,threshold=50, lines=np.array([]), minLineLength=10, maxLineGap=30)
image_with_lines = draw_lines(img, lines)
# plt.imshow(image_with_lines)
# plt.show()
return image_with_lines
def blah(imgname):
blahb = cv.imread(imgname)
return blahb
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
# channel_count = img.shape[2]
match_mask_color = (255)
cv.fillPoly(mask, vertices, match_mask_color)
masked_image = cv.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines):
img = np.copy(img)
blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
for line in lines:
for x1, y1, x2, y2 in line:
cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0)
return img
#cap.release()
#cv.destroyAllWindows()
pimage = lanesDetection("highway4.jpg")
while 1:
cv.imshow("Image",pimage)
key = cv.waitKey(0) & 0xFF
if key == ord("q"):
break
推荐阅读
- java - 数据库中有空对象的Sqlite数据库
- javascript - 正则表达式可打印的 ascii 加上换行符和回车符
- java - 应用程序运行良好,但在测试时,未创建 bean 存储库
- spring-boot - 带有传出请求代理的 Spring Boot (GCP)
- php - 如何在共享主机中升级 ioncube
- reactjs - 使用带有 SharePoint 布尔或字符串列的流利 ui 复选框时遇到问题
- r - 日期选择逻辑
- python-3.x - `Content-Length` 和 `Transfer-Encoding` 有什么区别?urllib.request.urlopen
- python - 一个键是否可以在字典中有多个名称?
- django - 如何在不通过表的情况下保存 django manytomany 字段