3.4 fundamentals of spatial filters 以及C++卷积实现

correlation (相关)

( w f ) ( x , y ) = s = a a t = b b w ( s , t ) f ( x + s , y + t ) (w ☆ f)(x,y)= \sum_{s=-a}^a \sum_{t=-b}^b w(s,t)f(x+s,y+t)

采用该式子进行相关运算。另外也可以将 w w 旋转 18 0 ° 180^° ,然后利用该式子进行卷积运算。

相关运算满足分配律。

convolution (卷积)

( w f ) ( x , y ) = s = a a t = b b w ( s , t ) f ( x s , y t ) (w ★ f)(x,y) = \sum_{s=-a}^a \sum_{t=-b}^b w(s,t)f(x-s,y-t)

采用该式子进行卷积运算。另外也可以将 w w 旋转 180 ° 180° ,然后利用该式子进行相关运算。可以发现,我们可以两个式子都可以完成相关和卷积运算。

卷积运算满足交换律、结合律、分配律。
在这里插入图片描述

function f=correlation(A,B)
% CORRELATION performs the convolution of A and B.
% Matlab 矩阵下标从1开始;A为经过边界填充后的图像,B为卷积核
% 输出f的大小与A相同,B为滤波核
% 边界填充采用 'replicate'

% 版本号V1.0,编写于2019年3月30日。作者:LilyZJ

[m,n]=size(A);
[kSize,kSize]=size(B);
center=(kSize-1)/2; 
A=padarray(A,[center,center],'replicate','both');

for i=1:m
    for j=1:n
        for p=1:kSize
            for q=1:kSize
                f(i,j)=f(i,j)+A(i+p-1,j+q-1)*B(p,q);
            end
        end
    end
end
// C++实现,Mat 类型 下标从0开始;f为原图像,filters为卷积核
Mat_<uchar> correlation(const Mat &f, const double** filters,int filterSize) {
 Mat dist=Mat::zeros(f.rows,f.cols,CV_8UC1); // dist为滤波后的图像
 int center = filterSize / 2; // 需要对原图像f的上下左右各填充center行
 Mat zeropad_f; // zeropad_f 为经过0填充后的图像
 cv::copyMakeBorder(f, zeropad_f, center, center, center, center, BORDER_CONSTANT, 0);
 for (int i = 0; i < f.rows; i++) {
  for (int j = 0; j < f.cols; j++) {
   for (int m = 0; m < filterSize; m++) {
    for (int n = 0; n < filterSize; n++) {
     dist.at<double>(i, j) += f.at<uchar>(i + m, j + n)*filters[m][n];
    }
   }
  }
 }
 return dist;
}

linear spatial filtering / spatial convolution

When talking about filtering and kernels, you are likely to encounter the terms convolution filter, convolution mask, or convolution kernel to denote filter kernels of the type we have been discussing. Typically, these terms are used in the literature to denote a spatial filter kernel, and not to imply necessarily that the kernel is used for convolution. Similarly, “convolving a kernel with an image” often is used to denote the sliding, sum-of-products process we just explained, and does not necessarily differentiate between correlation and convolution. Rather, it is used generically to denote either of the two operations. This imprecise terminology is a frequent source of confusion. In this book, when we use the term linear spatial filtering, we mean convolving a kernel with an image.

通常,在文献中并不一定严格区分相关和卷积操作,并且由于卷积核通常是中心对称的,不需要经过旋转。(若滤波核中心对称,则相关运算与卷积运算的结果相同。)

separable filter kernels

一个2-D的核 w w ,如果其秩为1,则该核可以分解成两个一维向量的外积:
w = v 1 v 2 T w= v_1 v_2^T
分解成更小的核,在卷积操作时我们可以利用卷积运算的结合律,减小计算时间开销。

空间滤波和频率域滤波的比较

空间滤波中的卷积相当于频率域的乘积,频率域的卷积相当于空间滤波中的乘积;

As explained in Chapter 4 , a function (e.g., an image) satisfying some mild conditions can be expressed as the sum of sinusoids of different frequencies and amplitudes. Thus, the appearance of an image depends on the frequencies of its sinusoidal components—change the frequencies of those components, and you will change the appearance of the image. What makes this a powerful concept is that it is possible to associate certain frequency bands with image characteristics. For example, regions of an image with intensities that vary slowly (e.g., the walls in an image of a room) are characterized by sinusoids of low frequencies. Similarly, edges and other sharp intensity transitions are characterized by high frequencies. Thus, reducing the high-frequency components of an image will tend to blur it.

如果将一幅图像视为多个不同频率和振幅的正弦函数之和,即图像外观将依赖于其对应的正弦组成。如果改变组成的正弦成分,将会改变图像的外观。因此,某些特定的频率成分可能对应着图像的某些特征。例如:低频正弦信号通常表示亮度变化平缓的区域(例如一面白墙),而高频信号通常表示图像中的边缘等亮度变化剧烈的区域。

在空间中,我们常采用卷积滤波进行线性滤波处理;而在频率域中,我们常采用multiplicative filters。例如采用低通滤波时,我们先利用傅里叶变换将空间信号转化到频率域,然后再乘以the filter transfer function(在这里是低通滤波函数)消除高频成分,再通过傅里叶逆变换将频率域信号转换到空间域。(也就是说,通常频率域滤波对应着某个空间滤波,反之亦然。)

关于如何构建空间滤波器(spatial filter kernels)

书中给出了三种方法:
(1) 基于数学性质:如均值滤波器通过计算邻域的平均值;还有计算图像局部导数的滤波器能够使图像锐化。
(2) 对具有特定性质的2-D空间函数进行采样:如从高斯函数中采样构建的加权平均滤波器(低通滤波器)。
(3) 设计具有指定频率响应的空间滤波器。如通过一维滤波器生成2-D滤波器。

参考文献:

  1. 数字图像处理第四版,冈萨雷斯

猜你喜欢

转载自blog.csdn.net/LilyZJ/article/details/88853759
3.4