How to detect empty park space using morphologyEx and drawContours?
Asked Answered
L

1

7

I have the following image which has car and empty parking slots capture by drone. I would like detect the empty park space and draw a box as such it look like the expected image.

Here is my code:

import cv2
import numpy as np 
from matplotlib import pyplot as plt
%matplotlib inline 

img = cv2.imread('parking.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)        
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)     
blurred = cv2.bilateralFilter(gray,21,41,41)         
edged = cv2.Canny(blurred,400,600)        
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 40)    

mask = cv2.bitwise_not(edged)    
thresh = cv2.bitwise_and(thresh,thresh,mask=mask)     
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))    
thresh = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)    
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 

output = img.copy()     
cv2.drawContours(output, contours, -1, (0,255,0), 1)    
plt.imshow(output)

The problem is it detect all the existing rectangle. How can i reduce the contours to detect only vehicle?

Input Image:

enter image description here

Output Image:

enter image description here

What i trying to obtain, Expected Output:

enter image description here

Linnell answered 27/8, 2019 at 10:33 Comment(1)
I don't think there is a robust method to solving this problem by relying on simple morphological operations and contour detection. A better approach is to use deep learning like training a HOG object detector or a CNN. Take a look at this article which uses a Mask R-CNN to solve this problemCounterclockwise
T
1

I don't think this approach will work. There are two ways of approaching this:

  1. Train an object detection neural network to find the empty spaces. This will result in the most accurate predictions but is a bit hard to implement and requires an annotated dataset.

  2. Use the dominant background colour (which is reasonably uniform) to get "empty" space, then process the blobs to determine empty areas between cars or inside the frames you detected in your code

You can improve the quality of your detection by adding corner detection to find the corners of the parking spaces. Some will be probably obscured by cars but you will have enough datapoints to find the sequence to properly segment the space.

Unfortunately all of these requires more coding than possible to include in an answer but hopefully it will give you some direction.

Tade answered 11/10, 2020 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.