'How to calculate Laplacian Variance in OpenCV js?

I am try to implement Blur detection using OpenCV but I cannot find any equivalent for the below statement in OpenCV js : cv2.Laplacian(image, cv2.CV_64F).var()

Is there any other way to calculate variance ?



Solution 1:[1]

You can calculate variance by getting the Standard Deviation of the Laplacian Mat and then squaring it. OpenCV has a function to calculate the standard deviation .

cv.meanStdDev(srcMat, mean, standardDeviationMat)

In the code below you can check how it's done. I'm reading the image from an ImageData object, then changing it to gray scale and finally calculating the Laplacian Variance:

const cvImage = cv.matFromImageData(imageData);
const grayImage = new cv.Mat();
const laplacianMat = new cv.Mat();

cv.cvtColor(cvImage, grayImage, cv.COLOR_RGBA2GRAY, 0);
cv.Laplacian(grayImage, laplacianMat, cv.CV_64F);

const mean = new cv.Mat(1, 4, cv.CV_64F);
const standardDeviationMat = new cv.Mat(1, 4, cv.CV_64F);

cv.meanStdDev(laplacianMat, mean, standardDeviationMat);

const standardDeviation = standardDeviationMat.doubleAt(0, 0);
const laplacianVar = standardDeviation * standardDeviation;

There is a similar answer in this post, but using the package opencv4nodejs instead of opencv.js.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexmeri98