compute method
double
compute(
- Image image
)
override
Implementation
@override
double compute(img.Image image) {
final int width = image.width;
final int height = image.height;
// Convert image to grayscale
List<int> gray = List<int>.filled(width * height, 0);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixelIndex = y * width + x;
final pixel = image.getPixel(x, y);
// Assuming the pixel object has r, g, b properties
final red = pixel.r.toDouble();
final green = pixel.g.toDouble();
final blue = pixel.b.toDouble();
gray[pixelIndex] = (0.299 * red + 0.587 * green + 0.114 * blue).toInt();
}
}
// Apply Laplacian filter
List<int> laplacian = List<int>.filled(width * height, 0);
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int pixelIndex = y * width + x;
int value = (-4 * gray[pixelIndex] +
gray[pixelIndex - width] +
gray[pixelIndex + width] +
gray[pixelIndex - 1] +
gray[pixelIndex + 1])
.abs();
laplacian[pixelIndex] = value;
}
}
// Calculate variance
double mean = laplacian.reduce((a, b) => a + b) / laplacian.length;
double variance =
laplacian.map((x) => (x - mean) * (x - mean)).reduce((a, b) => a + b) /
laplacian.length;
return variance;
}