Android 中使用拉普拉斯公式检测图像是否模糊的 OpenCV

OpenCV with Laplacian formula to detect image is blur or not in Android

在Ios中我们可以:

Is there a way to detect if an image is blurry?

我不知道如何在 Android 或 Java 中检测图像是否模糊?

    private void opencvProcess() {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap image = decodeSampledBitmapFromFile(picFilePath, options, 2000, 2000);
    int l = CvType.CV_8UC1; //8-bit grey scale image
    Mat matImage = new Mat();
    Utils.bitmapToMat(image, matImage);
    Mat matImageGrey = new Mat();
    Imgproc.cvtColor(matImage, matImageGrey, Imgproc.COLOR_BGR2GRAY);

    Bitmap destImage;
    destImage = Bitmap.createBitmap(image);
    Mat dst2 = new Mat();
    Utils.bitmapToMat(destImage, dst2);
    Mat laplacianImage = new Mat();
    dst2.convertTo(laplacianImage, l);
    Imgproc.Laplacian(matImageGrey, laplacianImage, CvType.CV_8U);
    Mat laplacianImage8bit = new Mat();
    laplacianImage.convertTo(laplacianImage8bit, l);

    Bitmap bmp = Bitmap.createBitmap(laplacianImage8bit.cols(), laplacianImage8bit.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(laplacianImage8bit, bmp);
    int[] pixels = new int[bmp.getHeight() * bmp.getWidth()];
    bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight()); // bmp为轮廓图

    int maxLap = -16777216; // 16m
    for (int pixel : pixels) {
        if (pixel > maxLap)
            maxLap = pixel;
    }

    int soglia = -6118750;
    if (maxLap <= soglia) {
        System.out.println("is blur image");
    }
    soglia += 6118750;
    maxLap += 6118750;
    LogUtil.log("图片位置=" + picFilePath
            + "\nimage.w=" + image.getWidth() + ", image.h=" + image.getHeight()
            + "\nmaxLap= " + maxLap + "(清晰范围:0~6118750)"
            + "\n" + Html.fromHtml("<font color='#eb5151'><b>" + (maxLap <= soglia ? "模糊" : "清晰") + "</b></font>"));
    opencvEnd = true;
    isBlur = maxLap <= soglia;
}