-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (30 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const sharp = require('sharp');
class BlurryDetector {
constructor(threshold = 300) {
this.threshold = threshold;
}
async computeLaplacianVariance(imagePath) {
// Define the Laplacian kernel
const laplacianKernel = {
width: 3,
height: 3,
kernel: [0, 1, 0, 1, -4, 1, 0, 1, 0]
};
// Convolve the image with the Laplacian kernel
const laplacianImageData = await sharp(imagePath)
.greyscale()
.raw()
.convolve(laplacianKernel)
.toBuffer();
// Compute the variance of the Laplacian image
const mean = laplacianImageData.reduce((sum, value) => sum + value, 0) / laplacianImageData.length;
const variance = laplacianImageData.reduce((sum, value) => sum + Math.pow(value - mean, 2), 0) / laplacianImageData.length;
return variance;
}
async isImageBlurry(imagePath) {
const variance = await this.computeLaplacianVariance(imagePath);
return variance < this.threshold;
}
}
// Export the class for CommonJS
module.exports = BlurryDetector;