title: Bounding box on objects based on color python
body: <p>I try to draw a  bounding box on every object in this picture, i wrote this code from <a href="https://docs.opencv.org/3.4.0/dd/d49/tutorial_py_contour_features.html" rel="noreferrer">documentation</a></p>
  
  <pre><code>import cv2 as cv2
  import os
  import numpy as np
  
  
  img = cv2.imread('1 (2).png')
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
  ret,thresh = cv2.threshold(img,127,255,0)
  im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
  for item in range(len(contours)):
      cnt = contours[item]
      if len(cnt)&gt;20:
          print(len(cnt))
          M = cv2.moments(cnt)
          cx = int(M['m10']/M['m00'])
          cy = int(M['m01']/M['m00'])
          x,y,w,h = cv2.boundingRect(cnt)
          cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
          cv2.imshow('image',img)
          cv2.waitKey(0)
          cv2.destroyAllWindows()
  </code></pre>
  
  <p>the result is only one object,<br>
   <a href="https://i.stack.imgur.com/LUH5i.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LUH5i.png" alt="pic1"></a></p>
  
  <p>when i change the value 127 in this line to 200 in this line <code>ret,thresh = cv2.threshold(img,127,255,0)</code> i got different object.
  <a href="https://i.stack.imgur.com/AP1Hi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AP1Hi.png" alt="pic2"></a></p>
  
  <p>here's the original image<br>
   <a href="https://i.stack.imgur.com/grtKa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/grtKa.png" alt="original picture"></a></p>
  
  <p>The question is how can i detect all objects once?</p>