如何将cvFilter2D函数与1维内核一起使用

时间:2012-10-08 08:48:08

标签: c++ opencv

我有4个内核分别是A,At,B和Bt。

  1. A [1 0 0 -1]
  2. At(A的转置矩阵)1 0 0 -1
  3. B [0.50 0 0 -1 0 0 0.50]
  4. Bt(B的转置矩阵)   0.50 0 0 -1 0 0   0.50
  5. 我分别用4个内核运行函数cvFilter2D。以下是不同内核的部分结果:

    1. 甲   0.003921568 -0.007843137 0.000653625 -0.009803951 -0.003921628 -0.009803891 -0.007843137 -0.007843137 -0.009803951 -0.003485799
    2. 在 -0.019082069 0.002332032 -0.008974016 0.000923872 -0.000217795 -0.00043577 0.002332032 -0.000512481 0.000923872 -0.000540674 -0.00043577 -0.000217855
    3. 乙 -2.68E-25 -2.67E-25 -2.68E-25 -2.67E-25 -2.68E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25
    4. BT -2.65E-25 -2.67E-25 -2.66E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25 -2.67E-25
    5. 从结果中,函数用内核B和Bt计算错误的结果。谁能告诉我如何使用4个内核正确运行cvFilter2D?

1 个答案:

答案 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);
    } 
}