I am working Auto Edge detection On CameraFrame,It Does not Detect Document Edges.Please help Me out. My Code
try {
return findLargestRectangle(inputFrame.rgba());
} catch (Exception e) {
e.printStackTrace();
}
return inputFrame.gray();
private Mat findLargestRectangle(Mat original_image) {
Mat imgSource = original_image;
//Mat untouched = original_image.clone();
//Clone the Image
Mat CloneImage =original_image.clone();
//convert the image to black and white
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
//convert the image to black and white does (8 bit)
Imgproc.Canny(imgSource, imgSource, 50, 50);
//apply gaussian blur to smoothen lines of dots
Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);
//find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint2f maxCurve = new MatOfPoint2f();
List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
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();
Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
if (approxCurve.total() == 4) {
maxCurve = approxCurve;
maxArea = contourarea;
maxAreaIdx = idx;
largest_contours.add(temp_contour);
}
}
}
Scalar GREEN = new Scalar(0,255,0);
Imgproc.line(CloneImage, new Point(maxCurve.get(3, 0)), new Point(maxCurve.get(2, 0)), GREEN, 4);
Imgproc.line(CloneImage, new Point(maxCurve.get(2, 0)), new Point(maxCurve.get(1, 0)), GREEN, 4);
Imgproc.line(CloneImage, new Point(maxCurve.get(1, 0)), new Point(maxCurve.get(0, 0)), GREEN, 4);
Imgproc.line(CloneImage, new Point(maxCurve.get(0, 0)), new Point(maxCurve.get(3, 0)), GREEN, 4);
double temp_double[] = maxCurve.get(0, 0);
Point p1 = new Point(temp_double[0], temp_double[1]);
Imgproc.circle(CloneImage, new Point(p1.x, p1.y), 20, new Scalar(255, 0, 0), 5); //p1 is colored red
String temp_string = "Point 1: (" + p1.x + ", " + p1.y + ")";
temp_double = maxCurve.get(1, 0);
Point p2 = new Point(temp_double[0], temp_double[1]);
Imgproc.circle(CloneImage, new Point(p2.x, p2.y), 20, new Scalar(0, 255, 0), 5); //p2 is colored green
temp_string += "\nPoint 2: (" + p2.x + ", " + p2.y + ")";
temp_double = maxCurve.get(2, 0);
Point p3 = new Point(temp_double[0], temp_double[1]);
Imgproc.circle(CloneImage, new Point(p3.x, p3.y), 20, new Scalar(0, 0, 255), 5); //p3 is colored blue
temp_string += "\nPoint 3: (" + p3.x + ", " + p3.y + ")";
temp_double = maxCurve.get(3, 0);
Point p4 = new Point(temp_double[0], temp_double[1]);
// if(Imgproc.contourArea(contours.get(contourIdx))>100) {
Imgproc.circle(CloneImage, new Point(p4.x, p4.y), 20, new Scalar(0, 255, 255), 5); //p4 is colored violet
// }
temp_string += "\nPoint 4: (" + p4.x + ", " + p4.y + ")";
/* if(isCapture) {
Rect roi = new Rect(40, 100, 100, 120);
Mat cropped = new Mat(CloneImage, roi);
Bitmap bmp = Bitmap.createBitmap(200, 400, Bitmap.Config.ARGB_8888);
Mat tmp = new Mat (200,200,CvType.CV_8UC1,new Scalar(4));
try {
//Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
Imgproc.cvtColor(cropped, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
Utils.matToBitmap(tmp, bmp);
img_capture.setImageBitmap(bmp);
}
catch (CvException e){Log.d("Exception",e.getMessage());}*/
//}
return CloneImage;
}