Ask Your Question
2

Drawing bounding box in java

asked 2013-12-21 06:21:06 -0600

pablas gravatar image

updated 2014-03-06 01:52:02 -0600

berak gravatar image

Hello, my problem is that i want to draw bounding boxes but i can't translate tutorials from c++/python to java. Now my program can drawing all contours but i want to identify them and draw 4 rectangles from them.

There is code:

public static void getContoursFrame(){

    //Getting edited frame
    contoursFrame = editedFrame.clone();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(contoursFrame, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    contoursCounter = contours.size();
    Imgproc.cvtColor(contoursFrame, contoursFrame, Imgproc.COLOR_GRAY2BGR);

    if (WebcamGame.shouldColorContours == true){
        Imgproc.drawContours(contoursFrame, contours, -1, new Scalar(255, 255, 255), 1); //Everything
        Imgproc.drawContours(contoursFrame, contours, 1, new Scalar(0, 255, 0), 2); //#1 square (green)
        Imgproc.drawContours(contoursFrame, contours, 6, new Scalar(0, 255, 255), 2); //#2 square (yellow)
        Imgproc.drawContours(contoursFrame, contours, 3, new Scalar(0, 0, 255), 2); //#3 square (red)
        Imgproc.drawContours(contoursFrame, contours, 4, new Scalar(255, 0, 0), 2); //#4 square (blue)
    } else {
        Imgproc.drawContours(contoursFrame, contours, -1, new Scalar(0, 255, 255), 1); //Everything
    }

    //Converting to BufferedImage
    MatOfByte byteContoursFrame = new MatOfByte();
    Highgui.imencode(".jpg", contoursFrame, byteContoursFrame);
    byte[] bytes = byteContoursFrame.toArray();
    InputStream in = new ByteArrayInputStream(bytes);
    try {
        imgContours = ImageIO.read(in);
    } catch (IOException e) {
        System.out.println("- Error: Can't save last ContoursFrame");

// e.printStackTrace(); } }

image description

edit retag flag offensive close merge delete

Comments

Have you seen boundingRect?

Haris gravatar imageHaris ( 2013-12-22 01:50:45 -0600 )edit

Here is also a good reference with it's own good links int he comments: http://stackoverflow.com/questions/19093973/android-opencv-draw-rectangle-on-contours

Buddhisthead gravatar imageBuddhisthead ( 2014-03-05 21:14:32 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-03-05 21:07:55 -0600

Buddhisthead gravatar image

updated 2014-03-05 21:11:42 -0600

Once you have your contours, you can walk through them and find the bounding rectangle of each one...

        MatOfPoint2f         approxCurve = new MatOfPoint2f();

    //For each contour found
    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)
        Core.rectangle(contoursFrame, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), (255, 0, 0, 255), 3); 

    }
edit flag offensive delete link more

Comments

Core.rectangle(contoursFrame, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), (255, 0, 0, 255), 3); is having error for me Core.rectangle(contoursFrame, rect.tl(), rect.br(), new Scalar(255, 0, 0),1, 8,0); is working perfectly

Adean Magarn gravatar imageAdean Magarn ( 2014-09-15 13:09:53 -0600 )edit

Hi. Why do you multiply arcLength for 0.02?

MMOSX gravatar imageMMOSX ( 2016-02-09 09:55:36 -0600 )edit

Question Tools

Stats

Asked: 2013-12-21 06:21:06 -0600

Seen: 28,372 times

Last updated: Mar 05 '14