使用this和this作为参考,我将以下两个函数放在一起,以尝试检测图像是否模糊:
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%的&#34;部分&#34;发现图像模糊,然后整个图像被认为是模糊的。
经测试,我发现结果尚无定论。几乎我投入测试的大多数图像都被宣布为模糊。我甚至尝试改变所使用的拉普拉斯阈值的变化,但没有找到提供一致正确结果的值,这使我认为我做错了什么。
答案 0 :(得分:1)
您可以使用以下方法检测图像是否模糊。
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;
}
}