Page 1 of 1

Java OpenCV Laplacian Blur -Erkennung gibt niedrige Punktzahlen für klare Bilder - wie kann man den richtigen Schwellenw

Posted: 30 Apr 2025, 22:47
by Anonymous
Bildbeschreibung Eingeben hier eingeben. Ich bin mir nicht sicher, ob mein Schwellenwert falsch ist oder ob die Logik angepasst werden muss. < /P>

Code: Select all

    package com.example.validator;

import org.bytedeco.javacpp.Loader;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class BlurDetetion {

static{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load OpenCV native library
}`

-
public static boolean isImageBlurry(String imagePath, double threshold) {
Mat image = Imgcodecs.imread(imagePath, Imgcodecs.IMREAD_ANYCOLOR);
if (image.empty()) {
System.err.println("Cannot read image: " + imagePath);
return false;
}
System.out.println("Image size: "+image.size());
Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);

Mat laplacian = new Mat();
Imgproc.Laplacian(image, laplacian, CvType.CV_64F);
// Imgcodecs.imwrite("D:\\id_ card image\\lisence.jpeg", laplacian);

MatOfDouble mean = new MatOfDouble();
MatOfDouble stddev = new MatOfDouble();
Core.meanStdDev(laplacian, mean, stddev);
double blurScore = stddev.get(0, 0)[0];
double variance = Math.pow(blurScore, 2);
//double variance = Math.pow(stddev.get(0, 0)[0], 2);
System.out.println("Laplacian variance: " + variance);

System.out.println("Blur score: "+blurScore);
// If variance is lower than threshold, image is considered blurry
return blurScore < threshold;
}

public static void main(String[] args) {
//Loader.load(org.bytedeco.opencv.opencv_java.class);
String imagePath = "D:/id_ card image/license.jpeg"; // Replace with your image path
double blurThreshold = 100.0;  // Tune this value based on image resolution/quality

boolean blurred = isImageBlurry(imagePath, blurThreshold);
System.out.println("Is image blurry? " + blurred);
}
}

`

[**enter image description here**][1]

[1]: https://i.sstatic.net/I7jhIHWk.webp