数字图像处理实验(一,图像增强)(python实现)

实验一

完整代码:https://github.com/Arancew/digtal-image-processing

  1. 图像反转实验,255-像素值
  2. 将彩色图像变换成灰度图像
  3. 直方图均衡化实验
  4. 线性对比度展宽实验
  5. 灰级窗实验
1.图像反转实验,255-像素值
import matplotlib.pyplot as plt

plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/1.1.jpg')  # 读取图片
plt.subplot(121),plt.title("1.原图"), plt.axis('off')
plt.imshow(img, cmap="gray")
new_img = 255 - img
plt.subplot(122),plt.title("2.灰度值反转后"), plt.axis('off')
plt.imshow(new_img, cmap="gray")
plt.show()

在这里插入图片描述

2.将彩色图像变换成灰度图像
import matplotlib.pyplot as plt
import numpy as np

# 读取图片,设置中文
plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/1.3.jpg')
plt.subplot(121), plt.title("1.原图"), plt.axis('off')
plt.imshow(img)
# print(img.shape[:2])  #获取像素的行数和列数
new_img = np.ones(img.shape[:2], dtype=int)


# Y= R*0.299+G*0.587+B*0.114,获得新图
for i in range(img.shape[0]):
   for j in range(img.shape[1]):
       new_img[i][j] = img[i][j][0] * 0.299 + img[i][j][1] * 0.587 + img[i][j][2] * 0.114
plt.subplot(122), plt.title("2.转化为灰度值后"), plt.axis('off')
plt.imshow(new_img, cmap="gray")
plt.show()

在这里插入图片描述

3.直方图均衡化实验
import matplotlib.pyplot as plt
import numpy as np
import cv2

# 读取图片,设置中文
plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/1.2.jpg')
plt.subplot(121), plt.title("1.原图"), plt.axis('off')
plt.imshow(img,cmap="gray")



new_img=cv2.equalizeHist(img)
plt.subplot(122), plt.title("2.直方图均衡化后"), plt.axis('off')
plt.imshow(new_img, cmap="gray")
plt.show()

在这里插入图片描述

4.线性对比度展宽实验
import matplotlib.pyplot as plt
import numpy as np
import cv2

# 读取图片,设置中文
plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/1.2.jpg')
plt.subplot(221), plt.title("1.原图"), plt.axis('off')
plt.imshow(img, cmap="gray")

# 统计下像素值
# 利用np初始化数组
bar = np.zeros(256, dtype=int)
for i in img:
    for j in i:
        bar[j] += 1

# 找到像素分布的范围
# flag=0
# for j,i in enumerate(bar):
#     if i!=0 and flag==0:
#         print(j)
#         flag=1
#     if i==0 and flag==1:
#         print(j)
#         break

# 选择的点a(50,25),点b(125,225)
for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        if img[i][j] <= 80:
            img[i][j] *= 0.3125
        elif img[i][j] <= 125:
            img[i][j] = 40 * (img[i][j] - 80)/9 + 25
        else:
            img[i][j] = (img[i][j] - 125) * 3 / 13 + 225
plt.subplot(223), plt.plot(bar), plt.title('原始直方图')
bar = np.zeros(256, dtype=int)
for i in img:
    for j in i:
        bar[j] += 1
plt.subplot(224), plt.plot(bar), plt.title('线性展宽后的直方图')
plt.subplot(222), plt.title("2.线性展宽后"), plt.axis('off')
plt.imshow(img, cmap="gray")
plt.show()

在这里插入图片描述

5.灰级窗实验
import matplotlib.pyplot as plt
import numpy as np
import cv2

# 读取图片,设置中文
plt.rc("font", family='Microsoft YaHei')
img = plt.imread('../img/1.2.jpg')
plt.subplot(221), plt.title("1.原图"), plt.axis('off')
plt.imshow(img, cmap="gray")

# 统计下像素值
# 利用np初始化数组
bar = np.zeros(256, dtype=int)
for i in img:
    for j in i:
        bar[j] += 1

# 找到像素分布的范围和点a,b
flag = 0
ax = 0
ay = 0
bx = 0
by = 0
for j, i in enumerate(bar):
    if i != 0 and flag == 0:
        ax = j
        ay = i
        flag = 1
    if i == 0 and flag == 1:
        bx = j
        by = i
        break

for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        if img[i][j] <= ax:
            img[i][j] = 0
        elif img[i][j] <= bx:
            img[i][j] = (by - ay) / (bx - ax) * (img[i][j] - ax) + ay
        else:
            img[i][j] = 0
plt.subplot(223), plt.plot(bar), plt.title('原始直方图')
bar = np.zeros(256, dtype=int)
for i in img:
    for j in i:
        bar[j] += 1
plt.subplot(224), plt.plot(bar), plt.title('灰度窗后的直方图')
plt.subplot(222), plt.title("2.灰度窗后"), plt.axis('off')
plt.imshow(img, cmap="gray")
plt.show()

在这里插入图片描述

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值