OpenCV2马拉松第5圈——线性滤波
Posted mqxnongmin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV2马拉松第5圈——线性滤波相关的知识,希望对你有一定的参考价值。
收入囊中
- 相关和卷积工作原理
- 边界处理
- 滤波器的工作原理
- 会使用均值滤波,高斯滤波
- 使用自己创造的核函数进行双线性滤波
- 可分离的滤波(加速)
葵花宝典
- 0填充。非常easy的处理方式
- 常数填充
- 夹取填塞(clamp),不断地复制边缘像素的值
- 重叠填塞(wrap),以环状形态围绕图像进行循环
- 镜像填塞(mirror),像素围绕图像边界进行镜像反射
- 延长(extend)。通过在边缘像素值中减去镜像信号的方式延长信号
/* Various border types, image boundaries are denoted with ‘|‘ * BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh * BORDER_REFLECT: fedcba|abcdefgh|hgfedcb * BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba * BORDER_WRAP: cdefgh|abcdefgh|abcdefg * BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified ‘i‘ */
初识API
- C++:?void?blur(InputArray?src, OutputArray?dst, Size?ksize, Point?anchor=Point(-1,-1), int?borderType=BORDER_DEFAULT?)
-
? - src?– 原始图像
- dst?– 输出图像
- ksize?– 核函数大小
- anchor?– 锚点,普通情况下默觉得(-1,-1)。意味着在中心进行卷积
- borderType?– 边界类型
The function smoothes an image using the kernel:
高斯滤波。对去除正态分布的噪声非常实用
- C++:?void?GaussianBlur(InputArray?src, OutputArray?dst, Size?ksize, double?sigmaX, double?sigmaY=0, int?borderType=BORDER_DEFAULT?)
-
? - src?– 输入图像
- dst?– 输出图像
- ksize?– 核大小
- sigmaX?– 控制幅度的參数(大家应该都学过或看过高斯函数吧,比方在正态分布中),假设sigmaX,sigmaY都为0,则由核的高度宽度自己计算
- sigmaY?– 二维高斯函数有两个方向能够控制幅度。或这个不设置则和X一样
- borderType?– 边界类型
用自己的核函数进行滤波
- C++:?void?filter2D(InputArray?src, OutputArray?dst, int?ddepth, InputArray?kernel, Point?anchor=Point(-1,-1), double?delta=0, intborderType=BORDER_DEFAULT?)
-
? - src?– 输入图像.
- dst?– 输出图像.
- depth?–?ddepth=-1,输出图像具有和输入图像一样的depth
- kernel?– 核函数,单通道浮点矩阵
- anchor?– 同之前
- delta?– 可选,直接加到输出图像
- borderType?– 边界类型
荷枪实弹
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Mat src,dst;
int i = 1;
static void change_dst(int, void*)
{
if(i%2 == 0)i++;
blur( src, dst, Size( i, i ), Point(-1,-1));
imshow("dstImage", dst);
}
int main( int, char** argv )
{
src = imread( argv[1] );
namedWindow("srcImage", 1);
namedWindow("dstImage", 1);
createTrackbar( "mean filter:", "dstImage", &i, 20, change_dst);
change_dst(0, 0);
imshow("srcImage", src);
waitKey();
return 0;
}
使用高斯滤波
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Mat src,dst;
int i = 1;
static void change_dst(int, void*)
{
if(i%2 == 0)i++;
GaussianBlur( src, dst, Size( i, i ), 0, 0 );
imshow("dstImage", dst);
}
int main( int, char** argv )
{
src = imread( argv[1] );
namedWindow("srcImage", 1);
namedWindow("dstImage", 1);
createTrackbar( "gauss filter:", "dstImage", &i, 20, change_dst);
change_dst(0, 0);
imshow("srcImage", src);
waitKey();
return 0;
}
使用自己定义线性滤波
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Mat src,dst,Kernel;
int main( int, char** argv )
{
src = imread( argv[1] );
namedWindow("srcImage", 1);
namedWindow("dstImage", 1);
Kernel = (Mat_<double>(3,3) << 1, 2, 1, 2, 4, 2, 1, 2, 1)/16;
filter2D(src, dst, -1 , Kernel, Point(-1,-1));
imshow("dstImage", dst);
imshow("srcImage", src);
waitKey();
return 0;
}
举一反三
K?=vhT ?
- C++:?void?sepFilter2D(InputArray?src, OutputArray?dst, int?ddepth, InputArray?kernelX, InputArray?kernelY, Point?anchor=Point(-1,-1), doubledelta=0, int?borderType=BORDER_DEFAULT?)
-
? - src?– Source image.
- dst?– Destination image of the same size and the same number of channels as?src?.
- ddepth?–
- Destination image depth. The following combination of?src.depth()?and?ddepth?are supported:
-
- src.depth()?=?CV_8U,?ddepth?= -1/CV_16S/CV_32F/CV_64F
- src.depth()?=?CV_16U/CV_16S,?ddepth?= -1/CV_32F/CV_64F
- src.depth()?=?CV_32F,?ddepth?= -1/CV_32F/CV_64F
- src.depth()?=?CV_64F,?ddepth?= -1/CV_64F
when?ddepth=-1, the destination image will have the same depth as the source.
- kernelX?– Coefficients for filtering each row.
- kernelY?– Coefficients for filtering each column.
- anchor?– Anchor position within the kernel. The default value??means that the anchor is at the kernel center.
- delta?– Value added to the filtered results before storing them.
- borderType?– Pixel extrapolation method. See?html?
highlight=sepfilter2d#int%20borderInterpolate(int%20p,%20int%20len,%20int%20borderType)" rel="nofollow" title="int borderInterpolate(int p, int len, int borderType)" style="color:rgb(0,144,217);text-decoration:none;">borderInterpolate()?for details.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Mat src,dst,kernelX,kernelY;
int main( int, char** argv )
{
src = imread( argv[1] );
namedWindow("srcImage", 1);
namedWindow("dstImage", 1);
kernelX = (Mat_<double>(1,5) << 1,4,6,4,1)/16;
kernelY = (Mat_<double>(1,5) << 1,4,6,4,1)/16;
sepFilter2D(src, dst, -1, kernelX, kernelY);
imshow("dstImage", dst);
imshow("srcImage", src);
waitKey();
return 0;
}