title: How to mask the largest object of one kind in opencv python
body: <p><strong>Want to fetch only the largest Object of each kind.</strong>
  Here, the aim is to visualize only RGB objects one of each kind. But it is fetching objects too. I found the best way of detecting the largest object by biggest contour but other items of similar kind are also show here i want to ignore them. </p>
  
  <pre><code>import cv2
  import numpy as np
  
  # Webcamera no 0 is used to capture the frames
  cap = cv2.VideoCapture(0)
  #print(cv2.__version__)
  # This drives the program into an infinite loop.
  while(1):
      # Captures the live stream frame-by-frame
      _, frame = cap.read()
      blurred_frame=cv2.GaussianBlur(frame,(5,5),100)
      # Converts images from BGR to HSV
      hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)
      #print(hsv)
      lower_red = np.array([0,50,50])
      upper_red = np.array([10,255,255])
      lower_green = np.array([65,60,60])
      upper_green = np.array([80,255,255])
      lower_blue = np.array([100,150,0])
      upper_blue = np.array([140,255,255])
  
      # Here we are defining range of bluecolor in HSV
      # This creates a mask of blue coloured
      # objects found in the frame.
      mask = cv2.inRange(hsv, lower_red, upper_red)
      mask = cv2.erode(mask, None, iterations=3)
      mask = cv2.dilate(mask, None, iterations=5)
      mask1 = cv2.inRange(hsv, lower_green, upper_green)
      mask1 = cv2.erode(mask1, None, iterations=3)
      mask1 = cv2.dilate(mask1, None, iterations=5)
      mask2 = cv2.inRange(hsv, lower_blue, upper_blue)
      mask2 = cv2.erode(mask2, None, iterations=3)
      mask2 = cv2.dilate(mask2, None, iterations=5)
      #ret,thresh = cv2.threshold(mask, 0, 255, 0)
      contours, hierarchy=cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      contours1, hierarchy = cv2.findContours(mask1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      contours2, hierarchy = cv2.findContours(mask2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      #print(contours)
      mask_rg=cv2.bitwise_or(mask,mask1)
      mask_res=cv2.bitwise_or(mask2,mask_rg)
      res = cv2.bitwise_and(frame,frame, mask= mask_res)
      #cv2.drawContours(frame, contours, -1, (0,0,255), 3)
      if len(contours)!=0:
          contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
          biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
          for c in contours:
              area=cv2.contourArea(c)
              if area&gt;50:
                  cv2.drawContours(res, biggest_contour,-5, (0,0,255), 10)
      if len(contours1)!=0:
          contour_sizes1 = [(cv2.contourArea(contour), contour) for contour in contours1]
          biggest_contour1 = max(contour_sizes1, key=lambda x: x[0])[1]
          for c in contours1:
              area=cv2.contourArea(c)
              if area&gt;50:
                  cv2.drawContours(res, biggest_contour1,-5, (0,255,0), 10)
      if len(contours2)!=0:
          contour_sizes2 = [(cv2.contourArea(contour), contour) for contour in contours2]
          biggest_contour2 = max(contour_sizes2, key=lambda x: x[0])[1]
          for c in contours1:
              area=cv2.contourArea(c)
              if area&gt;50:
                  cv2.drawContours(res, biggest_contour2,-5, (255,0,0), 10)
          # The bitwise and of the frame and mask is done so
          # that only the blue coloured objects are highlighted
          # and stored in res
  
      cv2.imshow('frame',frame)
      #cv2.imshow('mask',mask)
      cv2.imshow('res',res)
  
      # This displays the frame, mask
      # and res which we created in 3 separate windows.
      k = cv2.waitKey(1) &amp; 0xFF
      if k == 27:
          break
  
  # Destroys all of the HighGUI windows.
  cv2.destroyAllWindows()
  
  # release the captured frame
  cap.release()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FXKE0.png" rel="nofollow noreferrer">enter image description here</a>
  I don't want 2 blue color objects only one should be seen which is detected by contours.
  Please help me out.</p>