Learning OpenCV 2.4.9 图像平滑 cvSmooth

来源:互联网 发布:数据库软件 编辑:程序博客网 时间:2024/06/05 08:59

win7+VS2013+OpenCV2.4.9


一、cvSmooth函数用法

译自OpenCV2.4.7,篇末有附录

 定义原型

<span style="font-size:14px;">void cvSmooth(const CvArr* src,//输入图像CvArr* dst,//输出图像,一般大小和src相同int smoothtype=CV_GAUSSIAN,// 平滑方式int size1=3,//平滑窗口的宽度int size2=0,//平滑窗口的长度double sigma1=0,// double sigma2=0//);</span>

参数说明:

src-输入图像. 

dst-输出图像. 

smoothtype-平滑方法:

CV_BLUR_NO_SCALE(简单不带尺度变换的模糊) - -对每个象素的size1×size2 领域求和。

如果邻域大小是变化的,可以事先利用函数 cvIntegral 计算积分图像。

CV_BLUR (simple blur)- -对每个象素param1×param2邻域求和并做尺度变换 1/(size1×size2)。

CV_GAUSSIAN(gaussian blur) - -对图像进行核大小为size1×size2 的高斯卷积。

CV_MEDIAN(median blur) - -对图像进行核大小为size1×size1 的中值滤波 (邻域是方的)。

CV_BILATERAL(双向滤波) - -应用双向 size1×size1滤波,彩色 sigma=sigma1,空间 sigma=sigma2.。

关于双向滤波,可参考http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html。

size1-平滑操作的第一个参数,平滑窗口的宽度。必须是正奇数(1,3,5,...)

size2-平滑操作的第二个参数,平滑窗口的高度。CV_MEDIANCV_BILATERAL模式无视之。

          在其他三种模式下,如果size2被设置为默认值0,那么程序会自动将size2设为size1。否则size2必须为正奇数。

sigma1-对应高斯参数的 Gaussian sigma (标准差). 如果为零,则标准差由下面的核尺寸计算:

           sigma = (n/2 - 1)*0.3 + 0.8, 其中 n=size1 对应水平核,n=size2 对应垂直核.        

  对小的卷积核 (3×3 to 7×7) 使用如上公式所示的标准 sigma 速度会快。

          如果sigma1 不为零,而 size1 和 size2 为零,则核大小由sigma 计算 (以保证足够精确的操作).

sigma2-OpenCV2.4.7文档中未说明,Learning OpenCV 中解释为不对称高斯核垂直方向的sigma,而水平方向simga为sigma1

   函数 cvSmooth 可使用上面任何一种方法平滑图像。每一种方法都有自己的特点以及局限:  

1、 没有缩放的图像平滑仅支持单通道图像,并且支持8位到16位的转换(与cvSobel和cvLaplace相似)和32位浮点数到32位浮点数的变换格式。  

2、简单模糊和高斯模糊支持 1- 或 3-通道, 8-比特 和 32-比特 浮点图像。这两种方法可以(in-place)方式处理图像。 

3、中值和双向滤波工作于 1- 或 3-通道, 8-位图像,但是不能以 in-place 方式处理图像.

注意:此函数现被弃用。使用 GaussianBlur() blur() medianBlur() 或者 bilateralFilter()

二、说明例程:

#include<cv.h>#include<highgui.h>#include<stdio.h>int main(int argc,char** argv){//check arginif(argc<2){printf("not enough inputs!\n");return 1;}IplImage* source=cvLoadImage(argv[1]);if(source==NULL){printf("Invalid image !\n");return 1;}cvNamedWindow("source",CV_WINDOW_AUTOSIZE);cvShowImage("source",source);IplImage* dest1=cvCreateImage(cvGetSize(source),8,3);//no_scale变换需要更高的数据精度IplImage* dest2=cvCreateImage(cvGetSize(source),16,3);cvNamedWindow("dest",CV_WINDOW_AUTOSIZE);cvSmooth(source,dest1,CV_BLUR,5,5,0.0,0.0);cvShowImage("dest",dest1);printf("now cv_blur\n");cvWaitKey(0);cvSmooth(source,dest2,CV_BLUR_NO_SCALE,5,5,0.0,0.0);printf("now cv_blur_no_scale\n");cvShowImage("dest",dest1);cvWaitKey(0);cvSmooth(source,dest1,CV_MEDIAN,5,5,0.0,0.0);cvShowImage("dest",dest1);printf("now cv_median\n");cvWaitKey(0);cvSmooth(source,dest1,CV_GAUSSIAN,5,5,0.0,0.0);cvShowImage("dest",dest1);printf("now cv_gaussian\n");cvWaitKey(0);cvSmooth(source,dest1,CV_BILATERAL,5,5,0.0,0.0);cvShowImage("dest",dest1);printf("now cv_bilateral\n");cvWaitKey(0);cvReleaseImage(&source);cvReleaseImage(&dest1);cvReleaseImage(&dest2);cvDestroyAllWindows();return 0;}

运行效果:


*篇幅有限,只贴了CV_BLUR

三、附录

OpenCV 2.4.7.0 手册

src – The source image
dst – The destination image
smoothtype – Type of the smoothing:
– CV_BLUR_NO_SCALE linear convolution with size1 × size2 box kernel (all 1’s).
If you want to smooth different pixels with different-size box kernels, you can use the
integral image that is computed using integral()
– CV_BLUR linear convolution with size1 × size2 box kernel (all 1’s) with subsequent
scaling by 1/(size1 · size2)
– CV_GAUSSIAN linear convolution with a size1 × size2 Gaussian kernel
– CV_MEDIAN median filter with a size1 × size1 square aperture
– CV_BILATERAL bilateral filter with a size1 × size1 square aperture, color sigma=
sigma1 and spatial sigma= sigma2 . If size1=0 , the aperture square side is set
to cvRound(sigma2*1.5) *2+1 . Information about bilateral filtering can be found at
http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
size1 – The first parameter of the smoothing operation, the aperture width. Must be a positive odd number (1, 3, 5, ...)
size2 – The second parameter of the smoothing operation, the aperture height. Ignored
by CV_MEDIAN and CV_BILATERAL methods. In the case of simple scaled/non-scaled and
Gaussian blur if size2 is zero, it is set to size1 . Otherwise it must be a positive odd
number.
sigma1 – In the case of a Gaussian parameter this parameter may specify Gaussian σ (standard deviation). If it is zero, it is calculated from the kernel size:
σ 0.3(n/2 − 1) + 0.8 where size1 for horizontal kernel
size2 for vertical kernel
3.1. Imag e Filtering 263
The OpenCV Reference Manual, Release 2.4.7.0
Using standard sigma for small kernels ( × to × ) gives better speed. If sigma1 is
not zero, while size1 and size2 are zeros, the kernel size is calculated from the sigma (to
provide accurate enough operation).
The function smooths an image using one of several methods. Every of the methods has some features and restrictions
listed below:
• Blur with no scaling works with single-channel images only and supports accumulation of 8-bit to 16-bit format
(similar to Sobel() and Laplacian() ) and 32-bit floating point to 32-bit floating-point format.
• Simple blur and Gaussian blur support 1- or 3-channel, 8-bit and 32-bit floating point images. These two
methods can process images in-place.
• Median and bilateral filters work with 1- or 3-channel 8-bit images and can not process images in-place.
Note: The function is now obsolete. Use GaussianBlur() blur() medianBlur() or bilateralFilter() .



0 0