Automatic Enhancement of Scanned Images

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:

Sample Image

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:

Sample Image

Result:

Sample Image

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:

Sample Image

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:

Sample Image

Sharpened result:

Sample Image

Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV

enter image description here

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



Leave a reply



Submit