title: How to use OpenCV ConnectedComponents to get the images
body: <p>How to use Python OpenCV ConnectedComponents function to obtain the images?</p>
  
  <p>From searching some past question, I have only been able to find how to shade the connected objects in different colors (Which I tested and it worked, but I have no idea how the labels work)<br>
  Reference from these previously answered questions: <a href="https://stackoverflow.com/questions/48303309/use-cv2-connectedcomponents-and-eliminate-elements-with-a-small-number-of-pixels">Stackoverflow question 48303309</a> and <a href="https://stackoverflow.com/questions/46441893/connected-component-labeling-in-python">Stackoverflow question 46441893</a></p>
  
  <p>Using this code, I can get the shaded output</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img = cv2.imread('eGaIy.jpg', 0)
  img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]  # ensure binary
  ret, labels = cv2.connectedComponents(img)
  
  # Map component labels to hue val
  label_hue = np.uint8(179*labels/np.max(labels))
  blank_ch = 255*np.ones_like(label_hue)
  labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
  
  # cvt to BGR for display
  labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
  
  # set bg label to black
  labeled_img[label_hue==0] = 0
  
  cv2.imshow('labeled.png', labeled_img)
  cv2.waitKey()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JELaz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JELaz.png" alt="Original"></a>
  <a href="https://i.stack.imgur.com/TJgbJ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TJgbJ.png" alt="Shaded"></a></p>
  
  <p>Is there any way I can get the connected objects out from the image?<br>
  So output would be multiple images from the original image</p>