I’m presently engaged on a doc scanner characteristic for my Flutter app, leveraging the OpenCV SDK for native Android implementation. The target is to detect and spotlight paperwork in real-time throughout the digital camera preview.
Right here’s a snippet of the preprocessing and detection logic I’m utilizing in Java:
// Grayscale and Edge Detection
Mat grey = new Mat();
Imgproc.cvtColor(rgba, grey, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(grey, grey, new Measurement(11, 11), 0);
Mat edges = new Mat();
Imgproc.Canny(grey, edges, 50, 100);
// Contours Detection
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Measurement(5, 5));
Imgproc.dilate(edges, edges, kernel);
Checklist contours = new ArrayList<>();
Imgproc.findContours(edges, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
Collections.type(contours, (lhs, rhs) -> Double.valueOf(Imgproc.contourArea(rhs)).compareTo(Imgproc.contourArea(lhs)));
**The Downside
**Whereas this implementation works moderately nicely below darkish backgrounds, I’m going through vital challenges with shiny or erratically lit backgrounds. Particularly:
Edge detection fails or produces noisy outcomes.
The algorithm struggles to establish doc contours reliably.
**What I’ve Tried
**Adjusting Canny thresholds: Minimal enchancment however not adequate.
Lighting variations: Blended outcomes relying on the background and ambient gentle.
Contour filtering: Sorting by contour space works, however noise in shiny backgrounds nonetheless impacts detection.
Request
I’d respect steering on enhance the doc detection pipeline, particularly in situations with various lighting situations.
Ought to I discover adaptive thresholding or different preprocessing methods?
Are there higher approaches for filtering contours in such situations?
Any really useful OpenCV capabilities or parameters to attempt?
I’d love to listen to any insights, options, or different methods from the neighborhood.