首页 > 解决方案 > 在 OpenCV 和 Java 中检索 R-CNN 掩码

问题描述

我正在使用 R-CNN 来检索已识别对象的掩码。

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);`

找到对象的边界框并可以显示

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);

根据 Using Java with OpenCV and Mask_RCNN - 堆栈内存溢出,我通过以下方式检索掩码:

   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)));

要绘制蒙版,我使用以下代码:

   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());
   }

结果,我得到了超过 14000 个轮廓,但没有正确的结果。这真的是正确的方法吗?如何选择某个找到的对象?

标签: javaopencv

解决方案


推荐阅读