title: How to remove the object marked by the biggest contour from an image and save it as a separate image?
body: <p>Our objective is to classify plants in a field based on its leaves. We have trained our model on segmented images (these images only have a leaf and black background). But the live feed from the camera will look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/SrXcI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SrXcI.jpg" alt="enter image description here"></a></p>
  
  <p>So our idea is to find the biggest contour, separate the leaf marked by it and give it a black background.</p>
  
  <p><a href="https://i.stack.imgur.com/piYae.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/piYae.png" alt="biggest contour marked"></a></p>
  
  <p>This is kinda what we are trying to achieve (except the small leaf popping in):</p>
  
  <p><a href="https://i.stack.imgur.com/zIaZ7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zIaZ7.png" alt="enter image description here"></a></p>
  
  <p>Our approach was to draw a bounding box around the leaf and form a new separate frame. This is our code:</p>
  
  <pre><code>def nothing(useless=None):
      pass
  
  cv2.namedWindow("Mask")
  cap = cv2.VideoCapture(0)
  
  cv2.createTrackbar('R_l','Mask',26,255,nothing)
  cv2.createTrackbar('G_l','Mask',46,255,nothing)
  cv2.createTrackbar('B_l','Mask',68,255,nothing)
  
  cv2.createTrackbar('R_h','Mask',108,255,nothing)
  cv2.createTrackbar('G_h','Mask',138,255,nothing)
  cv2.createTrackbar('B_h','Mask',155,255,nothing)
  
  while True:
  
      R_l = cv2.getTrackbarPos('R_l', 'Mask')
      G_l = cv2.getTrackbarPos('G_l', 'Mask')
      B_l = cv2.getTrackbarPos('B_l', 'Mask')
  
      R_h = cv2.getTrackbarPos('R_h', 'Mask')
      G_h = cv2.getTrackbarPos('G_h', 'Mask')
      B_h = cv2.getTrackbarPos('B_h', 'Mask')
  
      _,frame = cap.read()
      blurred_frame = cv2.blur(frame,(5,5),0)
  
      hsv_frame = cv2.cvtColor(blurred_frame,cv2.COLOR_BGR2HSV)
  
      low_green = np.array([R_l, G_l, B_l])
      high_green = np.array([R_h, G_h, B_h])
      green_mask = cv2.inRange(hsv_frame, low_green, high_green)
      green = cv2.bitwise_and(frame, frame, mask=green_mask)
  
      contours,_ = cv2.findContours(green_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
  
      try:
          sorted_ = sorted(contours,key=cv2.contourArea,reverse=True)
          biggest = sorted_[0]
          cv2.drawContours(frame,biggest,-1,(255,0,0),1)
      except :
          pass
      #kernel = np.zeros(frame.shape(), np.uint8)
      x,y,w,h=cv2.boundingRect(biggest)
      roi= frame[y:y+h, x:x+w] 
      blurred_frame1 = cv2.blur(roi,(5,5),0)
      hsv_frame1 = cv2.cvtColor(blurred_frame1,cv2.COLOR_BGR2HSV)
      low_green1 = np.array([R_l, G_l, B_l])
      high_green1 = np.array([R_h, G_h, B_h])
      green_mask1 = cv2.inRange(hsv_frame1, low_green, high_green)
      green1= cv2.bitwise_and(roi,roi, mask=green_mask1)
  
  
      cv2.imshow("frame",frame)
      cv2.imshow("Mask",green1)
      key = cv2.waitKey(1)
      if key == 27:
          break
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>How can we prepare the desired image?</p>