圖片連通區域檢測
1.原圖
2.連通區域結果
3.編碼實現
from skimage import measure, color
import cv2
import numpy as np
import matplotlib.pyplot as plt
def detect(image):
label_img, num = measure.label(image, neighbors=8, background=0, return_num=True, connectivity=2)
return label_img, num
if __name__=='__main__':
in_image_path = 'binary.jpg'
out_image_path = 'labeled.jpg'
img = cv2.imread(in_image_path, 0)
assert img is not None
ret, thresh = cv2.threshold(img, 170, 255, cv2.THRESH_BINARY)
label_img, num = detect(thresh)
dst = color.label2rgb(label_img)
cv2.imshow('1', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()