使用OpenCV检测图像是否模糊

3

参考这篇文章这篇问答,我编写了以下两个函数来尝试检测图像是否模糊:

public static boolean checkIfImageIsBlurred(BitmapRegionDecoder bitmapRegionDecoder) {
    if (bitmapRegionDecoder == null) {
        Timber.e("Expected bitmapRegionDecoder was null");
        return true;
    }

    int loadImageHeight = bitmapRegionDecoder.getHeight();
    int loadImageWidth = bitmapRegionDecoder.getWidth();

    int checkImageTopPosition = 0;
    int checkImageBottomPosition = loadImageHeight / 10;
    int checkImageLeftPosition = 0;
    int checkImageRightPosition = loadImageWidth / 10;

    int totalDividedRectangles = 0;
    int numberOfBlurredRectangles = 0;

    while ((checkImageRightPosition <= loadImageWidth) && (checkImageLeftPosition < checkImageRightPosition)) {
        while ((checkImageBottomPosition <= loadImageHeight) && (checkImageTopPosition < checkImageBottomPosition)) {
            Timber.d("left: " + checkImageLeftPosition + " right: " + checkImageRightPosition + " top: " + checkImageTopPosition + " bottom: " + checkImageBottomPosition);

            Rect rect = new Rect(checkImageLeftPosition,checkImageTopPosition,checkImageRightPosition,checkImageBottomPosition);
            totalDividedRectangles++;

            Bitmap processBitmap = bitmapRegionDecoder.decodeRegion(rect,null);

            if (checkIfImageIsBlurred(processBitmap)) {
                numberOfBlurredRectangles++;
            }

            checkImageTopPosition = checkImageBottomPosition;
            checkImageBottomPosition += (checkImageBottomPosition < (loadImageHeight - checkImageBottomPosition)) ? checkImageBottomPosition: (loadImageHeight - checkImageBottomPosition);
        }

        checkImageTopPosition = 0; //reset to start
        checkImageBottomPosition = loadImageHeight / 10; //reset to start
        checkImageLeftPosition = checkImageRightPosition;
        checkImageRightPosition += (checkImageRightPosition < (loadImageWidth - checkImageRightPosition)) ? checkImageRightPosition : (loadImageWidth - checkImageRightPosition);
    }

    Timber.d("blurred rectangles count = " + numberOfBlurredRectangles + ", total rectangles count = " + totalDividedRectangles);
    return numberOfBlurredRectangles > totalDividedRectangles * 0.50;
}

public static boolean checkIfImageIsBlurred(Bitmap bitmap) {
    if(bitmap == null) {
        Timber.e("Expected bitmap was null");
        return false;
    }

    Mat imageBitmapMat = new Mat(bitmap.getWidth(),bitmap.getHeight(),CvType.CV_8UC1);
    Utils.bitmapToMat(bitmap,imageBitmapMat);

    Mat grayscaleBitmapMat = new Mat();
    Imgproc.cvtColor(imageBitmapMat,grayscaleBitmapMat,Imgproc.COLOR_RGB2GRAY);

    Mat postLaplacianMat = new Mat();
    Imgproc.Laplacian(grayscaleBitmapMat,postLaplacianMat,3);

    MatOfDouble mean = new MatOfDouble();
    MatOfDouble standardDeviation = new MatOfDouble();
    Core.meanStdDev(postLaplacianMat,mean,standardDeviation);

    double result = Math.pow(standardDeviation.get(0,0)[0],2);
    Timber.d("blurry result = " + result);
    return result < 100;       
}

由于从相机拍摄的图像太大,我使用BitmapRegionDecoder获取它们的部分,然后检查该部分整个图像是否模糊。如果拉普拉斯变换的变化小于所定义的阈值,则将该图像声明为模糊,而在这种情况下,该阈值为100(此值从第一个附加文章中选择)。如果发现50%以上的图像“部分”模糊,则认为整个图像模糊。
经过测试,我发现结果不确定。几乎我测试的所有图像都被声明为模糊。我甚至尝试修改使用的拉普拉斯变换的变化阈值,但没有找到一个提供一致正确结果的值,这让我想我做错了什么。

{btsdaf} - Duloren
1个回答

3
您可以使用以下方法来检测图像是否模糊。
 private synchronized boolean isBlurredImage(Bitmap image) {
    try {
        if (image != null) {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inDither = true;
            opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
            int l = CvType.CV_8UC1;
            Mat matImage = new Mat();
            Utils.bitmapToMat(image, matImage);
            Mat matImageGrey = new Mat();
            Imgproc.cvtColor(matImage, matImageGrey, Imgproc.COLOR_BGR2GRAY);

            Mat dst2 = new Mat();
            Utils.bitmapToMat(image, dst2);

            Mat laplacianImage = new Mat();
            dst2.convertTo(laplacianImage, l);
            Imgproc.Laplacian(matImageGrey, laplacianImage, CvType.CV_8U);
            Mat laplacianImage8bit = new Mat();
            laplacianImage.convertTo(laplacianImage8bit, l);
            System.gc();

            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());
            if (bmp != null)
                if (!bmp.isRecycled()) {
                    bmp.recycle();

                }
            int maxLap = -16777216;

            for (int i = 0; i < pixels.length; i++) {

                if (pixels[i] > maxLap) {
                    maxLap = pixels[i];
                }
            }
            int soglia = -6118750;

            if (maxLap < soglia || maxLap == soglia) {


                return true;
            } else {


                return false;
            }
        } else {
            return false;
        }
    } catch (NullPointerException e) {
        return false;
    } catch (OutOfMemoryError e) {
        return false;
    }
}

这种方法对于小尺寸的图像效果良好。但是,当我上传大尺寸的图像时,应用程序会崩溃。 - Vigneswaran A
有点晚了,但如果您正在使用OpenCV的onCameraFrame方法,则可以优化该方法的性能。在我的用例中,我已经有了一个转换后的灰色Mat和原始的rgba Mat。您可以消除许多位图转换:请参见下一个评论中的代码。 - davy307
isBlurredImage(Mat dst2, Mat matImageGrey) { try { if (dst2 != null && matImageGrey != null) { int l = CvType.CV_8UC1; Mat laplacianImage = new Mat(); dst2.convertTo(laplacianImage, l); Imgproc.Laplacian(matImageGrey, laplacianImage, CvType.CV_8U); Mat laplacianImage8bit = new Mat(); laplacianImage.convertTo(laplacianImage8bit, l); System.gc();[...] - davy307

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接