OpenCV - Removal of noise in image

后端 未结 9 1710
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 01:01

I have an image here with a table.. In the column on the right the background is filled with noise

How to detect the areas with noise? I only want to apply some kind of

9条回答
  •  渐次进展
    2021-02-04 01:28

    My solution is based on thresholding to get the resulted image in 4 steps.

    1. Read image by OpenCV 3.2.0.
    2. Apply GaussianBlur() to smooth image especially the region in gray color.
    3. Mask the image to change text to white and the rest to black.
    4. Invert the masked image to black text in white.

    The code is in Python 2.7. It can be changed to C++ easily.

    import numpy as np
    import cv2
    import matplotlib.pyplot as plt
    %matplotlib inline 
    
    # read Danish doc image 
    img = cv2.imread('./imagesStackoverflow/danish_invoice.png')
    
    # apply GaussianBlur to smooth image
    blur = cv2.GaussianBlur(img,(5,3), 1) 
    
    # threshhold gray region to white (255,255, 255) and sets the rest to black(0,0,0)
    mask=cv2.inRange(blur,(0,0,0),(150,150,150))
    
    # invert the image to have text black-in-white
    res = 255 - mask
    
    plt.figure(1)
    plt.subplot(121), plt.imshow(img[:,:,::-1]), plt.title('original') 
    plt.subplot(122), plt.imshow(blur, cmap='gray'), plt.title('blurred')
    plt.figure(2)
    plt.subplot(121), plt.imshow(mask, cmap='gray'), plt.title('masked')
    plt.subplot(122), plt.imshow(res, cmap='gray'), plt.title('result')
    plt.show()
    

    The following is the plotted images by the code for reference.

    Here is the result image at 2197 x 3218 pixels.

提交回复
热议问题