rainyl / opencv_dart

OpenCV bindings for Dart language and Flutter. Support Asynchronous Now!
https://pub.dev/packages/opencv_dart
Apache License 2.0
112 stars 14 forks source link

Blur Detection on captured Image in flutter #34

Closed VishGoria closed 5 months ago

VishGoria commented 5 months ago

I'm trying to utilize this package in my flutter app where the users can capture and upload their documents.

Is there any possible way to detect whether the captured Image is blur or not so can ask for a retake? So far I've tried to make the following method but doesn't seems to work as expected:

` Future detectBlur(File file) async { // Load image from assets or other sources final imageData = file.readAsBytesSync(); // Your image loading function

// Convert image data to OpenCV format
final src = imdecode(imageData, IMREAD_COLOR);

// Convert image to grayscale
final gray = cvtColor(src, COLOR_BGR2GRAY);
// Calculate Laplacian variance as a measure of blur
final laplac = laplacian(gray, MatType.CV_64F);
//  MatOfDouble mean = new MatOfDouble();
//   MatOfDouble stddev = new MatOfDouble();
final meanStdDevResult = meanStdDev(laplac);
final result = MeanStdDevResult(mean: meanStdDevResult.$1, stddev: meanStdDevResult.$2);

// Extract standard deviation values
final stddevValues = <double>[];
final data = result.stddev.data;
for (var i = 0; i < data.length; i += result.stddev.elemSize) {
  final value = data.buffer.asFloat64List()[i ~/ result.stddev.elemSize];
  stddevValues.add(value);
}

// Calculate the variance
final variance = stddevValues.isNotEmpty ? stddevValues[0] * stddevValues[0] : 0;

// Determine if the image is blurry based on the variance threshold
const threshold = 100.0; // Adjust as needed
final isBlurry = variance < threshold;

debugPrint('Image is blurry: $isBlurry');

// Clean up
src.release();
gray.release();
laplac.release();

}`

Thanks

rainyl commented 5 months ago

@VishGoria hello, thanks for using this package, BUT:

  1. This is not the problem of opencv_dart itself.
  2. I am not your employee, you paid me nothing.
  3. You even didn't star this project and ignored my instructions in Question template.

I suggest you to search on google rather than wasting my time here. Closed.

VishGoria commented 5 months ago

Hello @rainyl

Thank you for taking the time to respond. I understand your frustration, but I believe constructive dialogue is essential for the improvement of any project. Asking questions and discussing scenarios are valuable contributions toward the success of this package. Regarding starring the project, I assure you I have already done so.

Let's foster a supportive community where everyone feels welcome to seek help and contribute. Thank you for your understanding.

Best.

rainyl commented 5 months ago

It seems calculating the var/sd of a mat is not elegant, i will add more methods to mat.

For now, you can use mat.at() or mat.tolist() to get the data of stddev.