Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter2d in opencv4android

I'm trying to write a simple code in opencv4android using filter2d. but after calling the function 'filter2d', in the destination mat, i get zeros (it didn't run the function correctly).

the result should be:

  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

i tried changing the source's and kernel's depth/type but it didn't help.

here's my code:

    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);

Can anyone tell my what's the problem here? is there something i'm doing wrong?


1 Answers

Nothing wrong here. OpenCV result is correct for the default border handling mode used in filter2d function.

You need to set the last borderType parameter to Imgproc.BORDER_CONSTANT to get your expected result.

like image 61
Andrey Kamaev Avatar answered Feb 21 '26 17:02

Andrey Kamaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!