title: OpenCV- Detecting objects excluding shadows
body: <p>I'm developing a way to detect cars from an aerial view image. I'm using scikit package to calculate the difference of an empty parking lot image and a parking lot with cars to detect the foreign objects. Then I draw the minimum area rectangle around cars.</p>
  
  <p>This works well when there are no shadows.</p>
  
  <p>Empty parking lot image (Please ignore the maroon color car)</p>
  
  <p><a href="https://i.stack.imgur.com/DSu68.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DSu68.jpg" alt="Empty parking lot"></a></p>
  
  <p>Without shadows</p>
  
  <p><a href="https://i.stack.imgur.com/2JnIj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2JnIj.jpg" alt="Without shadows original"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MAHLR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MAHLR.png" alt="Without shadows threshold"></a></p>
  
  <p><a href="https://i.stack.imgur.com/IzE2M.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IzE2M.jpg" alt="Without shadows result "></a></p>
  
  <p>With shadows (Problem)</p>
  
  <p>When there are car shadows, they are also in the minimum area rectangle. How can I exclude shadows from the rectangle?</p>
  
  <p><a href="https://i.stack.imgur.com/XYyBC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XYyBC.jpg" alt="With shadows original"></a></p>
  
  <p><a href="https://i.stack.imgur.com/4Z3pE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Z3pE.png" alt="With shadows threshold"></a></p>
  
  <p><a href="https://i.stack.imgur.com/qij4M.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qij4M.jpg" alt="With shadows result"></a></p>
  
  <p>Here is my source</p>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  from skimage.measure import compare_ssim
  import imutils
  from cv2 import cv2
  
  # construct the argument parse and parse the arguments
  # load the two input images
  imageA = cv2.imread('empty-lot.png')
  imageB = cv2.imread('two-car-lot-shadow.png')
  
  # convert the images to grayscale
  grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
  grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
  
  # compute the Structural Similarity Index (SSIM) between the two
  # images, ensuring that the difference image is returned
  (score, diff) = compare_ssim(grayA, grayB, full=True, gaussian_weights=True, sigma=4)
  diff = (diff * 255).astype("uint8")
  print("SSIM: {}".format(score))
  
  # threshold the difference image, followed by finding contours to
  # obtain the regions of the two input images that differ
  thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
  cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if imutils.is_cv2() else cnts[1]
  
  # loop over the contours
  for c in cnts:
      # compute the min area rect of the contour and area
      rect = cv2.minAreaRect(c)
      box = cv2.boxPoints(rect)
      area = cv2.contourArea(c)
      # remove small contour areas
      if area &lt; 10000: continue
      # convert all coordinates floating point values to int
      box = np.int0(box)
      # draw a green rectangle
      cv2.drawContours(imageB, [box], 0, (0, 255, 0), 2)
  
  # show the output images
  cv2.imshow("Modified", imageB)
  cv2.imshow("Thresh", thresh)
  k = cv2.waitKey() &amp; 0xFF
  if k == 27:
      exit()
  </code></pre>