Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV, Variation of the Laplacian (Java)

I am attempting to locate the Variation for the Laplacian based on an image with the goal of getting a numerical value based on the blurriness of an image.

This is a useful post http://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/

cv2.Laplacian(image, cv2.CV_64F).var()

I've been trying to implement the same without luck. My starting point is a byte[] representing the image (img):

Mat mat = new Mat(img.getImageHeight(), img.getImageWidth(), CvType.CV_8UC3);
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2GRAY);
mat.put(0, 0, putData.getFileContents());
Imgproc.Laplacian(mat,mat, CvType.CV_64F);

Any help would be appreciated.

Essentially, I'm starting with an image and want to get a value representing the blurriness of an image using Laplacian.

like image 845
Scott Avatar asked Apr 04 '16 21:04

Scott


1 Answers

The Laplacian transformation can be replaced by a kernel applied with the filter2D method. I have been running that same python example in a java program like this:

    Mat destination = new Mat();
    Mat matGray=new Mat();  
    Mat kernel = new Mat(3,3, CvType.CV_32F){
       {
          put(0,0,0);
          put(0,1,-1);
          put(0,2,0);

          put(1,0-1);
          put(1,1,4);
          put(1,2,-1);

          put(2,0,0);
          put(2,1,-1);
          put(2,2,0);
       }
    };        
    Imgproc.cvtColor(image, matGray, Imgproc.COLOR_BGR2GRAY);          
    Imgproc.filter2D(matGray, destination, -1, kernel); 
    MatOfDouble median = new MatOfDouble();
    MatOfDouble std= new MatOfDouble();        
    Core.meanStdDev(destination, median , std);

    Math.pow(std.get(0,0)[0],2);

Hope this helps your coding.

Note: The values I get from the Python and Java program are different and I still didn't figure out why. Anyway the java code runs well.

Found out why the values were not the same. Using the Imgproc.Laplacian method instead of Imgproc.filter2D produces exactly the same values in python and java.

The new code becomes:

Mat destination = new Mat();
Mat matGray=new Mat();  

Imgproc.cvtColor(image, matGray, Imgproc.COLOR_BGR2GRAY);          
Imgproc.Laplacian(matGray, destination, 3); 
MatOfDouble median = new MatOfDouble();
MatOfDouble std= new MatOfDouble();        
Core.meanStdDev(destination, median , std);

Math.pow(std.get(0,0)[0],2);

Much simpler and with same results as python.

like image 124
Pedro Nogueira Avatar answered Sep 30 '22 01:09

Pedro Nogueira