http://stackoverflow.com/questions/29491669/real-time-paper-sheet-detection-using-opencv-in-android/29492699#29492699
at srcImg; //you may want to apply Canny or some threshold before searching for contours
List contours = new ArrayList();
Mat hierarchy;
Imgproc.findContours(srcImg, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f mat2fsrc, mat2fdst;
Scalar color = new Scalar(250, 250, 255);
for (int i = 0; i < contours.size(); i++) {
contours.get(i).convertTo(mat2fsrc, CvType.CV_32FC2);
Imgproc.approxPolyDP(mat2fsrc, mat2fdst, 0.01 * Imgproc.arcLength(mat2fsrc, true), true);
mat2fdst.convertTo(contours.get(i), CvType.CV_32S);
Imgproc.drawContours(srcImg, contours, i, color, 2, 8, hierarchy, 0, new Point());
}
===================================================================================================================================================================================================================
http://stackoverflow.com/questions/23134304/crop-out-part-from-images-findcontours-opencv-java
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// reading image
Mat image = Highgui.imread(".\\testing2.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
// clone the image
Mat original = image.clone();
// thresholding the image to make a binary image
Imgproc.threshold(image, image, 100, 128, Imgproc.THRESH_BINARY_INV);
// find the center of the image
double[] centers = {(double)image.width()/2, (double)image.height()/2};
Point image_center = new Point(centers);
// finding the contours
ArrayList contours = new ArrayList();
Mat hierarchy = new Mat();
Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// finding best bounding rectangle for a contour whose distance is closer to the image center that other ones
double d_min = Double.MAX_VALUE;
Rect rect_min = new Rect();
for (MatOfPoint contour : contours) {
Rect rec = Imgproc.boundingRect(contour);
// find the best candidates
if (rec.height > image.height()/2 & rec.width > image.width()/2)
continue;
Point pt1 = new Point((double)rec.x, (double)rec.y);
Point center = new Point(rec.x+(double)(rec.width)/2, rec.y +