小白opencv学习篇(3)图像掩膜

简介

不知各位帅哥美女们在p图时有没有注意到图片的模糊操作呢,其实,ps等软件里模糊图像就是通过图像的掩膜来实现的,下面就让我们好好学习一下图像的掩膜。

基本原理

图像的掩膜可以看成图像的滤波操作,通常用均值滤波和高斯滤波实现,效果也比较好,我们这里只讨论这二种。

  • 均值滤波:即有规律选取操作像素点附近一部分点,进行相加平均之后,将此操作点像素值用算出的均值代替。这样整个图像的像素点的方差就变小了,整个图像特别是边缘就变的模糊了。
  • 高斯滤波:不知读者有没有发现,均值滤波的缺陷也很明显,就是附近各点的占比是一样,但实际并非如此,里操作点近的点应该占的比重大,高斯滤波就好的解决了这个问题。相信大家在高中就学过正态分布,也就是高斯分布:
    f ( x ) = exp ( x 2 2 ) 2 p i f(x)=\cfrac{\exp(-\cfrac{x^2}{2})}{\sqrt{2*pi}}
    在这里插入图片描述
    可以看到,里中点近的地方所占比重大。把高斯分布离散化,是不是完美解决了均值滤波的问题呢tql。

相关API

  • 均值滤波blur官方描述
/** @brief Convolves an image with the kernel.

The function applies an arbitrary linear filter to an image. In-place operation is supported. When
the aperture is partially outside the image, the function interpolates outlier pixel values
according to the specified border mode.

The function does actually compute correlation, not the convolution:

\f[\texttt{dst} (x,y) =  \sum _{ \stackrel{0\leq x' < \texttt{kernel.cols},}{0\leq y' < \texttt{kernel.rows}} }  \texttt{kernel} (x',y')* \texttt{src} (x+x'- \texttt{anchor.x} ,y+y'- \texttt{anchor.y} )\f]

That is, the kernel is not mirrored around the anchor point. If you need a real convolution, flip
the kernel using #flip and set the new anchor to `(kernel.cols - anchor.x - 1, kernel.rows -
anchor.y - 1)`.

The function uses the DFT-based algorithm in case of sufficiently large kernels (~`11 x 11` or
larger) and the direct algorithm for small kernels.

@param src input image.
@param dst output image of the same size and the same number of channels as src.
@param ddepth desired depth of the destination image, see @ref filter_depths "combinations"
@param kernel convolution kernel (or rather a correlation kernel), a single-channel floating point
matrix; if you want to apply different kernels to different channels, split the image into
separate color planes using split and process them individually.
@param anchor anchor of the kernel that indicates the relative position of a filtered point within
the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor
is at the kernel center.
@param delta optional value added to the filtered pixels before storing them in dst.
@param borderType pixel extrapolation method, see #BorderTypes
@sa  sepFilter2D, dft, matchTemplate
 */
  • 高斯滤波GaussianBlur官方描述
/** @brief Applies the bilateral filter to an image.

The function applies bilateral filtering to the input image, as described in
http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is
very slow compared to most filters.

_Sigma values_: For simplicity, you can set the 2 sigma values to be the same. If they are small (\<
10), the filter will not have much effect, whereas if they are large (\> 150), they will have a very
strong effect, making the image look "cartoonish".

_Filter size_: Large filters (d \> 5) are very slow, so it is recommended to use d=5 for real-time
applications, and perhaps d=9 for offline applications that need heavy noise filtering.

This filter does not work inplace.
@param src Source 8-bit or floating-point, 1-channel or 3-channel image.
@param dst Destination image of the same size and type as src .
@param d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,
it is computed from sigmaSpace.
@param sigmaColor Filter sigma in the color space. A larger value of the parameter means that
farther colors within the pixel neighborhood (see sigmaSpace) will be mixed together, resulting
in larger areas of semi-equal color.
@param sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that
farther pixels will influence each other as long as their colors are close enough (see sigmaColor
). When d\>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is
proportional to sigmaSpace.
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes
 */

大家照这上面套就可以了,vs编译器还有提示功能,相信对大家都不难

示例

以下是示例

blur(test01, dst, Size(21, 1), Point(-1, -1));
GaussianBlur(test01, dst, Size(5, 5), 11, 11);

高斯模糊效果图:
在这里插入图片描述

结语

ok,掩膜就讲完啦!

猜你喜欢

转载自blog.csdn.net/qq_45908056/article/details/106828203