1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波1 - 灰度变换和空间滤波

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波1 - 灰度变换和空间滤波

时间:2021-01-19 18:07:21

相关推荐

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波1 - 灰度变换和空间滤波

这里写目录标题

本节的目标背景灰度变换和空间滤波基础

本节的目标

了解空间域图像处理的意义,以及它与变换域图像处理的区别熟悉灰度变换所有的主要技术了解直方图的意义以及如何操作直方图来增强图像了解空间滤波的原理

import sysimport numpy as npimport cv2import matplotlib import matplotlib.pyplot as pltimport PILfrom PIL import Imageprint(f"Python version: {sys.version}")print(f"Numpy version: {np.__version__}")print(f"Opencv version: {cv2.__version__}")print(f"Matplotlib version: {matplotlib.__version__}")print(f"Pillow version: {PIL.__version__}")

Python version: 3.6.12 |Anaconda, Inc.| (default, Sep 9 , 00:29:25) [MSC v.1916 64 bit (AMD64)]Numpy version: 1.16.6Opencv version: 3.4.1Matplotlib version: 3.3.2Pillow version: 8.0.1

def normalize(mask):return (mask - mask.min()) / (mask.max() - mask.min() + 1e-5)

背景

灰度变换和空间滤波基础

g(x,y)=T[f(x,y)](3.1)g(x, y) = T[f(x, y)] \tag{3.1} g(x,y)=T[f(x,y)](3.1)

式中f(x,y)f(x, y)f(x,y)是输入图像, g(x,y)g(x, y)g(x,y)是输出图像,TTT是在点(x,y)(x, y)(x,y)的一个邻域上定义的针对f的算子。

最小的邻域大小为1×11\times 11×1

则式(3.1)中的TTT称为灰度(也称灰度级或映射)变换函数,简写为如下:

s=T(r)(3.2)s=T(r) \tag{3.2}s=T(r)(3.2)

对比度拉伸

通过将kkk以下的灰度级变暗,并将高于kkk的灰度级变亮,产生比原图像对比度更高的一幅图像

阈值处理函数

小于kkk的处理为0,大于kkk的设置为1,产生一幅二级(二值)图像

# 显示一个图像的3x3邻域height, width = 18, 18img_ori = np.ones([height, width], dtype=np.float)# 图像3x3=9个像素赋了不同的值,以便更好的显示kernel_h, kernel_w = 3, 3img_kernel = np.zeros([kernel_h, kernel_w], dtype=np.float)for i in range(img_kernel.shape[0]):for j in range(img_kernel.shape[1]):img_kernel[i, j] = 0.3 + 0.1 * i + 0.1 * jimg_kernel[kernel_h//2, kernel_w//2] = 0.9img_ori[5:5+kernel_h, 12:12+kernel_w] = img_kernelfig = plt.figure(figsize=(7, 7), num='a')plt.matshow(img_ori, fignum='a', cmap='gray', vmin=0, vmax=1)plt.show()

为什么会把Sigmoid函数写在这里

从Sigmoid函数的图像曲线来看,与分段线性函数的曲线类似,所以在一定程度上可以用来代替对比度拉伸,这样就不需要输入太多的参数。当然,有时可能也得不到想要的结果,需要自己多做实验。

sigmoid函数也是神经网络用得比较多的一个激活函数。

def sigmoid(x, scale):"""simgoid fuction, return ndarray value [0, 1]param: input x: array like param: input scale: scale of the sigmoid fuction, if 1, then is original sigmoid fuction, if < 1, then the values between 0, 1will be less, if scale very low, then become a binary fuction; if > 1, then the values between 0, 1 will be more, if scalevery high then become a y = x"""y = 1 / (1 + np.exp(-x / scale))return y

# sigmoid fuction plotx = np.linspace(0, 10, 100)x1 = x - x.max() / 2 # Here shift the 0 to the x center, here is 5, so x1 = [-5, 5]t_stretch = sigmoid(x1, 1)t_binary = sigmoid(x1, 0.001)plt.figure(figsize=(10, 5))plt.subplot(121), plt.plot(x, t_stretch), plt.title('s=T(r)'), plt.ylabel('$s_0 = T(r_0)$', rotation=0)plt.xlabel('r'), plt.xticks([]), plt.yticks([])plt.subplot(122), plt.plot(x, t_binary), plt.title('s=T(r)'), plt.ylabel('$s_0 = T(r_0)$', rotation=0)plt.xlabel('r'), plt.xticks([]), plt.yticks([])plt.tight_layoutplt.show()

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波1 - 灰度变换和空间滤波基础 Sigmoid激活函数

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。