title: Parkingspot corners coordinates gather through OpenCV in Python 3.6
body: <p>I want to make a parkingspots detector with OpenCV and Python and I got stucked at finding the coordinates of each parking spot.</p>
  
  <p>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.</p>
  
  <p>Here's what I got until now: </p>
  
  <p>Function to detect if the spot is empty:</p>
  
  <pre><code>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&gt;1400:
      cv2.rectangle(img,(d,b),(c,a),(0,0,255),3)
  else:cv2.rectangle(img,(d,b),(c,a),(0,255,),3)
  </code></pre>
  
  <p>The part where I'm tryin to detect the spots:</p>
  
  <pre><code>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&gt;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()
  </code></pre>
  
  <p>Original image</p>
  
  <p><a href="https://i.stack.imgur.com/bkG9A.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bkG9A.jpg" alt="1"></a></p>
  
  <p>houghlines and harrisCornerDetection result</p>
  
  <p><a href="https://i.stack.imgur.com/l0HP1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l0HP1.jpg" alt="2"></a></p>
  
  <p>Is this the correct approach to detect open parking spots?  If not, what would be better?</p>