How to auto adjust contrast and brightness of a scanned Image with opencv python
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:
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:
Result:
How to do a localized Contrast Enhancement In a scanned Image Using OpenCV Python
Here is one way to do that in Python/OpenCV using division normalization and some sharpening.
- Read the input
- Convert to grayscale
- Blur the image
- Divide the grayscale image by the blurred image
- Apply sharpening (as desired)
- Save the results
Input:
import cv2
import numpy as np
import skimage.filters as filters
# read the image
img = cv2.imread('math_questions.jpg')
# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# blur
smooth = cv2.GaussianBlur(gray, (95,95), 0)
# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)
# sharpen using unsharp masking
result = filters.unsharp_mask(division, radius=1.5, amount=1.5, multichannel=False, preserve_range=False)
result = (255*result).clip(0,255).astype(np.uint8)
# save results
cv2.imwrite('math_question_division.jpg',division)
cv2.imwrite('math_question_division_sharpen.jpg',result)
# show results
cv2.imshow('smooth', smooth)
cv2.imshow('division', division)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Division image:
Sharpened result:
Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV
This method should work well for your application. First you find a threshold value that separates the distribution modes well in the intensity histogram then rescale the intensity using that value.
from skimage.filters import threshold_yen
from skimage.exposure import rescale_intensity
from skimage.io import imread, imsave
img = imread('mY7ep.jpg')
yen_threshold = threshold_yen(img)
bright = rescale_intensity(img, (0, yen_threshold), (0, 255))
imsave('out.jpg', bright)
I'm here using Yen's method, can learn more about this method on this page.
Related Topics
Create a C# Method to Generate Auto Increment Id
Asp.Net Core - Swashbuckle Not Creating Swagger.Json File
How to Call a Button Click Event from Another Method
How to Convert English Digits to Arabic Digits
Asp.Net C# Button Onclick Even Not Firing
Find Item in Observablecollection Without Using a Loop
How to Properly Make a Http Web Get Request
How to Programmatically Set the System Volume
Linq - Select Date from Datetime
Cannot Access a Closed Stream of a Memorystream, How to Reopen
Deserialize Json in C# - How to Handle Null Return Values
Passing Datetimeoffset as Webapi Query String
How to Call Wcf Service Method from Postman
How to Validate a Datetime in C#