Search code examples
pythonpython-3.xopencvpython-imaging-library

OpenCV: Crop an image based on certain condition


I need to crop the image when ever there's a blank space detected. input image

and the expected output is the individual cropped images of the input data like below after blank space is detected between two text in an image.

image 1

image 2

image 3

Any idea would be appreciated!


Solution

  • Here is how I suggest to do that in Python/Opencv from my comment above.

    "Threshold on the green using cv2.inRange(). Use a horizontal morphology close such that the letters space is closed, but not so large that the space between words is filled. Then get contours and get the bounding boxes. Then use the bounding boxes to crop the original image."

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read the image
    img = cv2.imread('StandOutFrom.png')
    
    # threshold on green
    lower=(30,30,0)
    upper=(110,110,60)
    thresh = cv2.inRange(img, lower, upper)
    
    # apply horizontal close morphology
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,3))
    morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    
    # get external contours
    contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]
    
    # get bounding boxes and save to disk
    i = 1
    result = img.copy()
    for cntr in contours:
        # get bounding boxes
        x,y,w,h = cv2.boundingRect(cntr)
        cv2.rectangle(result, (x, y), (x+w, y+h), (0, 0, 255), 2)
        print(i,x,y,w,h)
        crop = img[y:y+h, x:x+w]
        cv2.imwrite("StandOutFrom_crop_{0}.png".format(i), crop)
        i = i + 1
    
    # save result
    cv2.imwrite('StandOutFrom_thresh.png', thresh )
    cv2.imwrite('StandOutFrom_morph.png', morph )
    cv2.imwrite('StandOutFrom_bounding_boxes.png', result )
    
    # show results
    cv2.imshow('thresh', thresh)
    cv2.imshow('morph', morph)
    cv2.imshow('boundingboxes', result)
    cv2.waitKey(0)
    

    Threshold Image:

    enter image description here

    Morphology Close Image:

    enter image description here

    Bounding Box Image:

    enter image description here

    Result Images:

    enter image description here

    enter image description here

    enter image description here