直方图
原创
©著作权归作者所有:来自51CTO博客作者wx635131693f6f9的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.直方图就是为了显示图片中像素的分布,直方图的图像传入必须是灰色图
API:cv.calcHist([img],[chanels],mask,histSize,ranges,[,hist,accumulate])
参数:
img:传入的图像
chanels:图像的灰度值
mask:掩膜
histSize:Bin数目,像素的数目,默认为[256]
ranges:像素值范围,默认为[0,256]
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg',0)
#创建直方图图像变量
hist = cv.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist)#绘制直方图
plt.show()
2.掩膜的应用,作用就是为凸显图像中某个区域,也可以用于屏蔽某个区域
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg',0)
#创建掩膜参数变量
mask = np.zeros(img.shape[:2],np.uint8)
mask[100:75,200:150] = 255#创建掩膜突出变量img_mask = cv.bitwise_and(img,img,mask = mask)#统计掩膜后的灰色图mask_hist = cv.calcHist([img],[0],mask,[256],[0,256])plt.plot(mask_hist)plt.show()
3.直方图均衡化就是将图像变得更加的颜色浅
API:cv.eqaulizeHist(img)
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg',0)
#创建均衡化对象
dist = cv.equalizeHist(img)
plt.imshow(dist,cmap = plt.cm.gray)
plt.show()
4.自适应的均衡化:将图像进行划分,设置对比度限制,超过限制就会均衡化
API:cv.createCLAHE(clipLimit,TileGridSize)
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('image1.jpg',0)
#创建自适应均衡化对象
dist = cv.createHist = (clipLimit = 2.0,tileGridSize = (4,4))
cl = cv.apply(img)
plt.imshow(cl,cmap = plt.cm.gray)
plt.show()