[Python] How to use OpenCV to clean up the background of a receipt image after removing the...

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have written Python code to remove shadows from a receipt image, but there is still noise caused by the dilation and medianBlur processes around the characters. Are there any algorithms to clean up the background while preserving the characters?

    This is my Python code that removes shadows from the receipt image.

    import cv2
    import pytesseract
    import sys
    import numpy as np


    sys.stdout.reconfigure(encoding='utf-8')
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

    imRGB = cv2.imread(r'D:\Senior-project\images\028.jpg')


    split_color_channels = cv2.split(imRGB)
    print(split_color_channels)

    color_channels_result = []
    color_norm_channels_result = []

    for color_channel in split_color_channels:

    img_dilation = cv2.dilate(color_channel, np.ones((7,7), np.uint8))

    blur_img = cv2.medianBlur(img_dilation, 21)

    diff_img = 255 - cv2.absdiff(color_channel, blur_img)

    norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)

    color_channels_result.append(diff_img)
    color_norm_channels_result.append(norm_img)
    result = cv2.merge(color_channels_result)`
    result_norm = cv2.merge(color_norm_channels_result)
    print(result_norm.shape)


    cv2.imwrite(r'image.jpg', result_norm)

    image = cv2.imread(r'D:\1204314 - Computer Vision and Image Processing\Week1\Lab0101\image.jpg')
    print(image.shape)


    Original Image

    [​IMG]

    Image with no shadows

    [​IMG]

    I need to clean up the background after the shadow removal process. I have tried using thresholding, but it causes some loss of character information. I need a method to clean up the background while maintaining the completeness and clarity of the characters so that the text can be extracted effectively.

    Continue reading...

Compartilhe esta Página