1

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.

ISHAN JAISWAL
  • 66
  • 1
  • 8
  • I think you have a very simple documentation here. https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html – KalanaChinthaka Aug 04 '20 at 08:46
  • Thank you for your recommendation. But that is a very simple linear solution. 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 more dark more in contrast and the light pixels to get more white but not to a point where it get over exposed or anything like that – ISHAN JAISWAL Aug 04 '20 at 10:36

2 Answers2

7

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
3

You can use any local binarization method. In OpenCV there is one such method called Wolf-Julion local binarization which can be applied to the input image. Below is code snippet as an example:

import cv2

image = cv2.imread('input.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)[:,:,2]

T = cv2.ximgproc.niBlackThreshold(gray, maxValue=255, type=cv2.THRESH_BINARY_INV, blockSize=81, k=0.1, binarizationMethod=cv2.ximgproc.BINARIZATION_WOLF)
grayb = (gray > T).astype("uint8") * 255

cv2.imshow("Binary", grayb)
cv2.waitKey(0)

The output result from above code is below. Please note that to use ximgproc module you need to install opencv contrib package.

enter image description here

amras
  • 1,499
  • 2
  • 6
  • 10
  • Thanks you the output really blew me away. Just 1 thing though as you can see the result you gave did not give me a color image it just gave me a black and white image. Also when working on a different Image the results are not looking good because of the thresholding. Is there any option for a localized contrast adjustment. I believe that would give a similar effect while preserving the details and color. Thank you for your help. – ISHAN JAISWAL Aug 04 '20 at 16:11
  • Thanks. This was a perfect solution for me. Had to scan in 26 pages of a lightly printed document. My scanner has a document feeder so that was easy. But each page was an individual jpeg. Wrote a quick python script to fix the contrast on all pages in seconds so they could be read! Two thumbs up!! – Daniel Goldfarb Jul 05 '21 at 18:11