问题
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