pythonpictureanalysis/GammaCorrection.py
2022-07-27 20:41:38 +08:00

51 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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