栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数字图像处理实验(一,图像增强)(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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号