opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异

时间:2023-02-07 13:30:00

出处:

1:cvFilter2D() 函数为opencv中c语言函数

2:filter2D()函数为opencv中c++函数

3:imfilter()函数为matlab版本函数


计算结果的异同:

2(c++)与3(matlab)该两种语言的计算结果是一样的;

1(c语言)与2,3的计算结果是不一样的。


==============================以下为cvcvFilter2D( )与MATLAB中imfilter()结果的对比==========================================

opencv中cvFilter2D( ) 函数与MATLAB中imfilter()函数计算出的结果是不一样的。


首先对于MATLAB中的函数 imfilter( )

function b = imfilter(varargin)
%IMFILTER N-D filtering of multidimensional images.
% B = IMFILTER(A,H) filters the multidimensional array A with the
% multidimensional filter H. A can be logical or it can be a
% nonsparse numeric array of any class and dimension. The result,
% B, has the same size and class as A.
%
% Each element of the output, B, is computed using double-precision
% floating point. If A is an integer or logical array, then output
% elements that exceed the range of the given type are truncated,
% and fractional values are rounded.
%
% B = IMFILTER(A,H,OPTION1,OPTION2,...) performs multidimensional
% filtering according to the specified options. Option arguments can
% have the following values:
%
% - Boundary options
%
% X Input array values outside the bounds of the array
% are implicitly assumed to have the value X. When no
% boundary option is specified, IMFILTER uses X = 0.
%
% 'symmetric' Input array values outside the bounds of the array
% are computed by mirror-reflecting the array across
% the array border.
%
% 'replicate' Input array values outside the bounds of the array
% are assumed to equal the nearest array border
% value.
%
% 'circular' Input array values outside the bounds of the array
% are computed by implicitly assuming the input array
% is periodic.
%
% - Output size options
% (Output size options for IMFILTER are analogous to the SHAPE option
% in the functions CONV2 and FILTER2.)
%
% 'same' The output array is the same size as the input
% array. This is the default behavior when no output
% size options are specified.
%
% 'full' The output array is the full filtered result, and so
% is larger than the input array.
%
% - Correlation and convolution
%
% 'corr' IMFILTER performs multidimensional filtering using
% correlation, which is the same way that FILTER2
% performs filtering. <span style="color:#FF0000;">When no correlation or
% convolution option is specified, IMFILTER uses
% correlation.</span>
%
% 'conv' IMFILTER performs multidimensional filtering using
% convolution.
关于该函数,要说明的有两点:
1:该函数未指明是相关运算(correlation)还是卷积运算(convolution)时,默认使用相关运算。

2:该函数的运算结果(一般人都想要的结果)为下图所示:

opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异

opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异


而opencv中的卷积函数cvFilter2D( )

当kernel的大小为3*3时,计算出的结果为MATLAB计算出结果的5/6              也就是 7.5/9 =5/6

opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异

当kernel大小为5时,计算出的结果为MATLAB计算出结果的9/10  也就是22.5/25=9/10

opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异


我有去跟踪opencv的源码,但是看了一段时间也没搞清楚为啥,节约时间也就放弃了。

如果你看完源搞清楚了原因,欢迎分享,在评论区写出原因,以便需要的人去查看,谢谢!