Search code examples
pythonopencvimage-processingscikit-image

is it possible to extracting text inside colored background region using opencv in python?


I having the following table area from the original image:

enter image description here

I'm trying extract the text,from this table.But when using threshold the whole gray regions get darkening.For example like below,

enter image description here

The threshold type which i did used,

thresh_value = cv2.threshold(original_gray, 128, 255, cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)[1]

is it possible to extract and change gray background into white and lets remain text pixel as it is if black then?


Solution

  • You should use adaptive thresholding in Python/OpenCV.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread("text_table.jpg")
    
    # convert img to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # do adaptive threshold on gray image
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 11)
    
    # write results to disk
    cv2.imwrite("text_table_thresh.jpg", thresh)
    
    # display it
    cv2.imshow("thresh", thresh)
    cv2.waitKey(0)
    


    Result

    enter image description here