彩色直方图
(1)直方图是图像中像素强度分布。
(2)直方图统计了每一个强度值所具有的像素个数。
(3)cv2.calcHist(images, channels, mask, histSize, ranges)
import cv2
import matplotlib.pyplot as plt
import numpy as np
def show_image(image,title,pos):
plt.subplot(3,2,pos)
plt.title(title)
image_RGB=image[:,:,::-1]#BGR to RGB
plt.imshow(image_RGB)
plt.axis("off")
#显示彩色直方图
def show_histogram(hist,title,pos,color):
plt.subplot(3,2,pos)
plt.title(title)
plt.xlim([0,256])
for h,c in zip(hist,color):
plt.plot(h,color=c)
#计算直方图
def calc_color_hist(image):
hist=[]
hist.append(cv2.calcHist([image],[0],None,[256],[0,256]))
hist.append(cv2.calcHist([image],[1],None,[256],[0,256]))
hist.append(cv2.calcHist([image],[2],None,[256],[0,256]))
return hist
#主函数
def main():
#创建画布
plt.figure(figsize=(12,8))
plt.suptitle("Color Histogram",fontsize=4,fontweight="bold")
#读取原图
img=cv2.imread("./2037551.jpg")
#计算直方图
img_hist=calc_color_hist(img)
#显示直方图
show_image(img,"RGB Image",1)
show_histogram(img_hist,"RGB Image Hist",2,('b','g','r'))
#原始图片中的每个像素增加50个像素值
M=np.ones(img.shape,dtype="uint8")*50
added_image=cv2.add(img,M)#像素一一对应相加
added_image_hist=calc_color_hist(added_image)
show_image(added_image,'added image',3)
show_histogram(added_image_hist,'added image hist',4,('b','g','r'))
#原始图片中的每个像素减去50个像素值
subtracted_image=cv2.subtract(img,M)
subtracted_image_hist=calc_color_hist(subtracted_image)
show_image(subtracted_image,'subtracted Image',5)
show_histogram(subtracted_image_hist,'subtracted Image hist',6,('b','g','r'))
plt.show()
if __name__=='__main__':
main()
琐碎时间阅读基础知识,详情关注微信公众号“知识代码AI”。