OpenCV Chapter2 卷积下的运算
OpenCV
图像平滑处理
前导知识:卷积$(convolution)$
离散卷积
主要应用:数字图像处理
先导:如何计算两个骰子分别向外投掷一次不同和数的概率
怎么计算两个和数?
转置数组,从左向右移动,依次对齐计算

卷积计算方法
)但是如果每一面的概率不一样,而不是像之前那样都是$\frac{1}{6}\newline$了呢?

概率非均匀分布
)求概率的时候不再是$\frac {情况数}{组合总数}$了,而是使用纯概率计算

公式化纯概率计算
)公式化

公式化计算
)表格化,将每一条对角线上面的数求和,就得到了新的一组数(与上面一个原理,不过变成了可视化表格)

转化为表格计算
)$$ \begin{align} &(a * b)\ is\ a\ new\ array\newline &and\ (a * b)[n]=\sum_{i, j, i + j = n}a_i\ b_j\newline \end{align} $$
$Example:\newline$
$$ \begin{align} &(1,2,3) * (4, 5, 6)\newline &=(1 * 4, 1 * 5 + 2 * 4, 1 * 6 + 2 * 5 + 3 * 4, 2 * 6 + 3 * 5,3 * 6)\newline &=(4, 13, 28, 27, 18)\newline \end{align} $$
python科学计算库numpy中的使用
|
|
输出
|
|
一个长的data list
和一个短的概率array
组合(权重可以不同,但是求和要等于1),概率数组从左往右滑动,同时求和 $\rightarrow$ 在求小窗口数据里面的平均值

短数组与长数组
)应用与图像处理
(3X3)的矩阵扫描,用来求该矩阵下的加权平均值(RGB三个通道都是),实现了对图像的模糊化

RGB三个通道分别计算
)形象描述

图像模糊
)高斯分布
通过控制权重使得模糊的时候,权重从中心到两边逐渐变小,这样模糊出来的图像视觉效果更好
维基百科
正态分布(中国大陆作正态分布,香港作正态分布,台湾作常态分布,英语:Normal distribution),又名高斯分布(英语:Gaussian distribution)、正规分布,是一个非常常见的连续概率分布。正态分布在统计学上十分重要,经常用在自然和社会科学来代表一个不明的随机变量。
若随机变量
服从一个位置参数为
、尺度参数为
的正态分布,记为:
$X \sim N(\mu, \sigma^2)\newline$
则其概率密度函数为 $f(x)=\frac {1}{\sigma \sqrt{2\pi}}e^{-\frac {(x-\mu)^2}{2\sigma^2}}\newline$
正态分布的数学期望值或期望值
等于位置参数,决定了分布的位置;其方差$\sigma^2$的开平方或标准差$\sigma$等于尺度参数,决定了分布的幅度。
中心极限定理指出,在特定条件下,一个具有有限均值和方差的随机变量的多个样本(观察值)的平均值本身就是一个随机变量,其分布随着样本数量的增加而收敛于正态分布。因此,许多与独立过程总和有关的物理量,例如测量误差,通常可被近似为正态分布。
正态分布的概率密度函数曲线呈钟形,因此人们又经常称之为钟形曲线(类似于寺庙里的大钟,因此得名)。我们通常所说的标准正态分布是位置参数
,尺度参数$\displaystyle \sigma ^{2}=1$的正态分布
权值和不为1,且有负权重存在的情况

不同的卷积核效果不同
)判别图像中所有竖向的边界
通过不同的卷积核($Kernel$)(即矩阵),就可以对图像做不同的处理
卷积神经网络 $\rightarrow$ 用数据来算出应该选取什么样的核(取决于检测目标)
notice:因为在纯数理论下算出来的卷积实际上总是要比原数组长的,所以在计算机科学中一般会把多余的值截掉
算法优化
|
|
|
|
计算速度飙升!
什么是fftconvolve
?

fftclovolve
)先建立一个表格存储计算完成的组合乘积,然后沿着对角线求和,把原来老算法里面需要重复计算组合乘积的弊病给解决了
其他应用 $\rightarrow$ 多项式乘积

多项式乘积
)代码部分
均值滤波与方框滤波
|
|
有关$normalize$:(K
为卷积核)
$$ \begin{align} &K=\alpha \begin{bmatrix} &1, 1, 1, \dots, 1, 1, 1\newline &1, 1, 1, \dots, 1, 1, 1\newline &\dots \dots\newline &1, 1, 1, \dots, 1, 1, 1\newline \end{bmatrix} \end{align} $$
$$ \alpha=\begin{cases} &\frac {1}{ksize.width * ksize.height}\ when\ normalize=True, is\ equal\ to\ the\ K\ of\ blur\newline &1\ otherwise \end{cases} $$
高斯滤波
维基百科
Mathematically, applying a Gaussian blur to an image is the same as convolving the image with a Gaussian function. This is also known as a two-dimensional Weierstrass transform. By contrast, convolving by a circle (i.e., a circular box blur) would more accurately reproduce the bokeh effect.
Since the Fourier transform of a Gaussian is another Gaussian, applying a Gaussian blur has the effect of reducing the image’s high-frequency components; a Gaussian blur is thus a low-pass filter.
The Gaussian blur is a type of image-blurring filter that uses a Gaussian function (which also expresses the normal distribution in statistics) for calculating the transformation to apply to each pixel in the image. The formula of a Gaussian function in one dimension is
$G(x)=\frac {1}{\sqrt {2\pi \sigma^2}}e^{-\frac {x^2}{2\sigma^2}}\newline$
In two dimensions, it is the product of two such Gaussian functions, one in each dimension:
$G(x, y)=\frac {1}{2\pi \sigma^2}e^{-\frac {x^2+y^2}{2\sigma^2}}\newline$
where x is the distance from the origin in the horizontal axis, y is the distance from the origin in the vertical axis, and σ is the standard deviation of the Gaussian distribution. It is important to note that the origin on these axes are at the center (0, 0). When applied in two dimensions, this formula produces a surface whose contours are concentric circles with a Gaussian distribution from the center point.
Values from this distribution are used to build a convolution matrix which is applied to the original image. This convolution process is illustrated visually in the figure on the right. Each pixel’s new value is set to a weighted average of that pixel’s neighborhood. The original pixel’s value receives the heaviest weight (having the highest Gaussian value) and neighboring pixels receive smaller weights as their distance to the original pixel increases. This results in a blur that preserves boundaries and edges better than other, more uniform blurring filters; see also scale space implementation.
“离中心点越近权重越大,反之则越小”
|
|
参数说明
Parameters
|
|
src | input image; the image can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. |
---|---|
dst | output image of the same size and type as src. |
ksize | Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. Or, they can be zero’s and then they are computed from sigma. |
sigmaX | Gaussian kernel standard deviation in X direction. |
sigmaY | Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height, respectively (see getGaussianKernel for details); to fully control the result regardless of possible future modifications of all this semantics, it is recommended to specify all of ksize, sigmaX, and sigmaY. |
borderType | pixel extrapolation method, see BorderTypes. BORDER_WRAP is not supported. |
中值滤波
对卷积核内部的像素进行排序,取中值作为新的像素点
|
|
注意第二个参数发生了变化
@param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 …
去噪效果最好,但是也有相应的损失
连接图像
|
|
示例代码
|
|
腐蚀与膨胀
腐蚀和膨胀为经典的两个数字形态学处理算法,一般用于二值图像,本质也是对于核的卷积
腐蚀
腐蚀操作也是用卷积核扫描图像,只不过腐蚀操作的卷积核一般都是1,如果卷积核内部所有像素点都是白色,那么中心点即为白色,否则为黑色,腐蚀效果强弱和卷积核的大小成正相关
|
|
获取核结构
|
|
shape:
- MORPH_RECT :矩形元素
- MORPH_CROSS :十字形元素
- MORPH_ELLIPSE:椭圆形元素
ksize:矩阵大小
膨胀
膨胀是腐蚀的相反操作,基本原理是只要保证卷积核的中心点是非零值,周边无论是零还是非零值,都会变成非零值
|
|
开闭运算
开运算
先腐蚀,后膨胀
|
|
闭运算
先膨胀,后腐蚀
|
|
梯度运算
原理:膨胀
— 腐蚀
= 边界信息
使用例
|
|
礼帽与黑帽
原理:
礼帽
=原始输入
-开运算结果
$\rightarrow$腐蚀掉的部分
黑帽
=闭运算
-原始输入
$\rightarrow$更强的的应该被腐蚀掉的部分(因为闭运算中先膨胀就无法腐蚀掉了),相减后没有了,得到的其实是原来图像的轮廓
|
|