2

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

  1. 一个 [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

cvFilter2D我分别使用 4 个内核运行该函数。以下是不同内核的部分结果:

  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.0005457674 -00.0207
  3. B -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

从结果来看,函数用核 B 和 Bt 计算出错误的结果。谁能告诉我如何使用 4 个内核正确运行 cvFilter2D?

4

1 回答 1

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);
    } 
}
于 2012-10-12T01:40:22.500 回答