简体   繁体   中英

Parkingspot corners coordinates gather through OpenCV in Python 3.6

I want to make a parkingspots detector with OpenCV and Python and I got stucked at finding the coordinates of each parking spot.

So, my idea was to apply hughlines function to detect the lines and then track the corners and then apply a function which determine if the spot is empty or not.

Here's what I got until now:

Function to detect if the spot is empty:

def f(img,a,b,c,d):
sub_img=img[a:b, c:d]
edg= cv2.Canny(sub_img,100,150)
pix=cv2.countNonZero(edg)
if pix>1400:
    cv2.rectangle(img,(d,b),(c,a),(0,0,255),3)
else:cv2.rectangle(img,(d,b),(c,a),(0,255,),3)

The part where I'm tryin to detect the spots:

import cv2
import numpy as np
import csv
from matplotlib import pyplot as plt

img = cv2.imread('parking.jpg',1)

im_gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, im_bw)= cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY| cv2.THRESH_OTSU)
tresh=127
im_bw= cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('binary',im_bw)
edges = cv2.Canny(im_bw,300,350)
imagem = cv2.bitwise_not(im_bw)
height, width = im_bw.shape[:2]


contours=np.zeros((height, width,3),np.uint8)
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=0.1,maxLineGap=30)
for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.circle(im_bw,(x1,y1),3,(0,255,0),3)
    cv2.circle(im_bw,(x2,y2),3,(0,0,255),3)
    cv2.line(contours,(x1,y1),(x2,y2),255,2)


gray = cv2.cvtColor(contours,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
contours[dst>0.1*dst.max()]=[0,0,255]

# Printing corners coords
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
k=tuple(corners)
print(tuple(corners))

with open('csv.txt','w',newline='')as outf:
    csvw= csv.writer(outf)
    csvw.writerows(k)
corn=open('csv.txt')
cv2.line(contours,(corn[1],y1),(x2,y2),255,2)

cv2.imshow('contours',contours)
cv2.imshow('houghlines5.jpg',img)
# plt.imshow(contours, cmap = 'gray', interpolation = 'bicubic')
# plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
# plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

Original image

1

houghlines and harrisCornerDetection result

2

Is this the correct approach to detect open parking spots? If not, what would be better?

Something you could do to make the corner detector works is reduce the thickness of the line to one pixel after you found them. You could do this either directly or with a skeletization algorithm ( http://homepages.inf.ed.ac.uk/rbf/HIPR2/skeleton.htm ). If this does not work I would recommend you to use the AKAZE Detector to find those intersections. However, this might be a bit exaggerated for your aplication.

I hope this helps you further! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM