opencv python connectedComponents select component per label -
i want select each component of image :
in practice, each , every triangle, labels. don't figure out how. have code:
#!/usr/bin/python import cv2 import numpy np img = cv2.imread('invmehs.png', -1) imgray = cv2.cvtcolor(img, cv2.color_bgr2gray) ret, imbw = cv2.threshold(imgray, 250, 255, cv2.thresh_binary) invbwmesh = cv2.bitwise_not(imbw) mask = np.ones(imbw.shape, dtype="uint8") * 255 connectivity = 4 output = cv2.connectedcomponentswithstats(imbw, connectivity, cv2.cv_32s) num_labels = output[0] labels = output[1] stats = output[2] centroids = output[3] labels = labels + 1 b = ( labels == 1) cv2.imwrite('tst.jpg',labels[b])
but image complety black :s thank much.
the image want save (labels[b]) contains thin lines (greylevel 1). when saving image using jpeg format, compression algorithm smooths them, since have 1 greylevel background, erased. that's why black image
saving in png format not alter image labels.
in order keep labels each connected component (0 background), code write should :
cv2.imwrite('labels.png',output[1])
Comments
Post a Comment