title: Python OpenCV - filtering out contours, that are not nearby
body: <p>I'm trying to get my program to show me only the centre of the road, by filtering out the rest.
  I have started by finding width and length for every rectangle in the centre of the road and filtering out the rest.</p>
  
  <p>Problem:
  Some rectangles that are not in the centre of the road are still visible, as they are within the width and length of the other rectangles which are in the centre of the road.</p>
  
  <p>My idea:
  Running a for-loop for every rectangle shown which will:
  Using a small radius on one end of the rectangle to see if another rectangle is inside that radius, if so, show that rectangle,
  -> if not:
  use the other end of the rectangle with the same radius and see if another rectangle is inside that radius, if so, show that rectangle,
  -> if both of these statements are false: do not show the rectangle, which has been used to find another rectangle nearby.</p>
  
  <p>My idea is to filter out every other rectangle that has been found, which is not in the centre of the road, as every rectangle in the centre of the road is close to one another (has at least 1 rectangle nearby).
  Basically every rectangle that is not within the centre of the road has a bigger distance to another rectangle, which is why I think i can work with radius or distance here, but that is just my idea.
  Now I have run out of ideas, I would need some help to make it work, as I am not only new to OpenCV but to Python as well. 
  I want this code to work not only for this road, but other roads where the image looks the same.</p>
  
  <p>Original picture:
  <a href="https://i.stack.imgur.com/0zy3y.png" rel="nofollow noreferrer">This is the original picture</a>
  Picture with the rectangles left:
  <a href="https://i.stack.imgur.com/d3sQp.png" rel="nofollow noreferrer">This is the picture with the rectangles left</a></p>
  
  <p>EDIT!: I was told I can work with scipy.spatial.KDTree to find neighbors.</p>
  
  <p>this is my code:</p>
  
  <pre><code>import cv2
  import numpy as np
  
  # Read image
  
  image = cv2.imread('Resources/StreckeUni.png')
  
  # Grayscale
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
  # show image
  cv2.imshow('Gray', gray)
  cv2.waitKey(0)
  
  # Adjust Contrass and brightness
  alpha = 50  # Kontrast (0-100)
  beta = 0  # Helligkeit (0-100)
  adjusted = cv2.convertScaleAbs(gray, alpha=alpha, beta=beta)
  
  # Bild anzeigen
  cv2.imshow('Contrast', adjusted)
  cv2.waitKey(0)
  
  #  find Canny Edges
  edged = cv2.Canny(adjusted, 30, 200)
  
  # Use Blur
  blur = cv2.GaussianBlur(edged, (3, 3), 0)
  
  # find Conturs
  contours, hierarchy = cv2.findContours(blur, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  
  # show img
  cv2.imshow('Canny Edges', blur)
  cv2.waitKey(0)
  
  # show number of contours
  print("Anzahl der Konturen = " + str(len(contours)))
  
  # draw rectangles
  for cnt in contours:
      rect = cv2.minAreaRect(cnt)
      box = cv2.boxPoints(rect)
      box = np.int0(box)
  
      if rect[1][0] &gt; rect[1][1]:
          laenge = rect[1][0]
          breite = rect[1][1]
      else:
          laenge = rect[1][1]
          breite = rect[1][0]
  
      if 13.9 &lt; laenge &lt; 25.1 and 3.2 &lt; breite &lt; 7.7:
          cv2.drawContours(image, [box], -1, (0, 255, 0), 1)
  
  # show final pic
  cv2.imshow('Rectangles', image)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  
  </code></pre>