具有Laplacian公式的OpenCV可在Android中检测图像是否模糊 [英] OpenCV with Laplacian formula to detect image is blur or not in Android

查看:428
本文介绍了具有Laplacian公式的OpenCV可在Android中检测图像是否模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ios中,我们可以:

In Ios we can:

有没有办法检测是否图像模糊吗?

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

I don't know how to detect image is blur or not in Android or 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;
}

这篇关于具有Laplacian公式的OpenCV可在Android中检测图像是否模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆