进入冈萨雷斯的第三章内容,并用python实现功能。我更改了代码源,之前找到太烂了,代码全是错、、现在使用的代码很清晰,功能也很全。
灰度拉伸, 也称对比度拉伸, 是一种简单的线性点运算 ,扩展图像的直方图使其充满整个灰度等级范围内.
代码如下(示例):
"""
Created by HenryMa on 2020/8/24
"""
__author__ = 'HenryMa'
import math
# from builtins import range, print
import numpy as np
import cv2
def contrastStretchTransform(image):
"""
灰度拉伸
定义: 灰度拉伸, 也称对比度拉伸, 是一种简单的线性点运算.
作用: 扩展图像的直方图, 使其充满整个灰度等级范围内.
公式: A = min[f(x, y)], 最小灰度级;
B = max[f(x, y)], 最大灰度级;
f(x, y)为输入图像, g(x, y)为输出图像.
缺点: 如果灰度图像中最小值A=0, 最大值B=255, 则图像没有什么改变.
"""
# 彩色图像
h, w, d = image.shape
new_img = np.zeros((h, w, d), dtype=np.float32)
A = image.min()
B = image.max()
print(A, B)
for i in range(h):
for j in range(w):
for k in range(d):
new_img[i, j, k] = 255.0 / (B - A) * (image[i, j, k] - A) + 0.5
# new_img = cv2.normalize(new_img, None, 0, 255, cv2.NORM_MINMAX)
# new_img = cv2.convertScaleAbs(new_img)
new_img=new_img.astype(np.uint8)
A1 = new_img.min()
B1 = new_img.max()
print(A1,B1)
return new_img
if __name__ == '__main__':
img = cv2.imread('../pic/beizi.png', 1)
contrast_img = contrastStretchTransform(img)
cv2.imshow('src img', img)
cv2.imshow('contrast img', contrast_img)
cv2.waitKey(0)
伽马变换可以很好地拉伸图像的对比度, 扩展灰度级。伽马大于1,图片会变暗;伽马值小于1,图片整体变亮
代码如下(示例):
"""
Created by HenryMa on 2020/8/24
"""
__author__ = 'HenryMa'
import math
from builtins import range, print
import numpy as np
import cv2
def gammaTransform(c, gamma, image):
# 彩色图像
"""
h, w, d = image.shape[0], image.shape[1], image.shape[2]
new_img = np.zeros((h, w, d), dtype=np.float32)
for i in range(h):
for j in range(w):
for k in range(d):
new_img[i, j, k] = c*math.pow(image[i, j, k], gamma)
cv2.normalize(new_img, new_img, 0, 255, cv2.NORM_MINMAX)
print(new_img)
new_img = cv2.convertScaleAbs(new_img)
print(new_img)
"""
# 灰度图
h, w = image.shape[0], image.shape[1]
new_img = np.zeros((h, w), dtype=np.float32)
for i in range(h):
for j in range(w):
new_img[i, j] = c * math.pow(image[i, j], gamma)
cv2.normalize(new_img, new_img, 0, 255, cv2.NORM_MINMAX)
new_img = cv2.convertScaleAbs(new_img)
return new_img
if __name__ == '__main__':
img = cv2.imread('../pic/Fig0309(a)(washed_out_aerial_image).tif', 0)
# gamma_img1 = gammaTransform(1, 0.6, img)
# gamma_img2 = gammaTransform(1, 0.4, img)
# gamma_img3 = gammaTransform(1, 0.3, img)
gamma_img4 = gammaTransform(1, 3.0, img)
gamma_img5 = gammaTransform(1, 4.0, img)
gamma_img6 = gammaTransform(1, 5.0, img)
cv2.imshow('origin', img)
# cv2.imshow('gamma_img1', gamma_img1)
# cv2.imshow('gamma_img2', gamma_img2)
# cv2.imshow('gamma_img3', gamma_img3)
cv2.imshow('gamma_img4', gamma_img4)
cv2.imshow('gamma_img5', gamma_img5)
cv2.imshow('gamma_img6', gamma_img6)
cv2.waitKey(0)
由于对数曲线在像素值较低的区域斜率大, 在像素值较高的区域斜率较小, 所以图像经过对数变换后, 较暗区域的对比度将有所提升. 可用于增强图像的暗部细节.
代码如下(示例):
"""
Created by HenryMa on 2020/8/24
"""
__author__ = 'HenryMa'
import cv2
import math
import numpy as np
from builtins import range, print
def logTransform(c, image):
# 3通道RGB
"""
h, w, d = image.shape[0], image.shape[1], image.shape[2]
new_img = np.zeros((h, w, d))
min = 255
max = 0
for i in range(h):
for j in range(w):
for k in range(d):
new_img[i, j, k] = c * (math.log(1.0 + image[i, j, k]))
# print(new_img)
new_img = cv2.normalize(new_img, new_img, 0, 255, cv2.NORM_MINMAX)/255.
print(new_img.max(), new_img.min())
print(new_img)
return new_img
"""
# 灰度图
h, w = img.shape[0], img.shape[1]
new_img = np.zeros((h, w))
for i in range(h):
for j in range(w):
new_img[i, j] = c * (math.log(1.0 + img[i, j]))
new_img = cv2.normalize(new_img, new_img, 0, 255, cv2.NORM_MINMAX)/255.
return new_img
if __name__ == '__main__':
img = cv2.imread('../pic/Fig0305(a)(DFT_no_log).tif', 0)
log_img = logTransform(1, img)
cv2.imshow('src_img', img)
cv2.imshow('log_img', log_img)
cv2.waitKey(0)