title: Dealing with contours and bounding rectangle in OpenCV 2.4 - python 2.7
body: <p>I am working with openCv and python and I am dealing with Structural Analysis and Shape Descriptors.
  I have found this blog: <a href="http://opencvpython.blogspot.it/2012/06/contours-2-brotherhood.html" rel="noreferrer">http://opencvpython.blogspot.it/2012/06/contours-2-brotherhood.html</a>
  that's very helpful and I have tried with a black and white image to drawing a bounding rectangle and it works.
  But now from an image i extract, for example, the yellow color and on that i would like to draw a bounding rectangle. The problem is that the black and white image is not uniform it has some noise and like that the code doesn't recognize the whole shape.</p>
  
  <p><img src="https://i.stack.imgur.com/y2x8Q.png" alt="origianl image"></p>
  
  <p><img src="https://i.stack.imgur.com/D9DJb.jpg" alt="black and white image"></p>
  
  <p><img src="https://i.stack.imgur.com/iz56I.png" alt="final image"></p>
  
  <p>And this is the code:</p>
  
  <pre><code>import numpy as np
  import cv2
  
  im = cv2.imread('shot.bmp')
  hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
  COLOR_MIN = np.array([20, 80, 80],np.uint8)
  COLOR_MAX = np.array([40, 255, 255],np.uint8)
  frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)
  imgray = frame_threshed
  ret,thresh = cv2.threshold(frame_threshed,127,255,0)
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  cnt=contours[0]
  x,y,w,h = cv2.boundingRect(cnt)
  cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
  cv2.imshow("Show",im)
  cv2.waitKey()
  cv2.destroyAllWindows()
  </code></pre>