Reputation: 1370
Question 1: What I have tried ? I am building a document scanner app with document recognition and perspective correction. For this I am using OpenCV. Here are the steps :
Resize the image
Bitmap bitmap = CameraImageUtils.decodeSampledBitmapFromResource(data[0], CameraConfigurationUtils.MIN_FRAME_WIDTH, CameraConfigurationUtils.MIN_FRAME_HEIGHT);
Apply Canny Edge detection
Mat srcMat = new Mat(height, width, CvType.CV_8UC3);
Utils.bitmapToMat(sourceBitmap, srcMat);
Mat imgSource = new Mat(srcMat.size(), CvType.CV_8UC1);
Imgproc.cvtColor(srcMat, imgSource, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);
Imgproc.Canny(imgSource, imgSource, 50, 50);
//find the contours
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
Log.d("size", Integer.toString(contours.size()));
MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint largest_contour = contours.get(0);
for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
//compare this contour to the previous largest contour found
if (contourarea > maxArea) {
//check if this contour is a square
MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
int contourSize = (int) temp_contour.total();
MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
if (approxCurve_temp.total() == 4) {
maxArea = contourarea;
maxAreaIdx = idx;
approxCurve = approxCurve_temp;
largest_contour = temp_contour;
}
}
}
What is the problem ? I am trying to draw all the contours but the document itself is never recognized as a Contour.
What am I doing wrong here ? The main goal of the app is to detect the document in the live camera preview and when the user takes a picture, only the document is shown that is reduced in size (< 160kb) and upload the picture.
Question 2: I am using static initializer for OpenCv that will increase the app size and I don't want this. Is there an alternative for OpenCV ?
Upvotes: 0
Views: 1114
Reputation: 68
I can't comment but I'll try to make an answer for your Question 1.
Where is your original Image?
You need to have something like a Binary Image which consists of 0 & 255 color pixels and is a Gray Image (So you only have 1 channel). you need to have a full block of image in order for the OpenCV findContour()
function to work.
By then I'm sure you will find your contour. There is no problem with your code.
PS. You don't even need to use Canny for Contour Detection. You only use it if you really want to get the edges of an Image.
Hope it helps.
Upvotes: 1