Skip to content

Instantly share code, notes, and snippets.

@YCAyca
Created September 12, 2021 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YCAyca/b22590d8fe1977d7547ce277a1581a0a to your computer and use it in GitHub Desktop.
Save YCAyca/b22590d8fe1977d7547ce277a1581a0a to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import matplotlib.pyplot as plt
def show_components(labels):
# 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 Image after Component Labeling
plt.imshow(cv2.cvtColor(labeled_img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title("Image after Component Labeling")
plt.show()
img = cv2.imread("dog.jpg", cv2.IMREAD_GRAYSCALE)
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]
img2 = cv2.imread("peppers.png", cv2.IMREAD_GRAYSCALE)
img2 = cv2.threshold(img2, 127, 255, cv2.THRESH_BINARY)[1]
img3 = cv2.imread("opencv2.png", cv2.IMREAD_GRAYSCALE)
img3 = cv2.threshold(img3, 127, 255, cv2.THRESH_BINARY)[1]
""" 8 connectivity connected components """
num_labels, labels_im = cv2.connectedComponents(img, connectivity=8)
num_labels2, labels_im2 = cv2.connectedComponents(img2, connectivity=8)
num_labels3, labels_im3 = cv2.connectedComponents(img3, connectivity=8)
print(num_labels)
print(num_labels2)
print(num_labels3)
show_components(labels_im)
show_components(labels_im2)
show_components(labels_im3)
""" 4 connectivity connected components """
num_labels, labels_im = cv2.connectedComponents(img, connectivity=4)
num_labels2, labels_im2 = cv2.connectedComponents(img2, connectivity=4)
num_labels3, labels_im3 = cv2.connectedComponents(img3, connectivity=4)
print(num_labels)
print(num_labels2)
print(num_labels3)
show_components(labels_im)
show_components(labels_im2)
show_components(labels_im3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment