'opencv4nodejs how to calculate the blur degree with variance of laplacian

I have a code:

const cv = require('opencv4nodejs');

let text = "";

let image = cv.imread('./images/focused.jpg');
let gray = image.cvtColor(cv.COLOR_BGR2GRAY);
let fm = image.laplacian(cv.CV_64F);
cv.imwrite('./images/new_very_blured.jpg', gray);

console.log(image);
console.log(fm);
console.log(fm.step / image.step);

And picture, which i saved is the black square, but its isn't what i expected. I read an article about calculating the blur degrees and there:

cv2.Laplacian(image, cv2.CV_64F).var() // Python function

This article - https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/

But i using nodejs and in nodejs this function isn't exists, so i try to use other function, but it work doesn't as in Python. Please help me, how i need to edit my code to do it works. In the console i need to get a percent from 1 to 100, which will show me the blur degree.



Solution 1:[1]

Here is complete solution:

//image : orirginal image
let gray = new cv.Mat();
let laplacian_mat = new cv.Mat();

cv.cvtColor(image, gray, cv.COLOR_RGBA2GRAY);

cv.Laplacian(gray, laplacian_mat, cv.CV_64F, 1, 1, 0, cv.BORDER_DEFAULT);

// cv.imshow("canvasOutput", laplacian_mat);

let myMean = new cv.Mat(1, 4, cv.CV_64F);
let myStddev = new cv.Mat(1, 4, cv.CV_64F);
cv.meanStdDev(laplacian_mat, myMean, myStddev);

let sharpness = myStddev.doubleAt(0, 0);

console.log(sharpness * sharpness);


gray.delete();
laplacian_mat.delete();
myMean.delete();
myStddev.delete();

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 saurabh jadhav