This function returns the set of bounding boxes for all connected components in a scaled gray scale matrix. The outcome is optimal if the matrix is a zeros-and-ones matrix. See the image in example 1.
kero.ImageProcessing.photoBox.py class GreyScaleWorkShop: def multiple_scaled_box_positions(self, gsimg, scale="Auto", significant_fraction=0.001): return rect_set
|gsimg||List of list. 2D matrix scaled to 0 and 1.
Note: To better obtain bounding box, use zeros and ones 2D matrix.
|scale=”Auto”||None: Input image is processed without scaling. This will be slow.
“Auto”: Input image is scaled to (200,150) pixels before being scanned.
(Integer, Integer): a 2-tuple of integers specifying the size in pixels the image is resize before being scanned.
Image will be fed into the function get_connected_components() as an argument. It is recommended that the scale is chosen to be small if processing speed is the priority.
Default value is “Auto”
|significant_fraction||Float, between 0 and 1.0. Argument to the function get_connected_components().
Default value = 0.001
|return rect_set||List of [xmin, ymin, xmax, ymax], each specifying the shape of the rectangle of a bounding box. (xmin, ymin) is the top-left coordinates and (xmax, ymax) the bottom-right.|
Download the image from here and place the image in the working directory.
Example usage 1
Try the following with different value of scale as well.
import kero.ImageProcessing.photoBox as kip import cv2, time t=time.time() gsw = kip.GreyScaleWorkShop() fn= "pbtest3.png" s1 = cv2.imread(fn) s1grey = cv2.cvtColor(s1, cv2.COLOR_BGR2GRAY)/255 # The time listed in the following applies for "pbtest3.png", 576 by 480 pixels # rect_set = gsw.multiple_scaled_box_positions(s1grey, scale="Auto", significant_fraction=0.001) # 1.8397045135498047 s rect_set = gsw.multiple_scaled_box_positions(s1grey, scale="Auto", significant_fraction=0.001) # 4.6596386432647705 s print("no of rects = ", len(rect_set)) print("rect_set=",rect_set) for i in range(len(rect_set)): print("index of rect = ",i) rect = rect_set[i] xmin = rect ymin = rect xmax = rect ymax = rect cv2.rectangle(s1, (xmin, ymin), (xmax,ymax),(0,255,0),1) # cv2.imshow("grey", s1grey) cv2.imshow("color", s1) elapsed = time.time() - t print("time taken (s) : ",elapsed) cv2.waitKey(0)
The output is shown below where the green boxes show the bounding boxes delineating each connected component. However, with scale set to “Auto” which is (200,150), we can see the loss of precision. With larger scale (300,200), the correct boxes are obtained.
kero version: 0.4.3 and above