Search code examples
pythonopencvcheckboxdetection

How to use OpenCV to detect a checkbox that intersects a line in a document


I have successfully used code to detect checkboxes in documents, following the base case similar to:

How to detect/find checkbox contours using OpenCV

It is working well, however it fails whenever a box touches a line on one of its sides, which occurs fairly frequently in my use case. I have included two examples, one original and one is the image after being processed with canny.

Checkbox intersecting line Checkbox after canny

Is there a general method for separating two overlapping contours, or some other method I could use to detect the box that has such an overlap?


Solution

  • You can use morphology to remove most of the long line using Python/OpenCV as follows. Use morphology close to detect the line. Then invert the line and add it back to the input image to write over the line with white.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    img = cv2.imread('box_line.png')
    
    # do morphology to find long horizontal lines using a horizontal kernel longer than the width of the box
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (80,1))
    line = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations = 1)
    
    # invert line
    line = (255-line)
    
    # add inverted line to image
    result = cv2.add(img, line)
    
    # write result to disk
    cv2.imwrite("line_removed.png", result)
    
    # display it
    cv2.imshow("image", img)
    cv2.imshow("line", line)
    cv2.imshow("result", result)
    cv2.waitKey(0)
    


    enter image description here

    Use the above to preprocess your image and then use your other code to extract the box via contours.