我有4个内核分别是A,At,B和Bt。
我分别用4个内核运行函数cvFilter2D
。以下是不同内核的部分结果:
从结果中,函数用内核B和Bt计算错误的结果。谁能告诉我如何使用4个内核正确运行cvFilter2D?
答案 0 :(得分:0)
我在网上发现了一个函数如下。据说该函数与matlab中的“conv2”类似。任何改进功能的建议都将受到赞赏!
enum ConvolutionType {
/* Return the full convolution, including border */
CONVOLUTION_FULL,
/* Return only the part that corresponds to the original image */
CONVOLUTION_SAME,
/* Return only the submatrix containing elements that were not influenced
by the border */
CONVOLUTION_VALID
};
void conv2(const Mat &img, const Mat& kernel, ConvolutionType type, Mat& dest)
{
Mat source = img;
if(CONVOLUTION_FULL == type)
{
source = Mat();
const int additionalRows = kernel.rows-1, additionalCols = kernel.cols-1;
copyMakeBorder(img,source,(additionalRows+1)/2,additionalRows/2,\
additionalCols+1)/2,additionalCols/2,BORDER_CONSTANT,Scalar(0));
}
Point anchor(kernel.cols - kernel.cols/2 - 1, kernel.rows - kernel.rows/2 - 1);
int borderMode = BORDER_CONSTANT;
Mat kernelInvert;
flip(kernel,kernelInvert,0);
filter2D(source,dest,img.depth(),kernelInvert,anchor,0,borderMode);
if(CONVOLUTION_VALID == type)
{
dest = dest.colRange((kernel.cols-1)/2, dest.cols - kernel.cols/2)\
.rowRange((kernel.rows-1)/2, dest.rows - kernel.rows/2);
}
}