Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Drawing bounding box in java

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

click to hide/show revision 2
retagged

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

berak gravatar image

Drawing bounding box in java

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