第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波5 - 分段线性变换 - 灰度级分层

灰度级分层
  • 二值图像

  • 将感兴趣范围内的所有灰显示为一个值(白色),而将其它灰度值显示为另一个值(黑色)

  • 其他灰度级不变

  • 使期望的灰度范围变量(或变暗),但保持图像中的其它灰度级不变

x = np.arange(0, 256)
y_1 = np.where(x, x >= 100, 0)
y_1 = np.where(y_1, x < 200, 0) * 150

a = 70
b = 150
y_2 = np.zeros_like(x)
for i in range(len(x)):
    if a <= x[i] <= b:
        y_2[i] = 220
    else:
        y_2[i] = x[i]
        
plt.figure(figsize=(10, 5))
plt.subplot(121), plt.plot(x, y_1), plt.ylim([-1, 255]), plt.xlim([0, 255])
plt.subplot(122), plt.plot(x, y_2), plt.ylim([1, 255]), plt.xlim([0, 255])
plt.show()

在这里插入图片描述

# 灰度分层
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0312(a)(kidney).tif')

a = 155

# binary image
img_transform = img.copy()
img_transform[img_transform < a] = 0
img_transform[img_transform >= a] = 255

# grayscale image
img_transform_2 = img.copy()
img_transform_2[img_transform_2 < a] = 0.

plt.figure(figsize=(18, 15))
plt.subplot(1, 3, 1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(1, 3, 2), plt.imshow(img_transform, cmap='gray'), plt.title(f'Binary Image')
plt.subplot(1, 3, 3), plt.imshow(img_transform_2, cmap='gray'), plt.title(f'Grayscale Image')
plt.tight_layout()
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jasneik

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

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

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

打赏作者

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

抵扣说明:

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

余额充值