Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to sort bounding boxes in x-axis with java and android

I succeed to find contours and drawing bounding boxes but when i extract the letters in the bounding boxes they was saved randomly .Is there a method that order bounding boxes from left to right .this is my code :

      //find and draw contours:
     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
       Mat hierarchy = new Mat();
       Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
       for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {

       Imgproc.drawContours(gray, contours, contourIdx, new Scalar(255,0,0),6);


     MatOfPoint2f approxCurve = new MatOfPoint2f();
       //For each contour found
     int k=0;
       for (int i=0; i<contours.size(); i++)

           //Convert contours(i) from MatOfPoint to MatOfPoint2f
           MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );
           //Processing on mMOP2f1 which is in type MatOfPoint2f
           double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
           Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

           //Convert back to MatOfPoint
           MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

           // Get bounding rect of contour
           Rect rect = Imgproc.boundingRect(points);

           // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
       if (approxDistance >1.5){
           Imgproc.rectangle(gray, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(255,0,0),2);

           Mat ROI = gray.submat(rect.y, rect.y + rect.height, rect.x, rect.x + rect.width);

           Imgproc.resize(ROI,ROI, new Size(25,25) );