title: How to find contours around these objects in the colored images
body: <p><a href="https://i.stack.imgur.com/ts5XI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ts5XI.jpg" alt="enter image description here" /></a>I have a problem where I need to find the contours around colored images. Currently I am able to get good results for some of the images but for the images with shadows I am not able to get good predictions. The algorithm also sometimes misses out some of the objects in the image. I need help with a robust algorithm which can help detect the contours in a correct way for varied images.<a href="https://i.stack.imgur.com/kaeSm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kaeSm.jpg" alt="enter image description here" /></a></p>
  <p>This is another example -</p>
  <p><a href="https://i.stack.imgur.com/TFj7Q.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TFj7Q.jpg" alt="enter image description here" /></a></p>
  <pre><code>import cv2
  import numpy as np
  import numpy as np
  import cv2
  
  from utils import getMaxContour, getFilteredLabelIndex
  
  def get_image_dimensions(img):
      height, width, channels = img.shape
      return height,width
  
  def create_blank_white_image(height, width):
      temp_img = np.zeros((width,height,3), np.uint8) 
      temp_img.fill(255)
      return temp_img
  
  # Read image
  inputImage = cv2.imread(&quot;new_test/images/5.png&quot;)
  inputImage = cv2.resize(inputImage, (800, 800))
  h,w,chn = inputImage.shape
  ratio = inputImage.shape[0] / 800.0
  
  # Create deep copy for results:
  inputImageCopy = inputImage.copy()
  
  width, height = get_image_dimensions(inputImage)
  temp_img = create_blank_white_image(height, width) 
  
  # Convert to float and divide by 255:
  imgFloat = inputImage.astype(np.float) / 255.
  
  # Calculate channel K:
  kChannel = 1 - np.max(imgFloat, axis=2)
  
  # Convert back to uint 8:
  kChannel = (255*kChannel).astype(np.uint8)
  
  _, binaryImage = cv2.threshold(kChannel, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  
  # Use a little bit of morphology to clean the mask:
  # Set kernel (structuring element) size:
  kernelSize = 5
  # Set morph operation iterations:
  opIterations = 2
  # Get the structuring element:
  morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
  # Perform closing:
  binaryImage = cv2.morphologyEx(binaryImage, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)
  cv2.imshow(&quot;binaryImage&quot;, binaryImage)
  
  h_threshold,w_threshold = binaryImage.shape
  area = h_threshold*w_threshold
  
  (numLabels, labels, stats, centroids) = cv2.connectedComponentsWithStats(
      binaryImage, 4, cv2.CV_32S)
  print(&quot;No of contours&quot;,len(stats))
  filteredIdx = getFilteredLabelIndex(stats, areaHighLimit=area/2, heightUpperLimit=h_threshold*0.9, widthUpperLimit=w_threshold*0.9) # here we have to ensure that the height and the weight of the rectangle is neither to big or too small.
  
  for i in filteredIdx:
    
      componentMask = (labels == i).astype(&quot;uint8&quot;) * 255
      cv2.waitKey(0)
  
      cv2.imshow(&quot;componentMask&quot;, componentMask)
  
      opIterations = 3
     
      kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
      #componentMask = cv2.dilate(componentMask, kernel, iterations=3)
      componentMask = cv2.morphologyEx(componentMask, cv2.MORPH_CLOSE, kernel, None, None, opIterations, cv2.BORDER_REFLECT101)
  
      cv2.imshow(&quot;componentMask&quot;, componentMask)
  
      cntrs = getMaxContour(componentMask)
      cv2.drawContours(inputImage, [cntrs], -1, (255, 0, 255), 4)
      cv2.imshow(&quot;contour&quot;, inputImage)     
  
      line_width = 4
      if int(temp_img.shape[0]/250)&gt;line_width:
          line_width = int(temp_img.shape[0]/250)
      cv2.drawContours(temp_img, [cntrs], -1, (255,0,0),line_width )
      cv2.imshow(&quot;contour2&quot;, temp_img)
  
  cv2.imshow(&quot;original contour&quot;, temp_img)
  cv2.imwrite(&quot;new_test/my_algo_results/output/5.jpg&quot;, temp_img)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>