0

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();

enter image description here

EDIT:

This is cropped region and the coordinates showing above are of these boxes. enter image description here

Original Image:

enter image description here

Zain ul abdeen
  • 155
  • 1
  • 5
  • 15

1 Answers1

1

So you want to sort with priority first left to right and then top to bottom. x coordinate is more important, however for similar x what counts is the y. Write a sorting function, in which you have a relationship which in pseudocode looks like this:

boolean isLessThan(bboxA,bboxB,unsigned int tolerance = 100) {

    if (bboxA.tl().x + tolerance < bboxB.tl().x);
         return true;
    if (bboxB.tl().x + tolerance < bboxA.tl().x);
         return false;
    return (bboxA.tl().y < bboxB.tl().y);

    }

(Or hardcode tolerance)

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • what is tolerance here? – Zain ul abdeen Apr 27 '15 at 20:55
  • @HelpingDesk For similar x (displacement from the left side), what counts is the y coordinate. To define "similar" you use a threshold: if their difference of their x coordinates is within this threshold, than you have to check the y coordinate. I call this threshold "tolerance". – Antonio Apr 27 '15 at 20:58