import chardet
import numpy as np
import cv2 as cv
from PIL import Image
import sys
from matplotlib import pyplot as plt
plt库的中文乱码处理:
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
对于opencv的像素是BGR顺序,然而matplotlib所遵循的是RGB顺序:
或者采用img3=img[:,:,::-1]转换
或者采用img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
图像均衡化,参数说明:一维数组,bin+1的长度(即257),range
hist = cv.calcHist([images],[0],None,[256],[0,256])
plt.figure("Image")
#这里必须加 cmap='gray' ,否则显示的是伪彩色图像
plt.subplot(121)
plt.title('gray')
# 灰度图像
img = cv.imread('D:/1/1.jpg',0)
plt.imshow(img,cmap='gray')
plt.subplot(122)
# 彩色图像
img = cv.imread('D:/1/1.jpg',1)
b,g,r=cv.split(img)
img2=cv.merge([r,g,b])
plt.title('color ')
plt.imshow(img2)
plt.show()
img = cv.imread("D:/1/1.jpg",0)
# 返回值hist:数组直方图的值,bin:返回bin边缘(length(hist)+1)
hist, bins = np.histogram(img.flatten(), 256, [0,256])
cdf = hist.cumsum()
cdf_normalized = cdf*float(hist.max())/cdf.max()
plt.plot(cdf_normalized, color = 'b') #画曲线
plt.hist(img.flatten(),256,[0,256], color = 'r') #绘制直方图
plt.xlim([0,256]) # x轴的范围
plt.show()