0

I am using opencv Houghlinesp to detect lines in a parking lot. Here is the source image

When I did a hough transform-p to detect the lines, I got final image like this.

It did detect empty spaces. Any ideas how these noisy lines on top of the cars can be removed? Or any direction on alternative algorithms or approaches highly appreciated.

img = cv.imread('Parking-Lot.jpg')
threshold=100
minLineLength = 60
rho=2
maxLineGap=20
theta = np.pi/180
edges = cv.Canny(img, 100, 200)
lines = cv.HoughLinesP(edges, rho, theta, threshold, np.array([]), minLineLength =minLineLength , maxLineGap=maxLineGap)
 for i in range(len(lines)):
    for line in lines[i]:
        cv.line(img, (line[0],line[1]), (line[2],line[3]), (0,255,0), 2)
cv2.imwrite("lines.jpg", img)
Balaji
  • 201
  • 2
  • 12

1 Answers1

0

You can remove most of the noise by thresholding your image before you apply the edge detection. That way you will remove (most of) the cars and keep your white space lines you are interested in:

import cv2
import numpy as np

img = cv2.imread('Parking-Lot.jpg')
threshold=100
minLineLength = 60
rho=2
maxLineGap=20
theta = np.pi/180

# here you convert the image to grayscale and then threshhold to binary
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,180,255,cv2.THRESH_BINARY)

# continue with the threshholded image instead
edges = cv2.Canny(thresh, 100, 200)
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]), minLineLength =minLineLength , maxLineGap=maxLineGap)
for i in range(len(lines)):
  for line in lines[i]:
     cv2.line(img, (line[0],line[1]), (line[2],line[3]), (0,255,0), 2)
cv2.imwrite("lines.jpg", img)

This will yield you a much cleaner result:

enter image description here

Feel free to experiment with the threshold parameters; you will need to find a threshold that excludes most of the cars while keeping all the lines that you want to detect.

Osi
  • 406
  • 3
  • 13
T A
  • 1,677
  • 4
  • 21
  • 29
  • Thanks a lot! The pic looks improved now. I will work on it and let you know how it goes. – Balaji May 13 '19 at 15:09
  • Hi,I have one more question. The white colored cars are causing noise. For e.g. the SUV close to top left corner. Is it possible to use image pre-processing techniques like erosion? – Balaji May 13 '19 at 16:13
  • But it is still not enough to achieve the job, I think finding the lines is not the way to implement the functionality. Becover the cars which far from camera cover areas and the lines which make it hard to correctly detect the empty place. Also as @Balaji mentioned the white color causing noise is also the case in which finding lines will fail. – Bahramdun Adil May 13 '19 at 16:16
  • @Balaji The white colored cars will unfortunately always cause some noise and removing them is not trivial. Erosion probably won't work either, because you will remove the lines together with the cars in the binary image, since they are a lot smaller. If you want to keep it simple, your best option will be tweaking the parameters to get the best result possible. – T A May 14 '19 at 06:39
  • Otherwise you would need to detect & remove the cars in a first step by a template matching or classification approach, but that wont be an easy task. – T A May 14 '19 at 06:40
  • @BahramdunAdil Unfortunately there is no easy way around it. Having noise with similar or the same properties as the object you want to detect is always difficult and cant be removed without a lot of effort, which would go way beyond the question and maybe even what OP is willing to do. – T A May 14 '19 at 06:43