Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java与OpenCV和Mask_RCNN结合使用_Java_Opencv_Reshape_Mask_Mat - Fatal编程技术网

将Java与OpenCV和Mask_RCNN结合使用

将Java与OpenCV和Mask_RCNN结合使用,java,opencv,reshape,mask,mat,Java,Opencv,Reshape,Mask,Mat,我对Java有很多经验,并且已经编写了自己的图像处理实用程序。我意识到Python有很多教程,但我更倾向于使用Java,因为我想将OpenCV 4.1的一些功能与我现有的Java代码集成在一起。 我的一个实验是尝试使用coco数据集获取由离子图像识别的物体的遮罩 // Give the textGraph and weight files for the model private static final String TEXT_GRAPH = "models/mask_rcnn_incept

我对Java有很多经验,并且已经编写了自己的图像处理实用程序。我意识到Python有很多教程,但我更倾向于使用Java,因为我想将OpenCV 4.1的一些功能与我现有的Java代码集成在一起。 我的一个实验是尝试使用coco数据集获取由离子图像识别的物体的遮罩

// Give the textGraph and weight files for the model
private static final String TEXT_GRAPH = "models/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
private static final String MODEL_WEIGHTS = "models/mask_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb";
private static final String CLASSES_FILE = "models/mscoco_labels.names";

TextFileConsumer consumer = new TextFileConsumer();
File textFile = new File(CLASSES_FILE);
if (textFile.exists()){
    BufferedReader fr1 = new BufferedReader(new FileReader(textFile));
    fr1.lines().forEach(consumer::storeLines);
}
String[] CLASSES = consumer.lines.toArray(new String[0]);
Mat image = Imgcodecs.imread("images/bird.jpg");
Size size = image.size();
int cols = image.cols();
int rows = image.rows();
double h = size.height;
double w = size.width;
int hh = (int)size.height;
int ww = (int)size.width;
Mat blob = org.opencv.dnn.Dnn.blobFromImage(image, 1.0,  new Size(w, h), Scalar.all(0), true, false);

// Load the network
Net net = org.opencv.dnn.Dnn.readNetFromTensorflow(MODEL_WEIGHTS, TEXT_GRAPH);
net.setPreferableBackend(org.opencv.dnn.Dnn.DNN_BACKEND_OPENCV);
net.setPreferableTarget(org.opencv.dnn.Dnn.DNN_TARGET_CPU);
net.setInput(blob);
ArrayList<String> outputlayers = new ArrayList<String>();
ArrayList<Mat> outputMats = new ArrayList<Mat>();
outputlayers.add("detection_out_final");
outputlayers.add("detection_masks");
net.forward(outputMats,outputlayers);
Mat numClasses = outputMats.get(0);
numClasses = numClasses.reshape(1, (int)numClasses.total() / 7);

for (int i = 0; i < numClasses.rows(); ++i) {
    double confidence = numClasses.get(i, 2)[0];
    //System.out.println(confidence);
    if (confidence > 0.2) {
        int classId = (int) numClasses.get(i, 1)[0];
        String label = CLASSES[classId] + ": " + confidence;
        System.out.println(label);
        int left   = (int)(numClasses.get(i, 3)[0] * cols);
        int top    = (int)(numClasses.get(i, 4)[0] * rows);
        int right  = (int)(numClasses.get(i, 5)[0] * cols);
        int bottom = (int)(numClasses.get(i, 6)[0] * rows);
        System.out.println(left + " " + top + " " + right + " " + bottom);
    }
}
Mat numMasks = outputMats.get(1);
//给出模型的文本图和权重文件
私有静态最终字符串TEXT\u GRAPH=“models/mask\u rcnn\u inception\u v2\u coco\u 2018\u 01\u 28.pbtxt”;
私有静态最终字符串模型\u WEIGHTS=“models/mask\u rcnn\u inception\u v2\u coco\u 2018\u 01\u 28/freezed\u interference\u graph.pb”;
私有静态最终字符串类\u FILE=“models/mscoco\u labels.names”;
TextFileConsumer=新的TextFileConsumer();
文件textFile=新文件(类\文件);
if(textFile.exists()){
BufferedReader fr1=新BufferedReader(新文件读取器(文本文件));
fr1.lines().forEach(consumer::storeLines);
}
String[]CLASSES=consumer.lines.toArray(新字符串[0]);
Mat image=Imgcodecs.imread(“images/bird.jpg”);
Size=image.Size();
int cols=image.cols();
int rows=image.rows();
双h=尺寸。高度;
双w=尺寸。宽度;
int hh=(int)size.height;
int ww=(int)size.width;
Mat blob=org.opencv.dnn.dnn.blobFromImage(图像,1.0,新大小(w,h),标量.all(0),真,假);
//加载网络
Net=org.opencv.dnn.dnn.readNetFromTensorflow(模型权重,文本图);
net.setPreferableBackend(org.opencv.dnn.dnn.dnn\u BACKEND\u opencv);
net.setPreferableTarget(org.opencv.dnn.dnn.dnn\u TARGET\u CPU);
net.setInput(blob);
ArrayList outputlayers=新的ArrayList();
ArrayList outputMats=新的ArrayList();
添加(“检测输出最终”);
添加(“检测屏蔽”);
net.forward(outputMats、outputlayers);
Mat numclass=outputMats.get(0);
numClasses=numClasses.reformate(1,(int)numClasses.total()/7);
对于(int i=0;i0.2){
int classId=(int)numclass.get(i,1)[0];
字符串标签=类[classId]+“:”+置信度;
系统输出打印项次(标签);
int left=(int)(numclass.get(i,3)[0]*cols);
inttop=(int)(numclass.get(i,4)[0]*行);
int-right=(int)(numclass.get(i,5)[0]*cols);
intbottom=(int)(numclass.get(i,6)[0]*行);
System.out.println(左+右+上+右+下);
}
}
Mat numMasks=outputMats.get(1);
到目前为止还不错。我现在的问题是试图从numMasks Mat获得分割形状。 尺寸为90x100,总计2025000,类型为-1*-1*CV_32FC1,isCont=true,IsubMat=true。 我知道我也必须重塑,也许是这样:

numMasks=numMasks.reformate(1,(int)numMasks.total()/90)

任何帮助都将不胜感激。 进一步编辑 我想面具是15 x 15。
90 x 100 x 15 x 15得到2025000,这就是我之前看到的总数,因此我需要调整遮罩的大小并与原始图像混合。

我可能会使用此
Mat-restrape=numMasks.restrape(1,(int)(numMasks.total()/(15*15));Mat rfound=重塑行(classId*100);rfound=rfound.重塑(1,15)//这将返回一个9000*225矩阵Mat restrape=numMasks.restrape(1,(int)(numMasks.total()/(15*15));System.out.println(“重塑”+重塑)//找到合适的类Mat rfound=重塑.row((i*100)+classId);系统输出打印项次(“(i*100)+classId”+rfound)//如有必要,获得一个1 x 225的掩模,rfound=rfound.重塑(1225);系统输出打印项次(“rfound.重塑(1225)1st”+rfound)//或形状为15x15rfound=rfound。重塑(1,15);系统输出打印LN(“rfound.重塑(1,15)第二”+rfound)是否也显示了遮罩?我正在使用你的代码,但图像全是黑色的。Mat REFORMATE=numMasks.reFORMATE(1,(int)(numMasks.total()/(15*15));System.out.println(“重塑”+重塑);重塑convertTo(重塑,CV_8UC3);重塑.copyTo(dnnTarget);HighGui.imshow(“CNN”,dnnTarget);我可能对以下内容感兴趣:
Mat restrape=numMasks.restrape(1,(int)(numMasks.total()/(15*15));Mat rfound=重塑行(classId*100);rfound=rfound.重塑(1,15)//这将返回一个9000*225矩阵Mat restrape=numMasks.restrape(1,(int)(numMasks.total()/(15*15));System.out.println(“重塑”+重塑)//找到合适的类Mat rfound=重塑.row((i*100)+classId);系统输出打印项次(“(i*100)+classId”+rfound)//如有必要,获得一个1 x 225的掩模,rfound=rfound.重塑(1225);系统输出打印项次(“rfound.重塑(1225)1st”+rfound)//或形状为15x15rfound=rfound。重塑(1,15);系统输出打印LN(“rfound.重塑(1,15)第二”+rfound)是否也显示了遮罩?我正在使用你的代码,但图像全是黑色的。Mat REFORMATE=numMasks.reFORMATE(1,(int)(numMasks.total()/(15*15));System.out.println(“重塑”+重塑);重塑convertTo(重塑,CV_8UC3);重塑.copyTo(dnnTarget);HighGui.imshow(“CNN”,dnnTarget);