Search code examples
javaopencvgaussianedge-detection

how to use 3x3 gaussian filter correctly


I am following a tutorial in image processing and it says the following

From the gray-scale image, a Gaussian image pyramid PGauss is
computed by firstly applying a 3× 3 Gaussian filter to the image

I know how to down and up sample an image using PyrUp and PyrDown. but i do not how to use or apply 3x3 gaussian filter!

i read some postes and i followed them, and they suggested to use
ImgProc.getGaussianKernel(p1,p2,p3)

the problem i faced when i used ImgProc.getGaussianKernel(p1,p2,p3) are

1-i could not specify the type as CV_32 or CV_64, beacause when i specify the parameter3 as ImgProc.CV_32 it is always underscred with red line?

2-i used the getGaussianKernel with only two parameters the size and the sigma, and when i ran this step and wrote the Mat returned from it to the hard drive, i found the image is empty despite it has size. why that is happening?

please let me know how to use 3x3 gaussian filter correctly

Update:

can i use ImgProc.blur(.....) to smooth the image instead of ImgProc.getGaussianKernel(p1,p2,p3)?


Solution

  • you really should look at the docs before asking , also: filter2D ..

    int kernelSize = 3; // 3x3 filter
    int resultType = CvType.CV_32F; // CvType, not Imgproc !
    double sigma = 1.2; // whatever you choose.
    
    Mat kernel = Imgproc.getGaussianKernel(kernelSize, sigma, resultType);
    
    // now apply the filter:
    Mat filtered = new Mat();
    Imgproc.filter2D(image, filtered, CvType.CV_32F, kernel);
    

    GaussianBlur does the same as the code above, blur() is using a box-filter, not a gaussian one.