程序代码
# coding: utf-8
'''
东南大学《数字图像处理》课程 作业4 - Gamma校正
09017227 卓旭 written with Python 3
本程序内灰度图像作为二维数组,存储顺序为[行][列],像素点坐标表示为img[x][y],坐标系为
O--------> [y axis]
|
|
V [x axis]
'''
import imageio
import numpy as np
import cv2 # OpenCV仅用于显示图片
IMAGE_PATH='./Img_GammaCorrection.bmp'
GAMMA=1/2.8
'''
读入灰度图像,转为二维numpy数组
'''
def readImage(imagePath):
return imageio.imread(imagePath)
'''
返回经过Gamma变换的图像
'''
def gammaCorrection(sourceImage, Gamma):
targetImage = np.zeros(sourceImage.shape, dtype=np.uint8)
# 归一化
normSourceImage = sourceImage / 255
for i in range(targetImage.shape[0]):
for j in range(targetImage.shape[1]):
targetImage[i][j] = np.uint8(pow(normSourceImage[i][j], Gamma) * 255)
return targetImage
if __name__ == '__main__':
sourceImage = readImage(IMAGE_PATH)
targetImage = gammaCorrection(sourceImage, GAMMA)
imageio.imsave("Gamma_result.bmp", targetImage)
cv2.imshow("test", targetImage)
cv2.waitKey(0)