直方图和直方图均衡化
a) 计算图像R,G,B通道的直方图,并利用matplotlib显示出来
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('F:/img/island.png')
color = ('r','g','b')
plt.figure(figsize=(7,7))
for i,col in enumerate(color):
plt.hist(img[:,:,i].ravel(),256,[0,256],color = col);
plt.show()
plt.imshow(img,cmap = 'gray')
b) 计算图像island.png对应灰度图像的直方图和直方累计图,并利用matplotlib显示出来
c) 利cv2.equalizeHist()进行均衡化,并画出均衡化后的直方图和直方累计图
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('F:/img/island.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hist_img,_ = np.histogram(gray,256)
cdf_img = np.cumsum(hist_img)
plt.figure(figsize=(13,13))
plt.subplot(2,2,1)
plt.plot(range(256),cdf_img,color = 'b')
plt.legend(loc='best')
plt.subplot(2,2,2)
plt.hist(gray.ravel(),256,[0,256],color = 'b');
plt.subplot(2,2,3)
plt.imshow(gray,cmap = 'gray')
plt.show()
使用图像灰度映射实现图像增强
a) 图像的灰度线性变换是通过建立灰度映射来调整原始图像的灰度,从而改善图像的质量,凸显图像的细节,提高图像的对比度。灰度线性变换的计算公式如下所示 g(x)=αf(x)+β
"""
Created on Fri Apr 10 20:28:47 2020
@author: Administrator
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('F:/img/lena.png')
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height = grayImage.shape[0]
width = grayImage.shape[1]
result_1 = np.zeros((height, width), np.uint8)
result_2 = np.zeros((height, width), np.uint8)
result_3 = np.zeros((height, width), np.uint8)
result_4 = np.zeros((height, width), np.uint8)
result_5 = np.zeros((height, width), np.uint8)
result_6 = np.zeros((height, width), np.uint8)
for i in range(height):
for j in range(width):
if (int(grayImage[i,j]) > 255):
gray = 255
else:
gray =