圖片連通區域檢測

圖片連通區域檢測

1.原圖

在這裏插入圖片描述

2.連通區域結果

在這裏插入圖片描述

3.編碼實現

#coding=utf-8
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)
    # plt.imshow(dst)

    # print('for_debug: thresh.dtype={}, shape={}'.format(thresh.dtype, thresh.shape))
    # cv2.imshow('1', label_img.astype(np.uint8))
    cv2.imshow('1', dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # cv2.imwrite(out_image_path, label_img.astype(np.uint8))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章