对比度增强,即将图片的灰度范围拉宽,如图片灰度分布范围在[50,150]之间,将其范围拉升到[0,256]之间。这里介绍下 线性变换,直方图正规化,伽马变换,全局直方图均衡化,限制对比度自适应直方图均衡化等算法。
线性变换
通过函数y=ax+b对灰度值进行处理,例如对于过暗的图片,其灰度分布在[0,100], 选择a=2,b=10能将灰度范围拉伸到[10, 210]。可以通过np或者opencv的convertScaleAbs()函数来实现 。
#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()
直方图正规化
#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()
全局直方图均衡化文章来源:https://www.toymoban.com/news/detail-794112.html
#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()
文章来源地址https://www.toymoban.com/news/detail-794112.html
到了这里,关于opencv-图像对比度增强的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!