'Detect filled checkboxes

I am using boxdetect 1.0.0 to detect the coordinates of the checkboxes.

I am directly using get_checkboxes method with the minor configurations changes

It is detecting this type of checkboxes enter image description here

But not able to detect in these cases enter image description here

Any preprocessing suggestions to make the second type of checkbox detect? or any other type of method which can detect these kinds of checkboxes

Code snippet

#pip install boxdetect
from boxdetect.pipelines import get_checkboxes
from boxdetect import config

def default_checkbox_config():
    cfg = config.PipelinesConfig()

    # important to adjust these values to match the size of boxes on your image
    cfg.width_range = (5,35)
    cfg.height_range = (5,35)

    # the more scaling factors the more accurate the results but also it takes more time to processing
    # too small scaling factor may cause false positives
    # too big scaling factor will take a lot of processing time
    cfg.scaling_factors = [10]

    # w/h ratio range for boxes/rectangles filtering
    cfg.wh_ratio_range = (0.5, 1.7)

    # group_size_range starting from 2 will skip all the groups
    # with a single box detected inside (like checkboxes)
    cfg.group_size_range = (2, 100)

    # num of iterations when running dilation tranformation (to engance the image)
    cfg.dilation_iterations = 0
    return cfg

def divide_checkbox(checkboxes,crop_image_file,pdf_file_name):
    img = cv2.imread(crop_image_file)
    checkbox_counter = 0  
    for checkbox in checkboxes:
        checkbox_counter+=1
        cropped = img[checkbox[0][1]:checkbox[0][1]+checkbox[0][3],checkbox[0][0]:checkbox[0][2]+checkbox[0][0]]
        # mpimg.imsave("checkbox_image/out{}{}.png".format(pdf_file_name,checkbox_counter), cropped)
        plt.imshow(cropped)
        plt.show()

def get_all_checkboxes(crop_image_file,pdf_file_name):
    cfg = default_checkbox_config()
    checkboxes = get_checkboxes(
        crop_image_file, cfg=cfg, px_threshold=0.1, plot=False, verbose=True)
    divide_checkbox(checkboxes,crop_image_file,pdf_file_name)
try:
    pdf_file_name = "something"
    crop_image_file= "shady_tick3.png" #just give your image path
    get_all_checkboxes(crop_image_file,pdf_file_name)
except:
    print(traceback.format_exc())


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source