Contrast enhancement means widening the grayscale range of the image. For example, if the grayscale distribution range of the image is between [50,150], the range will be increased to [0,256]. Here we introduce algorithms such as linear transformation, histogram regularization, gamma transformation, global histogram equalization, and limited contrast adaptive histogram equalization.
linear transformation
The grayscale value is processed through the function y=ax+b. For example, for a too dark picture, the grayscale distribution is [0,100]. Selecting a=2,b=10 can stretch the grayscale range to [10, 210 ]. This can be achieved through the convertScaleAbs() function of np or opencv.
#coding:utf-8
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from cv2 import convertScaleAbs
img = cv.imread(r"C:\Users\mzd\Desktop\opencv\images.jpg")
print(img)
img_bright = cv.convertScaleAbs(img,alpha=1.5,beta=0)
print(img_bright)
cv.imshow("img",img)
cv.imshow("img_bright",img_bright)
cv.waitKey(0)
cv.destroyAllWindows()
convertScaleAbs()
Histogram regularization
#coding:utf-8
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img = cv.imread(r"C:\Users\mzd\Desktop\opencv\images.jpg")
img_norm=cv.normalize(img,dst=None,alpha=350,beta=10,norm_type=cv.NORM_MINMAX)
cv.imshow("img",img)
cv.imshow("img_norm",img_norm)
cv.waitKey(0)
cv.destroyAllWindows()
cv.normalize()
Global histogram equalization
#coding:utf-8
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import math
img = cv.imread(r"C:\Users\Administrator\Desktop\dark.jpg",0)
img_equalize = cv.equalizeHist(img)
cv.imshow("img",img)
cv.imshow("img_equalize",img_equalize)
cv.waitKey(0)
cv.destroyAllWindows()
opencv equalizeHist()