kero.ImageProcessing.photoBox.py def get_connected_components(gs_matrix, significant_fraction=0.001): return out
This function takes in 2D matrix containing only zeros and ones. They are to represent images thresholded to either black and white, i.e. RGB (0,0,0) and (255,255,255) converted to gray scale and scaled to 0 and 1. And then it outputs a list of similarly zero and one 2D matrices, each of which only contains a single connected component. See the image shown in example 1.
Important: this function makes use of some recursions and is thus slow for processing large image. The strategy is to shrink the image (for example to about 200 by 150) and compute the actual components of the original large image, although we will lose some precision.
|gs_matrix||List of integers, 2D matrix of zeros and ones.|
|significant_fraction||Float. This argument takes in values between 0 and 1.0. It specifies the fraction of area relative to the area of whole matrix, above which a component will be considered as a component.
The total area is computed by multiplying the number of rows to the number of columns. On the other hand, we can see the matrix as an image, where each entry corresponds to a pixel.
A pixel (i, j) is connected to the next pixel if and only if the next pixel differs by at most 1 in at most one entry. i.e. for (i, j), the entry (i, j+1) is a next pixel but (i-1,j+1) one pixel diagonal away is not. The area a component is the sum of all entries in the component matrix.
Default value is .001
|return out||List of 2D matrix. Each 2D matrix is a zero and one matrix. Each 2D matrix contains a single connected component.|
Example usage 1
Uncomment the commented parts of the codes to try this function on different images and 2D matrix.
import cv2 import numpy as np import kero.ImageProcessing.photoBox as kip # # TEST # gs_matrix = [[0, 0, 0, 0,0,1], [1, 0, 1,0,0,1], [1, 1, 1,0,0,1], # [0, 0, 0, 0, 0, 0],[0,0,0,0,0,0],[0,0,0,1,1,0], # [0, 0, 0, 0, 1, 0]] # out = kip.get_connected_components(gs_matrix) # cv2.imshow("init", np.array(gs_matrix)*255) # for x in gs_matrix: # print(x) # for i in range(len(out)): # print("__________________________") # for x in out[i]: # print(x) # # cv2.imshow(str(i), np.array(out[i])*255) # # cv2.waitKey(0) s1 = cv2.imread("pbtest3.png") s1grey = cv2.cvtColor(s1, cv2.COLOR_BGR2GRAY)/255 cv2.imshow("grey", s1grey) print(np.array(s1grey).shape) s1grey_components = kip.get_connected_components(s1grey, significant_fraction=0.001) print(np.array(s1grey_components).shape) for i in range(len(s1grey_components)): cv2.imshow("grey" + str(i), 255*np.array(s1grey_components[i])) cv2.waitKey(0)
The figure below is pbtest3.png. With the above settings, 3 connected components are obtained. Try increase the argument significant_fraction to 0.01. The bottom left component will be ignored, since the area is not large enough relative to the entire image to qualify as a component. This is useful to prevent noises from being recognized as components.
Some other images that you can segment using this function are shown below, and can be downloaded here.
kero version: 0.4 and above