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

喜你入骨 提交于 2019-12-23 03:36:06

问题


In Ios we can:

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

I don't know how to detect image is blur or not in Android or Java?


回答1:


    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;
}


来源:https://stackoverflow.com/questions/36862451/opencv-with-laplacian-formula-to-detect-image-is-blur-or-not-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!