图像反转

顾名思义,图像反转即反转图像灰度级,可以得到等效的照片底片

设一幅图片的灰度级为[0,L-1],则反转图像公式如下:

                                                                                      s=L-1-r

s为目标图像的像素点的像素值,r为原图像像素点的像素值

下面使用Python实现图像反转:

使用的图像数据为:

导入待使用的第三方库:

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

读取图片,并查看图片大小和通道数:

img = Image.open('灰原.jpg')
np.shape(img)
(300, 533, 3)

原图像大小为300*533像素的,  RGB三通道彩色图片

可视化:

plt.axis('off')
plt.imshow(img)
plt.show()

 

将图像数据转换为numpy数组:

img_data = np.array(img)

 图像反转函数:

def Image_rollovers(img, func):
    img_data = np.array(img)
    a = np.shape(img_data)
    new_img = []
    for i in range(a[0]):
        new_row = []
        for j in range(a[1]):
            data = list(img_data[i][j])
            new_data = func(data)
            new_row.append(np.array(new_data))
        new_img.append(np.array(new_row))
    return new_img
def rollovers(data):
    new_data = []
    for k in data:
        new_data.append(int(255-k))
    return new_data

 调用函数并查看结果:

new_img = Image_rollovers(img, rollovers)
new_img = np.array(new_img)
new_img = Image.fromarray(new_img.astype('uint8')).convert('RGB')
plt.figure(figsize=(72,128))
plt.subplot(121)
plt.axis('off')
gray1 = new_img.convert('L')
plt.imshow(gray1, cmap='gray')
plt.subplot(122)
plt.axis('off')
plt.imshow(img, cmap='gray')
plt.show()

 

 左侧为目标图像,左侧为原图像

 

 

 

 

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猫猫虫(——)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值