Sort bounding boxes with Tl() : Opencv Android

耗尽温柔 提交于 2020-01-06 13:12:08

问题


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:


回答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)



来源:https://stackoverflow.com/questions/29884692/sort-bounding-boxes-with-tl-opencv-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!