I’m using R-CNN to retrieve a mask of the recognized objects.
String modelWeights="frozen_inference_graph.pb";
String textGraph="mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
Net net = Dnn.readNetFromTensorflow(modelWeights, textGraph);
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
net.setPreferableTarget(Dnn.DNN_TARGET_CPU);
The bounding box of the objects is found an can be displayed
java.util.List<Mat> result = new java.util.ArrayList<Mat>(3);
java.util.List<java.lang.String> outBlobNames = new ArrayList();
outBlobNames.add("detection_out_final");
outBlobNames.add("detection_masks");
net.forward(result, outBlobNames);
According to Using Java with OpenCV and Mask_RCNN - Stack Overflow I’m retrieving the mask by:
Mat numClasses = result.get(0);
numClasses = numClasses.reshape(1, (int)numClasses.total() / 7);
Mat numMasks = result.get(1);
numMasks = numMasks.reshape(1, (int)numMasks.total() / 90);
Mat reshape = numMasks.reshape(1, (int) (numMasks.total() / (15 * 15)));
To paint the mask I’m using the following code:
reshape.convertTo(reshape,CV_8UC3);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(reshape,contours,hierarchy,Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
Scalar color = new Scalar(125);
Imgproc.drawContours(reshape, contours, i, color, 2, 1, hierarchy, 0, new Point());
}
As a result I get more than 14000 Contours but no proper result. Is this really the correct way?
How to select a certain found object?