Search code examples
pythonopencvbrightnesscontrastimage-preprocessing

How to auto adjust contrast and brightness of a scanned Image with opencv python


I want to auto adjust the brightness and contrast of a color image taken from phone under different lighting conditions. Please help me I am new to OpenCV.

Source: Input Image

Result: result

What I am looking for is more of a localized transformation. In essence, I want the shadow to get as light as possible completely gone if possible and get darker pixels of the image to get darker, more in contrast and the light pixels to get more white but not to a point where it gets overexposed or anything like that.

I have tried CLAHE, Histogram Equalization, Binary Thresholding, Adaptive Thresholding, etc But nothing has worked.

My initials thoughts are that I need to neutralize Highlights and bring darker pixels more towards the average value while keeping the text and lines as dark as possible. And then maybe do a contrast filter. But I am unable to Get the result please help me.


Solution

  • Here is one way to do that in Python/OpenCV.

    • Read the input
    • Increase contrast
    • Convert original to grayscale
    • Adaptive threshold
    • Use the thresholded image to make the background white on the contrast increased image
    • Save results

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread("math_diagram.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, 21, 15)
    
    # make background of input white where thresh is white
    result = img.copy()
    result[thresh==255] = (255,255,255)
    
    # write results to disk
    cv2.imwrite("math_diagram_threshold.jpg", thresh)
    cv2.imwrite("math_diagram_processed.jpg", result)
    
    # display it
    cv2.imshow("THRESHOLD", thresh)
    cv2.imshow("RESULT", result)
    cv2.waitKey(0)
    

    Threshold image:

    enter image description here

    Result:

    enter image description here