DIP-Introductory python tutorials for image processing(29-41)-Image Filtering
学习自 Youtube 博主 DigitalSreeni。
文字数:---
正文
Tutorial 29 -Basic image processing using scikit-image library
缩放
Rescale, resize, and downscale — skimage v0.19.2 docs
- Rescale 操作按给定的缩放因子调整图像的大小。缩放因子可以是单个浮点值,也可以是多个值——每个轴上都有一个。
- Resize 也有同样的作用,但允许指定输出图像的形状而不是缩放因子。
- Downscale 的目的是通过整数因子对 n 维图像进行下采样,使用作为函数参数的大小因子的每个块的元素的局部均值。
1 | |
1 | |
1 | |
<matplotlib.image.AxesImage at 0x212d8734a48>

1 | |
<matplotlib.image.AxesImage at 0x212db1f7ac8>

1 | |
<matplotlib.image.AxesImage at 0x212db2830c8>

1 | |
<matplotlib.image.AxesImage at 0x212db506788>

gaussian 模糊
1 | |
C:\Users\gzjzx\anaconda3\envs\wxpython37\lib\site-packages\skimage\_shared\utils.py:348: RuntimeWarning: Images with dimensions (M, N, 3) are interpreted as 2D+RGB by default. Use `multichannel=False` to interpret as 3D image with last dimension of length 3.
return func(*args, **kwargs)
<matplotlib.image.AxesImage at 0x212dbb52848>

sobel 边缘检测
1 | |
<matplotlib.image.AxesImage at 0x212dc0185c8>
## Tutorial 30 - Basic image processing using opencv in python
使用 OpenCV 操作下面这张图:

OpenCV 读取到的都是 BGR 颜色通道, 此时使用 matplotlib 显示会导致颜色发生变化
1 | |
<matplotlib.image.AxesImage at 0x1e3d6809370>

缩放
1 | |

1 | |
(400, 200, 3)
1 | |
Top left [254 0 0]
Top right [ 1 255 255]
Bottom left [ 1 255 0]
Bottom right [ 42 0 255]
分离颜色通道
1 | |
或
1 | |
1 | |

合并颜色通道
1 | |

Canny 边缘检测
1 | |

Tutorial 31 - Image filtering in python - Unsharp mask
Unsharp Mask(USM)锐化算法的的原理及其实现。_大熊背的博客-CSDN 博客_usm 锐化算法

原理
1 | |

unsharp_mask 函数
1 | |

Tutorial 32 - Image filtering in python - Gaussian denoising for noise reduction

1 | |

Tutorial 33 - Image filtering in python - Median filter for denoising images
- 中值滤波清理椒盐噪声

OpenCV
1 | |
skimage
1 | |
array([[0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0]], dtype=uint8)
1 | |
1 | |

Tutorial 34 - Image filtering in python - Bilateral filter for image denoising
双边滤波 - Bilateral Filter - 知乎 (zhihu.com)
OpenCV
1 | |

Skimage
视频上说很慢…好像确实挺慢
1 | |

Tutorial 35 - Image filtering in python - Non-local means -NLM- filter for image denoising
NLM 去噪算法_SongpingWang 的博客-CSDN 博客_nlm 去噪

OpenCV
1 | |
C:\Users\gzjzx\AppData\Local\Temp\ipykernel_11200\3289355486.py:7: FutureWarning: `multichannel` is a deprecated argument name for `estimate_sigma`. It will be removed in version 1.0. Please use `channel_axis` instead.
sigma_est = np.mean(estimate_sigma(img, multichannel=True))
1 | |
C:\Users\gzjzx\AppData\Local\Temp\ipykernel_11200\4037562389.py:1: FutureWarning: `multichannel` is a deprecated argument name for `denoise_nl_means`. It will be removed in version 1.0. Please use `channel_axis` instead.
denoise_img = denoise_nl_means(img, h=1.15 * sigma_est, fast_mode=True,
1 | |
Skimage
1 | |

Tutorial 36 - Image filtering in python - Total variation filter -TVF- for image denoising
如何理解全变分(Total Variation,TV)模型?- 知乎 (zhihu.com)
1 | |
1 | |
(array([3.26417e+05, 3.28804e+05, 2.18786e+05, 3.22133e+05, 2.09763e+05,
3.05641e+05, 1.95677e+05, 2.78984e+05, 1.75691e+05, 2.45767e+05,
2.25122e+05, 1.37699e+05, 1.89186e+05, 1.14683e+05, 1.55440e+05,
9.30000e+04, 1.23786e+05, 7.34830e+04, 9.66150e+04, 5.74190e+04,
7.61160e+04, 6.49060e+04, 3.82890e+04, 5.08860e+04, 2.99340e+04,
4.02660e+04, 2.39850e+04, 3.23250e+04, 1.93770e+04, 2.62200e+04,
2.38270e+04, 1.45560e+04, 1.99740e+04, 1.24720e+04, 1.74630e+04,
1.06820e+04, 1.50500e+04, 9.49200e+03, 1.31800e+04, 8.33400e+03,
1.13970e+04, 1.06840e+04, 6.63100e+03, 9.49100e+03, 5.84100e+03,
8.29500e+03, 5.25100e+03, 7.20200e+03, 4.53600e+03, 6.35800e+03,
5.76500e+03, 3.56300e+03, 4.89500e+03, 3.01000e+03, 4.29100e+03,
2.55100e+03, 3.53500e+03, 2.23300e+03, 3.10300e+03, 1.92100e+03,
2.55900e+03, 2.31500e+03, 1.38900e+03, 1.87300e+03, 1.18800e+03,
1.62500e+03, 9.85000e+02, 1.37700e+03, 8.49000e+02, 1.17400e+03,
1.06500e+03, 6.06000e+02, 8.26000e+02, 5.57000e+02, 7.52000e+02,
4.27000e+02, 5.95000e+02, 3.91000e+02, 4.85000e+02, 3.13000e+02,
4.03000e+02, 3.87000e+02, 2.18000e+02, 3.35000e+02, 1.88000e+02,
2.95000e+02, 1.65000e+02, 2.55000e+02, 1.36000e+02, 2.35000e+02,
1.82000e+02, 1.15000e+02, 1.68000e+02, 1.13000e+02, 1.63000e+02,
1.13000e+02, 1.63000e+02, 9.40000e+01, 1.32000e+02, 1.18000e+02]),
array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,
0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,
0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,
0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,
0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
0.99, 1. ]),
<BarContainer object of 100 artists>)

- denoise_tv_chambolle(image, weight=0.1, eps=0.0002, n_iter_max=200, multichannel=False)
- weight: The greater weight, the more denoising (at the expense of fidelity to input). 权重越大,去噪越多(以牺牲对输入的保真度为代价)。
- eps: Relative difference of the value of the cost function that determines the stop criterion. 决定停止准则的代价函数值的相对差值。
- n_iter_max: Max number of iterations used for optimization 用于优化的最大迭代次数
1 | |
C:\Users\gzjzx\AppData\Local\Temp\ipykernel_8228\1044631449.py:1: FutureWarning: `multichannel` is a deprecated argument name for `denoise_tv_chambolle`. It will be removed in version 1.0. Please use `channel_axis` instead.
denoise_img = denoise_tv_chambolle(img, weight=0.1, eps=0.0002, n_iter_max=200, multichannel=True)
1 | |

Tutorial 37 - Image filtering in python - Block matching and 3D filtering -BM3D- for image denoising

1 | |
Collecting bm3d
Downloading bm3d-3.0.9-py3-none-any.whl (8.4 MB)
---------------------------------------- 8.4/8.4 MB 2.3 MB/s eta 0:00:00
Requirement already satisfied: PyWavelets in c:\users\gzjzx\anaconda3\lib\site-packages (from bm3d) (1.3.0)
Requirement already satisfied: numpy in c:\users\gzjzx\anaconda3\lib\site-packages (from bm3d) (1.23.1)
Requirement already satisfied: scipy in c:\users\gzjzx\anaconda3\lib\site-packages (from bm3d) (1.9.0)
Installing collected packages: bm3d
Successfully installed bm3d-3.0.9
Note: you may need to restart the kernel to use updated packages.
1 | |
- bm3d library is not well documented yet, but looking into source code…Bm3d 库还没有很好的文档化,但是查看源代码…
- sigma_psd - noise standard deviation 噪声标准差
- stage_arg: Determines whether to perform hard-thresholding or Wiener filtering. 确定是执行硬阈值还是维纳过滤。
- stage_arg = BM3DStages.HARD_THRESHOLDING or BM3DStages.ALL_STAGES (slow but powerful)
- BM3DStages。HARD_THRESHOLDING 或 BM3DStagesall_stage(缓慢但强大)
All stages performs both hard thresholding and Wiener filtering. 所有阶段都执行硬阈值和维纳滤波。
1 | |

Tutorial 38 - Image filtering in python - Edge detection
Edge Detection filters: 边缘检测滤波器
- Roberts
- Apply a horizontal and vertical filter one after the other. 依次使用 Horizontal 和 Vertical 滤镜。
- Both filters are applied (convoluted) to the image. 两个过滤器都被应用到图像上(卷积)。
- Computes the sum of the squares of the differences between diagonally adjacent pixels. 计算对角线相邻像素之间的差的平方和。
- It highlights regions of high spatial which often correspond to edges. 它突出了高空间区域,通常对应于边缘。

- Sobel
- Very similar to Roberts excerpt with a operator. 与 Roberts 非常相似,只是使用了操作符

- Scharr
- Typically used to identify gradients along the x-axis (dx=1, dy=0) and y-axis(dx=0, dy=1) independently. 通常用于分别识别沿 x 轴(dx=1, dy=0)和 y 轴(dx=0, dy=1)的梯度。
- Performance is quite similar to Sobel filter. 其性能与 Sobel 滤波器非常相似。
- Farid
- Farid and Simoncelli propose to use a pair of kernels, one for interpolation and another for differentiation (similar to Sobel). Farid 和 Simoncelli 建议使用一对核,一个用于插值,另一个用于微分(类似于 Sobel)。
- Fixed size kernels: (interpolation) and (differentiation) 固定大小的内核:(插值)和(分化)
- Prewitt
- The Prewitt operator is similar to Sobel, excerpt for the operator values.
- Very fast, similar to Sobel.

1 | |

1 | |
1 | |

Tutorial 39 - Image filtering in python - Edge detection using Canny
- Multi stage algorithm for edge detection: 多阶段边缘检测算法
- Step 1: Noise reduction - typically uses Gaussian (but any denoising can be used) 降噪-通常使用高斯(但也可以使用任何降噪)
- Step 2: Gradient calculation - detect edges, typically along 4 directions, horizontal, vertical, and two diagonals. (e.g. use Sobel) 梯度计算-检测边缘,通常沿 4 个方向,水平,垂直和两个对角线。 (如使用 Sobel)
- Step 3: Non-maximum suppression - thin out edges by finding pixels with max value in the edge direction. 非最大抑制-通过在边缘方向上找到最大值像素来减少边缘。
- Step 4: Double threshold - determines potential edges by double thresholding to obtain strong, weak and irrelevant pixels for edges. 双阈值-通过双阈值确定潜在的边缘,获得边缘的强、弱和无关像素。
- Step 5: Edge tracking by hysteresis - covert weak edge pixels to strong based on neighboring pixels. 边缘跟踪的迟滞-隐藏弱边缘像素到强基于邻近像素。
1 | |

Tutorial 40 - What is Fourier transform and how is it relevant for image processing
Fourier Transform 傅里叶变换
-
Fourier transform breaks a function (signal) into an alternate representation (using sine and cosines)
傅里叶变换将一个函数(信号)分解成另一种表示形式(使用正弦和余弦)
-
In other words, Fourier Transform shows that any signal can be reconstructed by summing up individual sine waves of different frequencies.
换句话说,傅里叶变换表明,任何信号都可以通过将不同频率的单个正弦波相加来重构。
-

-
Continuous 连续傅里叶变换
-
Discrete 离散傅里叶变换
$x = \frac { 1 } { N } \Sigma ^ { N - 1 } _ { n = 0 } x ( { \color { Red } n } ) \cdot e ^ { - \frac { { \color {Blue}j _ {2 \pi } }kn } { N } } $
- : Input signal (pixel value)
- : Complex number
代码
- 创建一个正弦波
1 | |
1 | |
1 | |
<matplotlib.image.AxesImage at 0x246a404a700>

1 | |
<matplotlib.image.AxesImage at 0x246a542f1c0>

OpenCV
1 | |
1 | |

右边图中,每一个点:
1)它到中点的距离描述的是频率
2)中点到它的方向,是平面波的方向
3)那一点的灰度值描述的是它的幅值
1 | |

Tutorial 41 - Image filtering using Fourier transform in python
1 | |
- Output is a 2D complex array. 1st channel real and 2nd imaginary
- 输出是一个 2D 复数数组。第一通道是实通道,第二通道是虚通道
- For fft in opencv input image needs to be converted to float32
- 对于 fft 在 opencv 输入图像需要转换为 float32
1 | |
- Rearranges a Fourier transform X by shifting the zero-frequency component to the center of the array.
- 通过将零频率分量移到数组的中心来重新排列傅里叶变换 X。
- Otherwise it starts at the tope left corenr of the image (array)
- 否则它将从图像(数组)的左核心位置开始
1 | |
- Magnitude of the function is 20.log(abs(f))
- 函数的大小为 20.log(abs(f))
- For values that are 0 we may end up with indeterminate values for log.
- 对于 0 的值,我们可能会得到 log 的不确定值。
- So we can add 1 to the array to avoid seeing a warning.
- 因此,我们可以向数组中添加 1 以避免看到警告。
1 | |
-
使用傅里叶变换进行边缘检测
-
Circular HPF mask, center circle is 0, remaining all ones
- 圆形 HPF 掩码,中心圆为 0,其余均为 1
-
Can be used for edge detection because low frequencies at center are blocked and only high frequencies are allowed.
- 可以用于边缘检测,因为中心的低频被阻挡,只允许高频。
-
Edges are high frequency components.
- 边是高频分量。
-
Amplifies noise.
- 放大噪声。
1 | |
- apply mask and inverse DFT: Multiply fourier transformed image (values) with the mask values.
- 应用掩模和反 DFT:将傅里叶变换后的图像(值)与掩模值相乘。
1 | |
- Get the magnitude spectrum (only for plotting purposes)
- 获取幅度谱(仅用于绘图目的)
1 | |
C:\Users\gzjzx\AppData\Local\Temp\ipykernel_19060\199016683.py:1: RuntimeWarning: divide by zero encountered in log
fshift_mask_mag = 20 * np.log(cv2.magnitude(fshift[:, :, 0], fshift[:, :, 1]))
- Inverse shift to shift origin back to top left.
- 反向移位将原点移回左上角。
1 | |
- Inverse DFT to convert back to image domain from the frequency domain.
- 逆 DFT: 从频域转换回图像域。
- Will be complex numbers
- 会是复数
1 | |
- Magnitude spectrum of the image domain
- 图像域的幅度谱
1 | |
- 绘图
1 | |

- Circular LPF mask, center circle is 1, remaining all zeros
- 圆形 LPF 掩码,中心圆为 1,其余均为零
- Only allows low frequency components - smooth regions
- 只允许低频组件-平滑区域
- Can smooth out noise but blurs edges.
- 可以消除噪音,但模糊边缘。
1 | |
1 | |
C:\Users\gzjzx\AppData\Local\Temp\ipykernel_19060\2728092902.py:2: RuntimeWarning: divide by zero encountered in log
fshift_mask_mag = 20 * np.log(cv2.magnitude(fshift[:, :, 0], fshift[:, :, 1]))
