本文主要内容
- 图像增强是什么?为什么要做图像增强?
- 如何进行图像增强?(小结)
- 灰度级变换
什么是图像增强、以及为什么要这么做
要注意‘增强’并没有增加任何信息量,同时没有统一客观评价标准,不同应用不同判定
如何进行图像增强(方法概述)
- 灰度变换
- 滤波器
- 空间域滤波
- 频域滤波
- 代数运算(视频信号)
总结:
空间域增强(空域变换)
- 灰度变换
- 灰度直方图均衡方法的推导
空间域增强的重要特点,不改变位置信息,只改灰度级
灰度变换本质也是数学函数在做变换因此
- 线性变换
- 非线性变换
线性变换
非线性变换(常见的有)
- 对数变换
- 幂次变换
- 灰度直方图均衡
这里的窄带是什么意思??低亮度区间(低灰度级)?
示例:
示例:
灰度级变换前后的灰度级直方图关系讨论
- 变换前后灰度直方图区域面积相等
- 衍生出灰度直方图均衡法
由此可得到:
由此衍生出重要的图像处理方案
直方图均衡(非线性变换方法的代表)
求解新灰度级构造函数
示例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def Origin_histogram (image):
histogram = {} #创建 dict 存储原始图像的各个灰度级像素点有多少个
for i in range(image.shape[0]):
for j in range(image.shape[1]):
temp_gray = image[i][j]
if temp_gray in histogram:
histogram[temp_gray] = histogram[temp_gray] + 1
else:
histogram[temp_gray] = 1
sorted_histogram = {} # 灰度级从小到大排序的dict
sorted_list = sorted(histogram.keys())
for i in range( len( sorted_list ) ):
sorted_histogram[ sorted_list[i] ] = histogram[ sorted_list[i] ]
return sorted_histogram
def equalization_histogram( histogram, img ):
pr = {}#建立概率分布映射表
A_0 = img.shape[0] * img.shape[1]
D_m = max( histogram )
for i in histogram.keys():
pr[i] = histogram[i] / A_0
tmp = 0
for m in pr.keys():
tmp = pr[m] + tmp
pr[m] = max( histogram ) * tmp
new_img = np.zeros( shape = ( img.shape[0], img.shape[1] ), dtype = np.uint8 )
for k in range( img.shape[0] ):
for l in range( img.shape[1] ):
new_img[k][l] = pr[img[k][l]]
return new_img
def GrayHist( img ):
# 计算灰度直方图
height, width = img.shape[:2]
grayHist = np.zeros([256], np.uint64)
for i in range(height):
for j in range(width):
grayHist[img[i][j]] += 1
return grayHist
if __name__ == '__main__':
#读取原始图像
img = cv2.imread( 'test.jpg', cv2.IMREAD_GRAYSCALE )
#计算原图灰度直方图
origin_histogram = Origin_histogram( img )
#直方图均衡化
new_img = equalization_histogram( origin_histogram, img )
origin_grayHist = GrayHist(img)
equaliza_grayHist = GrayHist( new_img )
x = np.arange(256)
# 绘制灰度直方图
plt.figure( num = 1 )
plt.subplot( 2, 2, 1 )
plt.plot(x, origin_grayHist, 'r', linewidth=2, c='black')
plt.title("Origin")
plt.ylabel("number of pixels")
plt.subplot( 2, 2, 2 )
plt.plot(x, equaliza_grayHist, 'r', linewidth=2, c='black')
plt.title("Equalization")
plt.ylabel("number of pixels")
plt.subplot( 2, 2, 3 )
plt.imshow( img, cmap = plt.cm.gray )
plt.title( 'Origin' )
plt.subplot( 2, 2, 4 )
plt.imshow( new_img, cmap = plt.cm.gray )
plt.title( 'Equalization' )
plt.show()
直方图均衡法的应用:提高人脸识别精度
对获取图像亮度不同的问题进行预处理,提高精度。