图像Smoothing/Blurring

来源:互联网 发布:零售业大数据应用实例 编辑:程序博客网 时间:2024/06/03 17:52

Smooth/Blurring是图片预处理的一种技术,这样可以减弱图片中突变点对后续处理的影响,像噪音这样的突变点是需要处理的;也可以降低图像的分辨率,因为图像变模糊了。这种技术相对于图像锐化,增强突变点。


1. 普通的smoothing

输出的像素为周围一定范围内的像素算数平均值;boxFilter是blur的一般形式,前者可以指定输出的深度,数据格式,比如:CV_32F

Simple Blur and the Box Filter
void cv::blur(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
cv::Size ksize, // Kernel size
cv::Point anchor = cv::Point(-1,-1), // Location of anchor point
int borderType = cv::BORDER_DEFAULT // Border extrapolation to use
);

void cv::boxFilter(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
int ddepth, // Output depth (e.g., CV_8U)
cv::Size ksize, // Kernel size
cv::Point anchor = cv::Point(-1,-1), // Location of anchor point
bool normalize = true, // If true, divide by box area
int borderType = cv::BORDER_DEFAULT // Border extrapolation to use
);


2.中值滤波/Median Filter

采用周围一定范围内像素值的中值作为输出;

中值和平均值没有必然的关系。中值是将所给的一组数从小到大或从大到小排列,奇数个数的话取中间的数字,偶数个数的话取中间两个数的平均数;而平均值就是把这组数相加,然后除以这组数的个数。
中值的优点是不受偏大或偏小数据的影响,很多情况下用它代表全体数据的一般水平更合适。如果数列中存在极端变量值,用中位数做代表值就比平均数更好

void cv::medianBlur(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
cv::Size ksize // Kernel size
);


3.高斯滤波/Gaussian Filter

输出的像素是原像素与2D高斯函数模板进行卷积的结果;

2D高斯模板需要确定M、N,sigma(M:长,N:宽,sigma:标准差),python代码例子如下:M=5,N=5,sigma=0.6

******************************************************************************

import numpy as np
x, y = np.meshgrid(np.linspace(-2,2,5), np.linspace(-2,2,5))
sigma=0.6
coefficient=1/(2*np.pi*sigma**2)
value1=np.exp(-( x**2+y**2)/ ( 2.0 * sigma**2 )  )
g=coefficient*value1

******************************************************************************

void cv::GaussianBlur(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
cv::Size ksize, // Kernel size
double sigmaX, // Gaussian half-width in x-direction
double sigmaY = 0.0, // Gaussian half-width in y-direction
int borderType = cv::BORDER_DEFAULT // Border extrapolation to use
);


4.双边滤波/bilateral filter

相对于高斯滤波,能够更好的保存边缘信息;

void cv::bilateralFilter(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
int d, // Pixel neighborhood size (max distance)
double sigmaColor, // Width param for color weight function
double sigmaSpace, // Width param for spatial weight function
int borderType = cv::BORDER_DEFAULT // Border extrapolation to use
);






原创粉丝点击