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();
}
}
Have you seen boundingRect?
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