2013-04-13 41 views
3

我想在opencv4android中使用filter2d写一个简单的代码。 但调用函数'filter2d'后,在目标垫,我得到零(它没有正确运行功能)。在opencv4android filter2d

的结果应该是:

0.3750 0.5000 0.5000 0.5000 0.3750 
     0   0   0   0   0 
     0   0   0   0   0 
     0   0   0   0   0 
-0.3750 -0.5000 -0.5000 -0.5000 -0.3750 

我试图改变源的和内核的深度/类型,但它并没有帮助。

这里是我的代码:

Mat sourceMat = new Mat(5, 5, CvType.CV_32F); 
    Mat convMat = new Mat(3, 3, CvType.CV_32F); 
    Mat destMat = Mat.zeros(5, 5, CvType.CV_32F); 

    sourceMat.put(0,0,1); 
    sourceMat.put(0,1,1); 
    sourceMat.put(0,2,1); 
    sourceMat.put(0,3,1); 
    sourceMat.put(0,4,1); 
    sourceMat.put(1,0,1); 
    sourceMat.put(1,1,1); 
    sourceMat.put(1,2,1); 
    sourceMat.put(1,3,1); 
    sourceMat.put(1,4,1); 
    sourceMat.put(2,0,1); 
    sourceMat.put(2,1,1); 
    sourceMat.put(2,2,1); 
    sourceMat.put(2,3,1); 
    sourceMat.put(2,4,1); 
    sourceMat.put(3,0,1); 
    sourceMat.put(3,1,1); 
    sourceMat.put(3,2,1); 
    sourceMat.put(3,3,1); 
    sourceMat.put(3,4,1); 
    sourceMat.put(4,0,1); 
    sourceMat.put(4,1,1); 
    sourceMat.put(4,2,1); 
    sourceMat.put(4,3,1); 
    sourceMat.put(4,4,1); 

    convMat.put(0,0, 0.125); 
    convMat.put(0,1, 0.5); 
    convMat.put(0,2, 0.125); 
    convMat.put(1,0, 0); 
    convMat.put(1,1, 0); 
    convMat.put(1,2, 0); 
    convMat.put(2,0, -0.125); 
    convMat.put(2,1, -0.5); 
    convMat.put(2,2, -0.125); 


    Imgproc.filter2D(sourceMat, destMat, sourceMat.depth(), convMat); 

谁能告诉我这里有什么问题吗?有什么我做错了吗?

回答

1

这里没有错。 OpenCV结果对于filter2d函数中使用的默认边界处理模式是正确的。

您需要将最后的borderType参数设置为Imgproc.BORDER_CONSTANT才能获得预期的结果。

+0

您好!我想向您发送一封有关自由职业机会的电子邮件。如果您有兴趣,请在[Twitter](https://twitter.com/karlphillip)或[LinkedIn](http://www.linkedin.com/in/karlphillip)上留言。 – karlphillip