import cv2
import os
import random
import numpy as np
def connectedComponent(img):
binary_img = img
ret, binary_img = cv2.threshold(binary_img, 127, 255, cv2.THRESH_BINARY)
# binary_img = SprayPredeal(img)
# binary_img = Predeal(img)
# cv2.imshow("binary_img", binary_img)
w, h = binary_img.shape
color = []
color.append((0, 0, 0))
img_color = np.zeros((w, h, 3), dtype=np.uint8)
retval, labels, stats, centroids = cv2.connectedComponentsWithStats(binary_img)
for num in range(1,retval):
color_b = random.randint(0, 255)
color_g = random.randint(0, 255)
color_r = random.randint(0, 255)
color.append((color_b, color_g, color_r))
for x in range(w):
for y in range(h):
lable = labels[x,y]
img_color[x,y,:] = color[int(lable)]
cv2.imshow("img_color",img_color)
cv2.waitKey(0)
if __name__ == '__main__':
tpath = "word2Data"
filelist = os.listdir(tpath)
for file in filelist:
fpath = os.path.join(tpath, file)
img = cv2.imread(fpath,0)
connectedComponent(img)
cv2连通域分割
最新推荐文章于 2022-10-26 15:35:09 发布