脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 图像增强算法实现详解

python 图像增强算法实现详解

2021-08-28 00:38Wade_whl Python

这篇文章主要介绍了python 图像增强算法实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

使用python编写了共六种图像增强算法:

1)基于直方图均衡化
2)基于拉普拉斯算子
3)基于对数变换
4)基于伽马变换
5)限制对比度自适应直方图均衡化:clahe
6)retinex-ssr
7)retinex-msr其中,6和7属于同一种下的变化。
将每种方法编写成一个函数,封装,可以直接在主函数中调用。
采用同一幅图进行效果对比。

图像增强的效果为:

直方图均衡化:对比度较低的图像适合使用直方图均衡化方法来增强图像细节
拉普拉斯算子可以增强局部的图像对比度
log对数变换对于整体对比度偏低并且灰度值偏低的图像增强效果较好
伽马变换对于图像对比度偏低,并且整体亮度值偏高(对于相机过曝)情况下的图像增强效果明显
clahe和retinex的效果均较好

python代码为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 图像增强算法,图像锐化算法
# 1)基于直方图均衡化 2)基于拉普拉斯算子 3)基于对数变换 4)基于伽马变换 5)clahe 6)retinex-ssr 7)retinex-msr
# 其中,基于拉普拉斯算子的图像增强为利用空域卷积运算实现滤波
# 基于同一图像对比增强效果
# 直方图均衡化:对比度较低的图像适合使用直方图均衡化方法来增强图像细节
# 拉普拉斯算子可以增强局部的图像对比度
# log对数变换对于整体对比度偏低并且灰度值偏低的图像增强效果较好
# 伽马变换对于图像对比度偏低,并且整体亮度值偏高(对于相机过曝)情况下的图像增强效果明显
 
import cv2
import numpy as np
import matplotlib.pyplot as plt
 
 
# 直方图均衡增强
def hist(image):
  r, g, b = cv2.split(image)
  r1 = cv2.equalizehist(r)
  g1 = cv2.equalizehist(g)
  b1 = cv2.equalizehist(b)
  image_equal_clo = cv2.merge([r1, g1, b1])
  return image_equal_clo
 
 
# 拉普拉斯算子
def laplacian(image):
  kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
  image_lap = cv2.filter2d(image, cv2.cv_8uc3, kernel)
  return image_lap
 
 
# 对数变换
def log(image):
  image_log = np.uint8(np.log(np.array(image) + 1))
  cv2.normalize(image_log, image_log, 0, 255, cv2.norm_minmax)
  # 转换成8bit图像显示
  cv2.convertscaleabs(image_log, image_log)
  return image_log
 
 
# 伽马变换
def gamma(image):
  fgamma = 2
  image_gamma = np.uint8(np.power((np.array(image) / 255.0), fgamma) * 255.0)
  cv2.normalize(image_gamma, image_gamma, 0, 255, cv2.norm_minmax)
  cv2.convertscaleabs(image_gamma, image_gamma)
  return image_gamma
 
 
# 限制对比度自适应直方图均衡化clahe
def clahe(image):
  b, g, r = cv2.split(image)
  clahe = cv2.createclahe(cliplimit=2.0, tilegridsize=(8, 8))
  b = clahe.apply(b)
  g = clahe.apply(g)
  r = clahe.apply(r)
  image_clahe = cv2.merge([b, g, r])
  return image_clahe
 
 
def replacezeroes(data):
  min_nonzero = min(data[np.nonzero(data)])
  data[data == 0] = min_nonzero
  return data
 
 
# retinex ssr
def ssr(src_img, size):
  l_blur = cv2.gaussianblur(src_img, (size, size), 0)
  img = replacezeroes(src_img)
  l_blur = replacezeroes(l_blur)
 
  dst_img = cv2.log(img/255.0)
  dst_lblur = cv2.log(l_blur/255.0)
  dst_ixl = cv2.multiply(dst_img, dst_lblur)
  log_r = cv2.subtract(dst_img, dst_ixl)
 
  dst_r = cv2.normalize(log_r,none, 0, 255, cv2.norm_minmax)
  log_uint8 = cv2.convertscaleabs(dst_r)
  return log_uint8
 
 
def ssr_image(image):
  size = 3
  b_gray, g_gray, r_gray = cv2.split(image)
  b_gray = ssr(b_gray, size)
  g_gray = ssr(g_gray, size)
  r_gray = ssr(r_gray, size)
  result = cv2.merge([b_gray, g_gray, r_gray])
  return result
 
 
# retinex mmr
def msr(img, scales):
  weight = 1 / 3.0
  scales_size = len(scales)
  h, w = img.shape[:2]
  log_r = np.zeros((h, w), dtype=np.float32)
 
  for i in range(scales_size):
    img = replacezeroes(img)
    l_blur = cv2.gaussianblur(img, (scales[i], scales[i]), 0)
    l_blur = replacezeroes(l_blur)
    dst_img = cv2.log(img/255.0)
    dst_lblur = cv2.log(l_blur/255.0)
    dst_ixl = cv2.multiply(dst_img, dst_lblur)
    log_r += weight * cv2.subtract(dst_img, dst_ixl)
 
  dst_r = cv2.normalize(log_r,none, 0, 255, cv2.norm_minmax)
  log_uint8 = cv2.convertscaleabs(dst_r)
  return log_uint8
 
 
def msr_image(image):
  scales = [15, 101, 301] # [3,5,9]
  b_gray, g_gray, r_gray = cv2.split(image)
  b_gray = msr(b_gray, scales)
  g_gray = msr(g_gray, scales)
  r_gray = msr(r_gray, scales)
  result = cv2.merge([b_gray, g_gray, r_gray])
  return result
 
 
if __name__ == "__main__":
  image = cv2.imread("example.jpg")
  image_gray = cv2.cvtcolor(image, cv2.color_bgr2gray)
 
  plt.subplot(4, 2, 1)
  plt.imshow(image)
  plt.axis('off')
  plt.title('offical')
 
  # 直方图均衡增强
  image_equal_clo = hist(image)
 
  plt.subplot(4, 2, 2)
  plt.imshow(image_equal_clo)
  plt.axis('off')
  plt.title('equal_enhance')
 
  # 拉普拉斯算法增强
  image_lap = laplacian(image)
 
  plt.subplot(4, 2, 3)
  plt.imshow(image_lap)
  plt.axis('off')
  plt.title('laplacian_enhance')
 
  # log对象算法增强
  image_log = log(image)
 
  plt.subplot(4, 2, 4)
  plt.imshow(image_log)
  plt.axis('off')
  plt.title('log_enhance')
 
  # 伽马变换
  image_gamma = gamma(image)
 
  plt.subplot(4, 2, 5)
  plt.imshow(image_gamma)
  plt.axis('off')
  plt.title('gamma_enhance')
 
  # clahe
  image_clahe = clahe(image)
 
  plt.subplot(4, 2, 6)
  plt.imshow(image_clahe)
  plt.axis('off')
  plt.title('clahe')
 
  # retinex_ssr
  image_ssr = ssr_image(image)
 
  plt.subplot(4, 2, 7)
  plt.imshow(image_ssr)
  plt.axis('off')
  plt.title('ssr')
 
  # retinex_msr
  image_msr = msr_image(image)
 
  plt.subplot(4, 2, 8)
  plt.imshow(image_msr)
  plt.axis('off')
  plt.title('msr')
 
  plt.show()

增强效果如下图所示:

python 图像增强算法实现详解

到此这篇关于python 图像增强算法实现详解的文章就介绍到这了,更多相关python 图像增强算法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Wadewhl/article/details/112918863

延伸 · 阅读

精彩推荐