数字图像处理简介

ide:pycharm
结果截图我没有放,因为太麻烦了。读者可以直接使用源代码运行它。
1.安装数字图像辅助模块 scikit-image 和 Matplotlib
2. 图像采样,通过不同的采样比率观察图像的情况
3. 图像量化,通过不同的量化灰度级观察图像的情况
4. 亮度和对比度调整操作
5. 图像三个颜色分离
6. 图像红色通道与蓝色通道互换
7. 图像算术加减运算
8. 不同伽马值情况下的幂次变换
9. 颜色直方图
10. 直方图均衡化

图像采样,通过不同的采样率观察图像的情况

from skimage import data
from matplotlib import pyplot as plt
import numpy as np

image = data.coffee()
print(image.shape)
print(type(image))
ratio = 20
image1 = np.zeros((int(image.shape[0] / ratio), int(image.shape[1] / ratio), image.shape[2]), dtype='int 32')
for i in range(image1.shape[0]):
    for j in range(image1.shape[1]):
        for k in range(image1.shape[2]):
            delta = image[i * ratio:(i + 1) * ratio, j * ratio:(j + 1) * ratio, k]
            image[i, j, k] = np.mean(delta)
plt.imshow(image1)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jGZ9bh3n-1647241786551)(D:\picture\Figure_1-2.png)]

图像量化,通过不同的量化灰度等级观察图像的情况

from skimage import data
from matplotlib import pyplot as plt

image = data.coffee()
ratio = 128  # 设置量化比率
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        for k in range(image.shape[2]):
            # 对图像中的每个像素进行量化
            image[i][j][k] = int(image[i][j][k] / ratio) * ratio
plt.imshow(image)
plt.show()

亮度和对比度调整操作

from skimage import data
from matplotlib import pyplot as plt
import numpy as np  # 导入所需的类包


def change_alpha(im, a):
    im_changed = np.zeros(shape=im.shape, dtype='uint8')
    for i in range(im.shape[0]):
        for j in range(im.shape[1]):
            for k in range(im.shape[2]):
                if im[i, j, k] * a > 255:
                    im_changed[i, j, k] = 255
                elif im[i, j, k] * a < 0:
                    im_changed[i, j, k] = 0
                else:
                    im_changed[i, j, k] = im[i, j, k] * a
    return im_changed


if __name__ == '__main__':
    image = data.coffee();
    image1 = change_alpha(image, 10)
    plt.imshow(image1)
    plt.show()

图像三色分离

from skimage import data, io
from matplotlib import pyplot as plt

image = data.coffee()
image_r = image[:, :, 0]
image_g = image[:, :, 1]
image_b = image[:, :, 2]
plt.subplot(2, 2, 1)
io.imshow(image)
plt.subplot(2, 2, 2)
io.imshow(image_r)
plt.subplot(2, 2, 3)
io.imshow(image_g)
plt.subplot(2, 2, 4)
io.imshow(image_b)
plt.show()

交换图像的红色和蓝色通道

from skimage import data, io
from matplotlib import pyplot as plt

image = data.coffee()
image_r = image[:, :, 0]
image_g = image[:, :, 1]
image_b = image[:, :, 2]
temp = image_r
image_r = image_b
image_b = temp
image[:, :, 0] = image_r
image[:, :, 2] = image_b
plt.imshow(image)
plt.show()

图像算术加减法

from matplotlib.font_manager import FontProperties

front_set = FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=12)
from skimage import data
from matplotlib import pyplot as plt

moon = data.moon()
camera = data.camera()
image_minus = moon - camera
image_plus = moon + camera
plt.set_cmap(cmap='gray')
plt.subplot(2, 2, 1)
plt.title('月亮图像', fontProperties=front_set)
plt.imshow(moon)
plt.subplot(2, 2, 2)
plt.title('摄影师图像', fontProperties=front_set)
plt.imshow(camera)
plt.subplot(2, 2, 3)
plt.title('月亮加摄影师图像', fontProperties=front_set)
plt.imshow(image_plus)
plt.subplot(2, 2, 4)
plt.title('月亮减摄影师图像', fontProperties=front_set)
plt.imshow(image_minus)
plt.show()

不同伽玛值的功率变换

from skimage import data, io, exposure
from matplotlib import pyplot as plt


image = data.coffee()
image_1 = exposure.adjust_gamma(image, 0.2)
image_2 = exposure.adjust_gamma(image, 0.67)
image_3 = exposure.adjust_gamma(image, 25)
plt.subplot(2, 2, 1)
plt.title('gamma=1')
io.imshow(image)
plt.subplot(2, 2, 2)
plt.title('gamma=0.2')
io.imshow(image_1)
plt.subplot(2, 2, 3)
plt.title('gamma=0.67')
io.imshow(image_2)
plt.subplot(2, 2, 4)
plt.title('gamma=25')
io.imshow(image_3)
plt.show()

颜色直方图

from skimage import data, io, exposure
from matplotlib import pyplot as plt

image = data.coffee()
image_r = exposure.histogram(image[:, :, 0], nbins=256)
image_g = exposure.histogram(image[:, :, 1], nbins=256)
image_b = exposure.histogram(image[:, :, 2], nbins=256)
plt.subplot(2, 2, 1)
plt.title('coffee')
io.imshow(image)
plt.subplot(2, 2, 2)
plt.title('R hist')
plt.hist(image_r, bins=256, edgecolor='None', facecolor='red')
plt.subplot(2, 2, 3)
plt.title('G hist')
plt.hist(image_g, bins=256, edgecolor='None', facecolor='red')
plt.subplot(2, 2, 4)
plt.title('B hist')
plt.hist(image_r, bins=256, edgecolor='None', facecolor='red')
plt.show()

直方图均衡

import matplotlib.pyplot as plt
from skimage import data, io, exposure
from matplotlib import pyplot as pl


img = data.moon()
plt.figure("hist", figsize=(8, 8))
arr = img.flatten()
plt.subplot(221)
plt.imshow(img, plt.cm.gray)
plt.subplot(222)
plt.hist(arr, bins=256, normed=1, edgecolor='None', facecolor='red')
img1 = exposure.equalize_hist(img)
arr1 = img1.flatten()
plt.subplot(223)
plt.imshow(img1, plt.cm.gray)
plt.subplot(224)
plt.hist(arr1, bins=256, normed=1, edgecolor='None', facecolor='red')
plt.show()

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年3月15日 下午3:31
下一篇 2022年3月15日 下午3:50

相关推荐