'how to detect lane lines of a road image in python with opencv
hey so I am trying to detect lane lines that are colored white, yellow and red from a image and mark them as well as count the number of lane lines. I have tried several steps like using different masks but they are not working for every image. Either there are too many lane lines detected or few of the lane lines are missing.
import os
import re
import cv2
import numpy as np
from tqdm import tqdm_notebook
import matplotlib.pyplot as plt
col_images = list()
path = r'C:\Users\nancy\Downloads\lane\lane1.jpeg'
image = cv2.imread(path)
plt.imshow(image)
plt.show()
col_images.append(image)
idx = 0
stencil = np.zeros_like(col_images[idx][:,:,0])
he, we, ce = image.shape
polygon = np.array([[0,0], [0,he], [we,he]])
polygon1 = np.array([[0,0], [we,0], [we,he]])
cv2.fillConvexPoly(stencil, polygon, 1)
cv2.fillConvexPoly(stencil, polygon1, 1)
img = cv2.bitwise_and(col_images[idx][:,:,0], col_images[idx][:,:,0], mask=stencil)
ret, thresh = cv2.threshold(img, 100, 150, cv2.THRESH_BINARY)
lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)
dmy = col_images[idx][:,:,0].copy()
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)
cnt = 0
for img in tqdm_notebook(col_images):
masked = cv2.bitwise_and(img[:,:,0], img[:,:,0], mask=stencil)
ret, thresh = cv2.threshold(masked, 130, 145, cv2.THRESH_BINARY)
lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)
dmy = img.copy()
try:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)
cnt+= 1
except:
print("invalid")
plt.imshow(dmy)
plt.show()
print(cnt)

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
