数字图处理测试(第三章)

本文通过具体实例介绍了图像处理中的几种常见变换方法,包括图像反转、对数变换和伽马变换,并展示了灰度变换函数的应用,以及如何实现比特平面分层和滤波器效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

书上习题和代码测试

图像反转、对数变换、伽马变换

直接上代码:

# -*- coding:utf-8 -*-

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

img = np.array(Image.open('11.tif').convert('L'))

fig = plt.figure()

ax1 = fig.add_subplot(2, 2, 1)
plt.gray()
plt.imshow(img)

#图像翻转
L = np.tile(99, (571, 482))
ax2 = fig.add_subplot(2, 2, 2)
s = L - img
plt.gray()
plt.imshow(s)

#对数变换
c1 = 1
s1 = c1*np.log(1 + img)
ax3 = fig.add_subplot(2, 2, 3)
plt.gray()
plt.imshow(s1)

#伽马变换
c2 = 1
r = 0.4
s2 = c2 * img**r
ax4 = fig.add_subplot(2, 2 ,4)
plt.gray()
plt.imshow(s2)

plt.show()

结果为:
这里写图片描述
第一个为原始图像,可在官网下载。右上角为图像反转,用于增加图像暗区域中的白色或灰色细节。左下角为对数变换,用于扩展图像中的暗像素的值,同时压缩更高的灰度值。右下角为伽马变换,用于增强对比度。

习题测试

3.1 灰度为[C, L-1]的单调变换函数

#s = r/255.0 * (L - 1 - C) + C
#3.1
img = np.array(Image.open('lena.tiff').convert('L'))
fig = plt.figure()

C = 20
L = 50
s = img/255.0*(L - 1 - C) + C
ax1 = fig.add_subplot(2, 2, 1)
plt.imshow(s)

测试结果:
这里写图片描述

3.3 实现3.2(a)
当alpha等于0.001时:

r = np.arange(0, 255, 1)
alpha = 0.0001
s = np.exp(-alpha * r**2)
ax2 = fig.add_subplot(2, 2, 2)
plt.plot(r, s)

结果:
这里写图片描述

3.4 实现比特平面分层
第八层:[0, 127]灰度为0,[128, 255]灰度为1
第七层:[64, 127]为1,其余为0
其他的类似。代码如下:

img = np.array(Image.open('3.4.tif'))
fig = plt.figure()
fig.suptitle('3.4 test')

#original
ax1 = fig.add_subplot(3, 3, 1)
plt.imshow(img)

#8
ax2 = fig.add_subplot(3, 3, 2)
s8 = np.where(img > 127, 1, 0)
plt.imshow(s8)

#7
ax3 = fig.add_subplot(3, 3, 3)
s7 = np.where(img < 64, 0, np.where(img > 127, 0, 1))
plt.imshow(s7)

#6
ax4 = fig.add_subplot(3, 3, 4)
s6 = np.where(img < 32, 0, np.where(img > 63, 0, 1))
plt.imshow(s6)

#5
ax5 = fig.add_subplot(3, 3, 5)
s5 = np.where(img < 16, 0, np.where(img > 31, 0, 1))
plt.imshow(s5)

#4
ax6 = fig.add_subplot(3, 3, 6)
s4 = np.where(img < 8, 0, np.where(img > 15, 0, 1))
plt.imshow(s4)

#3
ax7 = fig.add_subplot(3, 3, 7)
s3 = np.where(img < 4, 0, np.where(img > 7, 0, 1))
plt.imshow(s3)

#2
ax8 = fig.add_subplot(3, 3, 8)
s2 = np.where(img < 2, 0, np.where(img > 3, 0, 1))
plt.imshow(s2)

#1
ax9 = fig.add_subplot(3, 3, 9)
s1 = np.where(img < 1, 0, np.where(img > 1, 0, 1))
plt.imshow(s1)

结果为:
这里写图片描述

还原过程为第n个平面的像素乘以常数 2n1 ,代码如下:

#重建
fig1 = plt.figure()
fig1.suptitle('bit rebuild')

rebuild87 = s8 * 2**(8 - 1) + s7 * 2**(7 - 1)
ax87 = fig1.add_subplot(1, 3, 1)
plt.imshow(rebuild87)

rebuild876 = s8 * 2**(8 - 1) + s7 * 2**(7 - 1) + s6 * 2**(6 - 1)
ax876 = fig1.add_subplot(1, 3, 2)
plt.imshow(rebuild876)

rebuild8765 = s8 * 2**(8 - 1) + s7 * 2**(7 - 1) + s6 * 2**(6 - 1) + s5 * 2**(5 - 1)
ax8765 = fig1.add_subplot(1, 3, 3)
plt.imshow(rebuild8765)

结果:
这里写图片描述

3.22滤波器
首先实现图3.44的例子,代码如下:

img = np.array(Image.open('3.22.tif'))
fig = plt.figure()
fig.suptitle('3.22 test')

ax1 = fig.add_subplot(1, 3, 1)
plt.imshow(img)

#扩充
imgCov = np.zeros((528 + 14, 485 + 14))
imgCov[6:534, 6:491] = img

#卷积核
kernel = 1.0/(15.0*15.0)*np.ones((15, 15))
img1 = np.zeros_like(img)

#卷积
for i in np.arange(528):
    for j in np.arange(485):
        img1[i,j] = np.sum(imgCov[i:i+15, j:j+15]*kernel)

ax2 = fig.add_subplot(1, 3, 2)
plt.imshow(img1)

#阈值化
img2 = np.where(img1 > 100, 255, 0)
ax3 = fig.add_subplot(1, 3, 3)
plt.imshow(img2)

注意255.0,不然会自动转换为int类型。其中主要是原始图像的扩充和卷积的计算,其中卷积的计算使用了2个for循环(不来不想用的,但是不会)、
结果如下:
这里写图片描述

3.22要求为 110 灰度,结果为:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值