In [38]:
# Importing the libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
def connected_component_label(path):
# Getting the input image
img = cv2.imread(path, 0)
# Converting those pixels with values 1-127 to 0 and others to 1
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]
# Applying cv2.connectedComponents()
num_labels, labels = cv2.connectedComponents(img)
# Map component labels to hue val, 0-179 is the hue range in OpenCV
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])
# Converting cvt to BGR
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue==0] = 0
# Showing Original Image
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.title("Orginal Image")
plt.show()
#Showing Image after Component Labeling
plt.imshow(cv2.cvtColor(labeled_img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title("Image after Component Labeling")
plt.show()
In [39]:
connected_component_label('C:/Users/Yash Joshi/OG_notebooks/face.jpg')
In [40]:
connected_component_label('C:/Users/Yash Joshi/OG_notebooks/crosses.jpg')
In [41]:
connected_component_label('C:/Users/Yash Joshi/OG_notebooks/shapes.png')