python - How to detect lines in opencv? -
i trying detect lines in parking shown below
what hope clear lines , (x,y) position in crossed line, result not promising
i guess due 2 main reasons
some lines broken or missing human eyes can identify them. (even houghline can connect missing lines since houghline connect unnecessary lines together, 'd rather manually)
there repeated lines
the general pipeline work shown below
1. select specific colors (white or yellow)
import cv2 import numpy np import matplotlib matplotlib.pyplot import imshow matplotlib import pyplot plt # white color mask img = cv2.imread(filein) #converted = convert_hls(img) image = cv2.cvtcolor(img,cv2.color_bgr2hls) lower = np.uint8([0, 200, 0]) upper = np.uint8([255, 255, 255]) white_mask = cv2.inrange(image, lower, upper) # yellow color mask lower = np.uint8([10, 0, 100]) upper = np.uint8([40, 255, 255]) yellow_mask = cv2.inrange(image, lower, upper) # combine mask mask = cv2.bitwise_or(white_mask, yellow_mask) result = img.copy() cv2.imshow("mask",mask)
2. repeat dilation , erosion until image can not changed (reference )
height,width = mask.shape skel = np.zeros([height,width],dtype=np.uint8) #[height,width,3] kernel = cv2.getstructuringelement(cv2.morph_cross, (3,3)) temp_nonzero = np.count_nonzero(mask) while(np.count_nonzero(mask) != 0 ): eroded = cv2.erode(mask,kernel) cv2.imshow("eroded",eroded) temp = cv2.dilate(eroded,kernel) cv2.imshow("dilate",temp) temp = cv2.subtract(mask,temp) skel = cv2.bitwise_or(skel,temp) mask = eroded.copy() cv2.imshow("skel",skel) #cv2.waitkey(0)
3. apply canny filter lines , use houghlinesp lines
edges = cv2.canny(skel, 50, 150) cv2.imshow("edges",edges) lines = cv2.houghlinesp(edges,1,np.pi/180,40,minlinelength=30,maxlinegap=30) = 0 x1,y1,x2,y2 in lines[0]: i+=1 cv2.line(result,(x1,y1),(x2,y2),(255,0,0),1) print cv2.imshow("res",result) cv2.waitkey(0)
i wonder after first step of selecting color, lines broken , noises , think in step should make broken line complete, less noisy line, , try apply canny , hough lines, ideas ?
here pipeline, maybe can give help.
first, gray image , process gaussianblur.
img = cv2.imread('src.png') gray = cv2.cvtcolor(img,cv2.color_bgr2gray) kernel_size = 5 blur_gray = cv2.gaussianblur(gray,(kernel_size, kernel_size),0)
second, process edge detection use canny.
low_threshold = 50 high_threshold = 150 edges = cv2.canny(blur_gray, low_threshold, high_threshold)
then, use houghlinesp lines. can adjust parameters better performance.
rho = 1 # distance resolution in pixels of hough grid theta = np.pi / 180 # angular resolution in radians of hough grid threshold = 15 # minimum number of votes (intersections in hough grid cell) min_line_length = 50 # minimum number of pixels making line max_line_gap = 20 # maximum gap in pixels between connectable line segments line_image = np.copy(img) * 0 # creating blank draw lines on # run hough on edge detected image # output "lines" array containing endpoints of detected line segments lines = cv2.houghlinesp(edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) line in lines: x1,y1,x2,y2 in line: cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
finally, draw lines on srcimage.
# draw lines on image lines_edges = cv2.addweighted(img, 0.8, line_image, 1, 0)
here final performance.
final image:
Comments
Post a Comment