直方图

 

 

 

参数

 

 

 

 

o = cv2.imread("image\\boat.bmp",cv2.IMREAD_UNCHANGED)
cv2.imshow("o",o)
hist = cv2.calcHist([o],[0],None,[256],[0,255])
print(hist.shape)
print(hist)
print(type(hist))
print(hist.size)
cv2.waitKey()
cv2.destroyAllWindows()

 

直方图绘制

 

import cv2
import numpy as np
import matplotlib.pyplot as plt
# 灰色图片直方图
o = cv2.imread("image/boatGray.bmp")
histgray = cv2.calcHist([o],[0],None,[256],[0,255])
plt.plot(histgray,color='b')
plt.show()
# 彩图
o = cv2.imread("image/lenacolor.png")
histb = cv2.calcHist([o],[0],None,[256],[0,255])
histg = cv2.calcHist([o],[1],None,[256],[0,255])
histr = cv2.calcHist([o],[2],None,[256],[0,255])
plt.plot(histb,'c')
plt.plot(histg,"g")
plt.plot(histr,'r')
plt.show()

掩膜图像(二值图像

 

cv2.IMREAD_GRAYSCALE以灰度模式读入

 

 

 

 

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread("image/boat.bmp",cv2.IMREAD_GRAYSCALE)
mask = np.zeros(image.shape,np.uint8)
mask[200:400,200:400] = 255
histMI = cv2.calcHist([image],[0],mask,[256],[0,255])
plt.plot(histMI)
plt.show()

 

生成掩膜图像

 

image = cv2.imread("image/boat.bmp",0)
mask = np.zeros(image.shape,np.uint8)
mask[200:400,200:400]=255
mi = cv2.bitwise_and(image,mask)
cv2.imshow("original",image)
cv2.imshow("mask",mi)


cv2.waitKey()
cv2.destroyAllWindows()

直方图均衡化 

 

 

 

p

 

 

ravel函数二维变成一维

均衡函数

 

supplot函数

 

 

 

imshow

 

#灰度
o = cv2.imread("image/girl.bmp")
g = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
plt.subplot(221)
plt.imshow(o)
plt.axis('off')
plt.subplot(222)
plt.imshow(o,cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(223)
plt.imshow(g)
plt.axis('off')
plt.subplot(224)
plt.imshow(g,cmap=plt.cm.gray)
plt.axis('off')
plt.show()

#彩色图片
img = cv2.imread('image/girl.bmp')
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121)
plt.imshow(img)
plt.axis('off')
plt.subplot(122)
plt.imshow(img2)
plt.axis('off')
plt.show()

 

直方图均衡化对比

 

o = cv2.imread("image/lenacolor.png",cv2.IMREAD_GRAYSCALE)
e = cv2.equalizeHist(o)
plt.subplot(221)
plt.imshow(o,cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(222)
plt.imshow(e,cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(223)
plt.hist(o.ravel(),256)
plt.subplot(224)
plt.hist(e.ravel(),256)
plt.show()

 

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页