title: Python - Finding contours of different colors on an image
body: <p>I have the following image: 
  <a href="https://i.stack.imgur.com/HZ3Te.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HZ3Te.jpg" alt="enter image description here"></a></p>
  
  <p>I used the following code to outline all the round blobs in this image using this code: </p>
  
  <pre><code>import numpy as np
  import cv2
  
  im = cv2.imread('im.jpg')
  
  imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  ret,thresh = cv2.threshold(imgray,200,255,0)
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  cv2.drawContours(im,contours,-1,(0,0,255),1)
  
  #(B,G,R)
  
  cv2.imshow('image',im)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>And it produces this image:
  <a href="https://i.stack.imgur.com/agpkF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/agpkF.jpg" alt="enter image description here"></a></p>
  
  <p>Which is great for the first step. But I'm having a really hard time drawing a different colored outline for the blue colored blobs. I tried using multiple contours: </p>
  
  <pre><code>import numpy as np
  import cv2
  
  im = cv2.imread('im.jpg')
  
  imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  ret,thresh = cv2.threshold(imgray,200,255,0)
  ret, thresh2 = cv2.threshold(imgray,130,255,0)
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  contours2, hierarchy2 = cv2.findContours(thresh2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  
  cv2.drawContours(im,contours,-1,(0,0,255),1)
  cv2.drawContours(im,contours2,-1,(0,255,0),1)
  
  #(B,G,R)
  
  cv2.imshow('image',im)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>and the image comes out like this: 
  <a href="https://i.stack.imgur.com/yFlDn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yFlDn.jpg" alt="enter image description here"></a></p>
  
  <p>The first problem with this approach is the fact that it doesn't accurately only outline the blue blobs. Moreover, the sensitivity rating in the <code>threshold</code> function would have to be modified for each image depending on lighting, etc. Is there a smoother way to do this?</p>