Remove noise from threshold image opencv python

后端 未结 3 2085
逝去的感伤
逝去的感伤 2020-12-08 08:00

I am trying to get the corners of the box in image. Following are example images, their threshold results and on the right after the arrow are the results that I need. You m

3条回答
  •  再見小時候
    2020-12-08 08:27

    Below is a python implementation of @dhanushka's approach

    import cv2
    import numpy as np
    
    # load color image
    im = cv2.imread('input.jpg')
    
    # smooth the image with alternative closing and opening
    # with an enlarging kernel
    morph = im.copy()
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
    morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
    morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    
    # take morphological gradient
    gradient_image = cv2.morphologyEx(morph, cv2.MORPH_GRADIENT, kernel)
    
    # split the gradient image into channels
    image_channels = np.split(np.asarray(gradient_image), 3, axis=2)
    
    channel_height, channel_width, _ = image_channels[0].shape
    
    # apply Otsu threshold to each channel
    for i in range(0, 3):
        _, image_channels[i] = cv2.threshold(~image_channels[i], 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
        image_channels[i] = np.reshape(image_channels[i], newshape=(channel_height, channel_width, 1))
    
    # merge the channels
    image_channels = np.concatenate((image_channels[0], image_channels[1], image_channels[2]), axis=2)
    
    # save the denoised image
    cv2.imwrite('output.jpg', image_channels)
    

    The above code doesn't give good results if the image you are dealing are invoices(or has large amount of text on a white background). In order to get good results on such images, remove

    gradient_image = cv2.morphologyEx(morph, cv2.MORPH_GRADIENT, kernel)
    

    and pass morph obj to the split function and remove the ~ symbol inside for loop

提交回复
热议问题