How to detect edge and crop an image in Python

前端 未结 1 487
旧时难觅i
旧时难觅i 2020-12-24 09:05

I\'m new to Image Processing in Python and I\'m trying to solve a common problem. I have an image having a signature of a person. I want to find the edges and crop it to fit

1条回答
  •  春和景丽
    2020-12-24 09:38

    What you need is thresholding. In OpenCV you can accomplish this using cv2.threshold().

    I took a shot at it. My approach was the following:

    1. Convert to grayscale
    2. Threshold the image to only get the signature and nothing else
    3. Find where those pixels are that show up in the thresholded image
    4. Crop around that region in the original grayscale
    5. Create a new thresholded image from the crop that isn't as strict for display

    Here was my attempt, I think it worked pretty well.

    import cv2
    import numpy as np
    
    # load image
    img = cv2.imread('image.jpg') 
    rsz_img = cv2.resize(img, None, fx=0.25, fy=0.25) # resize since image is huge
    gray = cv2.cvtColor(rsz_img, cv2.COLOR_BGR2GRAY) # convert to grayscale
    
    # threshold to get just the signature
    retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
    
    # find where the signature is and make a cropped region
    points = np.argwhere(thresh_gray==0) # find where the black pixels are
    points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices
    x, y, w, h = cv2.boundingRect(points) # create a rectangle around those points
    x, y, w, h = x-10, y-10, w+20, h+20 # make the box a little bigger
    crop = gray[y:y+h, x:x+w] # create a cropped region of the gray image
    
    # get the thresholded crop
    retval, thresh_crop = cv2.threshold(crop, thresh=200, maxval=255, type=cv2.THRESH_BINARY)
    
    # display
    cv2.imshow("Cropped and thresholded image", thresh_crop) 
    cv2.waitKey(0)
    

    And here's the result:

    0 讨论(0)
提交回复
热议问题