pythonpictureanalysis/GammaCorrection.py

51 lines
1.2 KiB
Python
Raw Permalink Normal View History

2022-07-26 21:27:11 +08:00
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 11:41
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : GammaCorrection.py
# @Software: PyCharm
'''
gamma矫正公式
2022-07-27 20:41:38 +08:00
f(x)=x^γ
2022-07-26 21:27:11 +08:00
即输出是输入的幂函数指数为γ
'''
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
2022-07-27 20:41:38 +08:00
# 文件保存的地址及文件夹
2022-07-26 21:27:11 +08:00
root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
2022-07-27 20:41:38 +08:00
# 定义伽马矫正函数
def gammaCorrect(filePath, val):
'''
:param filePath: 图片路径
:param val: 伽马值
:return: 图片
'''
# 获取图片
2022-07-26 21:27:11 +08:00
im = Image.open(filePath)
2022-07-27 20:41:38 +08:00
img = np.array(im) # 图片转矩阵
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
# 伽马函数使用
img1 = np.power(img / float(np.max(img)), 1 / 1.5) # gamma值为1/1.5
img2 = np.power(img / float(np.max(img)), val) # 自定义gamma值
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
# 多图合并
2022-07-26 21:27:11 +08:00
plt.subplot(131)
plt.imshow(img)
2022-07-27 20:41:38 +08:00
plt.title("origin") # 展示原图
2022-07-26 21:27:11 +08:00
plt.subplot(132)
plt.imshow(img1)
2022-07-27 20:41:38 +08:00
plt.title("gammar=1/1.5") # 展示gamma =1/1.5
2022-07-26 21:27:11 +08:00
plt.subplot(133)
plt.imshow(img2)
2022-07-27 20:41:38 +08:00
plt.title("user-defined") # 展示自定义 gammat Image
2022-07-26 21:27:11 +08:00
plt.show()
2022-07-27 20:41:38 +08:00
# 图片保存
2022-07-26 21:27:11 +08:00
Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg')
return img2