I am trying to sort the bounding boxes with y axis and then x axis but the results I find from tl().x and tl().y are bit confusing and after lot of work I couldn't find anything in documentation. Here are some results please take a look. I want them to be in order from 1 to 30
CODE:
m = Utils.loadResource(MainActivity.this, R.drawable.sheet1, Highgui.CV_LOAD_IMAGE_COLOR);
//Mat original = Utils.loadResource(MainActivity.this, R.drawable.sheet1, Highgui.CV_LOAD_IMAGE_COLOR);
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Imgproc.cvtColor(m, m, Imgproc.COLOR_BGR2GRAY);
Imgproc.medianBlur(m, m,3);
Imgproc.threshold(m, m, 0, 255, Imgproc.THRESH_OTSU);
Core.bitwise_not(m, m);
Imgproc.dilate(m, m, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,118)));
java.util.List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(m.clone(), contours, new Mat() ,Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
Rect rect = Imgproc.boundingRect(contours.get(35));
Toast.makeText(MainActivity.this, "TL:"+rect.tl()+"BR:"+rect.br(), Toast.LENGTH_LONG).show();
EDIT:
This is cropped region and the coordinates showing above are of these boxes.
Original Image: