对比度增强,即将图片的灰度范围拉宽,如图片灰度分布范围在[50,150]之间,将其范围拉升到[0,256]之间。这里介绍下 线性变换,直方图正规化,伽马变换,全局直方图均衡化,限制对比度自适应直方图均衡化等算法。
线性变换
通过函数y=ax+b对灰度值进行处理,例如对于过暗的图片,其灰度分布在[0,100], 选择a=2,b=10能将灰度范围拉伸到[10, 210]。可以通过np或者opencv的convertScaleAbs()函数来实现 。
#coding:utf-8import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from cv2 import convertScaleAbsimg = 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()
直方图正规化
#coding:utf-8import cv2 as cv
import matplotlib.pyplot as plt
import numpy as npimg = 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()
全局直方图均衡化
#coding:utf-8import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import mathimg = 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()