我需要为Cameraframe上的文档检测边缘创建一个应用程序。在彩色背景中未检测到边缘校正。绘制的轮廓已折叠。请建议
我没有给出预期的结果。
try {
return findLargestRectangle(inputFrame.rgba());
} catch (Exception e) {
e.printStackTrace();
}
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.drawContours(CloneImage, contours, maxAreaIdx, new Scalar(255, 255, 255), 1); //will draw the largest square/rectangle
// Attempt to draw a GREEN box, but it comes out white
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;
}
我的问题 1.检测文档边缘在背景上的不同算法? 2.请给点建议或建议给边修吗? 我的形象偶像 https://drive.google.com/open?id=1oZZ2A4T2TdMcIKzJ5mrg_qR5yX71dGd5