Search code examples
pythonimageopencvbordercrop

crop the border of image python


I have this picture enter image description here

and I want to crop it like this enter image description here

I use this code, but it doesn't crop the black border. so, can someone help me?

    im = cv2.imread("Data/"+file, 0)
    retval, thresh_gray = cv2.threshold(im, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
    points = np.argwhere(thresh_gray==0)
    points = np.fliplr(points)
    x, y, w, h = cv2.boundingRect(points)
    crop = im[y:y+h, x:x+w] # create a crop
    retval, thresh_crop = cv2.threshold(crop, thresh=200, maxval=255, type=cv2.THRESH_BINARY)
    path = 'D:\Picture\Camera Roll'
    cv2.imwrite(os.path.join(path , file),thresh_crop)

Solution

  • I think you can use contours for your solution The contours are a curve/line joining continuous points of same intensity. So the box enclosing the written script is one contour. The contours in an image also have relationships like one contour could be a parent of other contour(s). In your image, the box could be the parent of the written script.

    cv2.findContours finds all the contours in an image and the second parameter(cv2.RETR_TREE) is to mention what kind of relationships should be returned. As the contours have an hierarchy, it's likely that the box will be in index 0 or 1 of the contours list.

    import matplotlib.pyplot as plt
    %matplotlib inline
    img = cv2.imread('image.png', 0)
    ret, thresh = cv2.threshold(img, 127, 255, 0)
    image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    

    enter image description here

    I though the the first contour(contours[0]) would represent the box, but for some reason it's the second one.

    img_copy = img.copy()
    cnt= contours[1]
    cv2.drawContours(img_copy, [cnt], -1, color = (255, 255, 255), thickness = 20)
    plt.imshow(img_copy, 'gray')
    

    enter image description here

    After you have the contours, just draw a thick white line over the contour to remove the box. Here's a solution to draw specific contours on a image

    Don't worry about the bounding box in these images. It's just matplotlib. Your image is what is inside the coordinate box.