Python编译环境下实现图像的变换

前言

首先确保你的电脑已安装pycharm软件,我们在Pycharm平台编译自己的python语句,第一次发博客,纯纯的小白,请各位读者朋友手下留情不要踩啊,若有任何问题欢迎评论区留言,作者看到一定及时回复。

一、反色变换

图像反色变换 output = L - input
反色变换代码如下

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

def set_chinese():     #中文显示工具函数
    import matplotlib
    matplotlib.rcParams['font.sans-serif'] = ['SimHei']
    matplotlib.rcParams['axes.unicode_minus'] = False

def image_inverse(input):
    value_max = np.max(input)    #求输入图像最大灰度值,对应公式中的“L"
    output = value_max - input
    return output

if __name__ == '__main__':
    set_chinese()
    gray_img = np.asarray(Image.open('./00.jfif').convert('L'))
    inv_img = image_inverse(gray_img)

    fig = plt.figure()
    ax1 = fig.add_subplot(121)
    ax1.set_title('原图')
    ax1.imshow(gray_img, cmap='gray', vmin=0, vmax=255)

    ax2 = fig.add_subplot(122)
    ax2.set_title('反转变换结果')
    ax2.imshow(inv_img, cmap='gray', vmin=0, vmax=255)

    plt.show()

运行结果
图1 图像的反色变换

二、对数变换

假设原始图像的灰度取值范围是[x1,x2],(x2>x1>0),
则对数变换为:
output = log(1 + input) #1确保输出不为负无穷
程序代码如下:

import numpy as np
import matplotlib.pyplot as plt

def set_chinese():     #中文显示工具函数
   import matplotlib
   matplotlib.rcParams['font.sans-serif'] = ['SimHei']
   matplotlib.rcParams['axes.unicode_minus'] = False

def image_log(input):
   return np.log(1 + input)

if __name__ == '__main__':
   set_chinese() #调用此函数,窗口就可以正确显示中文

   input = np.array([[10,150],
                     [250,25500]])

   output = image_log(input)
   print(output)

   fig = plt.figure()
   ax1 = fig.add_subplot(121)
   ax1.set_title('对数变换前',fontsize=12)
   ax1.imshow(input, cmap='gray', vmin=0, vmax=25500)

   ax2 = fig.add_subplot(122)
   ax2.set_title('对数变换后', fontsize=12)
   ax2.imshow(output, cmap='gray')

   plt.show()

运行结果
图2 对矩阵实现对数变换

三、伽马变换

伽马变换又称幂律变换,因为他使用幂函数来操作输入图像。
实验程序如下:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

def set_chinese():     #中文显示工具函数
    import matplotlib
    print("[INFO] matplotlib版本为: %s" % matplotlib.__version__)
    matplotlib.rcParams['font.sans-serif'] = ['FangSong']
    matplotlib.rcParams['axes.unicode_minus'] = False

def gamma_trans(input,gamma=2,eps=0 ):
    return 255. * (((input + eps)/255.) ** gamma)

def update_gamma(val):
    gamma = slider1.val
    output = gamma_trans(input_arr, gamma=gamma, eps=0.5)
    print("--------\n", output)
    ax1.set_title("伽马变换后,gamma= " + str(gamma))
    ax1.imshow(output, cmap='gray', vmin=0, vmax=255)

if __name__ == '__main__':

    set_chinese() #调用此函数,窗口就可以正确显示中文

    input_arr = np.array( [ [0, 50, 100, 150],
                            [0, 50, 100, 150],
                            [0, 50, 100, 150]] )

    fig = plt.figure()
    ax0 = fig.add_subplot(121)
    ax0.set_title('输入矩阵')
    ax0.imshow(input_arr, cmap='gray', vmin=0, vmax=255)

    ax1 = fig.add_subplot(122)


    plt.subplots_adjust(bottom=0.3)
    s1 = plt.axes([0.25, 0.1, 0.55, 0.03], facecolor='lightgoldenrodyellow')
    slider1 = Slider(s1, '参数gamma', 0.0, 2.0,
                     valfmt='%.f', valinit=1.0, valstep=0.1)
    slider1.on_changed(update_gamma)
    slider1.reset()
    slider1.set_val(1)

    plt.show()

运行结果
图3 对矩阵实现对数变换

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CF996a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值