質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

2004閲覧

OpenCV を利用して、TensorFrow学習済みモデルを読み込み、Mask RCNNの処理を行いたい。

isuzugiga

総合スコア0

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/11/21 21:34

前提・実現したいこと

OpenCVを利用して、Mask理を行いたい。

####環境
・opencv4.5.x
・java 14
・ubuntu 20.04 LTS
・mask r-cnn

####利用するmodelとデータ
・mask_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb
・mask_rcnn_inception_v2_coco_2018_01_28.pbtxt
・colors.txt
・mscoco_labels.names

####参考サイト
https://www.pyimagesearch.com/2018/11/19/mask-r-cnn-with-opencv

####質問内容
Javaでopencvを利用たマスク処理を行おうとしています。

①net#forwardを実行したあとに取得される、detection_masksを15*15のマスク画像に変換したいのです。
c++のサンプルでは、classIdの値を元に該当のデータを取得してMatデータを作成しているように見えるのですが、
Javaではどのように実装したら良いのでしょうか?変換方法がわからないので、教えて頂けませんでしょうか?

②Matデータは、100 * 90 * 15 * 15 * CV_32FC1 となっておりますが、
100件のmaskデータ、90classでの分類、15 * 15のサイズとの認識で良いのでしょうか?

c++でのサンプルでは、質問の箇所は、下記のようになっています。

for (int i = 0; i < numDetections; ++i) { float score = outDetections.at<float>(i, 2); if (score > confThreshold) { // Extract the bounding box int classId = static_cast<int>(outDetections.at<float>(i, 1)); int left = static_cast<int>(frame.cols * outDetections.at<float>(i, 3)); int top = static_cast<int>(frame.rows * outDetections.at<float>(i, 4)); int right = static_cast<int>(frame.cols * outDetections.at<float>(i, 5)); int bottom = static_cast<int>(frame.rows * outDetections.at<float>(i, 6)); left = max(0, min(left, frame.cols - 1)); top = max(0, min(top, frame.rows - 1)); right = max(0, min(right, frame.cols - 1)); bottom = max(0, min(bottom, frame.rows - 1)); Rect box = Rect(left, top, right - left + 1, bottom - top + 1); // Extract the mask for the object 質問の箇所 Mat objectMask(outMasks.size[2], outMasks.size[3], CV_32F, outMasks.ptr<float>(i, classId)); // Draw bounding box, colorize and show the mask on the image drawBox(frame, classId, score, box, objectMask); }   }

よろしくお願いいたします。

Javaでの実装

java

1// NCHW 2Mat blobImage = Dnn.blobFromImage(orgImage); 3 4net.setInput(blobImage); 5 6List<Mat> outputBlobs = new ArrayList<>(); 7List<String> outputNames = Arrays.asList(new String[] { "detection_out_final", "detection_masks" }); 8 9net.forward(outputBlobs, outputNames); 10 11Mat detectionOutFinal = outputBlobs.get(0); 12Mat detectionMasks = outputBlobs.get(1); // 本件で質問させていただいている値 13 14// Output size of masks is NxCxHxW where 15// N - number of detected boxes 16// C - number of classes (excluding background) 17// HxW - segmentation shape 18// detections.size(3) == 7 19Mat detection = detectionOutFinal.reshape(1, (int) detectionOutFinal.total() / detectionOutFinal.size(3)); 20// detections.size(2) ==100 21int numDetections = detectionOutFinal.size(2); 22// masks.size(1) == 90 23int numClasses = detectionMasks.size(1); 24 25int thickness = 3; 26double threshold = 0.5; 27 28int height = orgImage.height(); 29int width = orgImage.width(); 30 31Mat dstImage = orgImage.clone(); 32Scalar color = new Scalar(0, 255, 0); 33IntStream.range(0, numDetections).filter(i -> detection.get(i, 2)[0] > threshold).forEach(i -> { 34 int classId = (int) detection.get(i, 1)[0]; 35 String className = names.get(classId); 36 double score = detection.get(i, 2)[0]; 37 double left = detection.get(i, 3)[0] * width; 38 double top = detection.get(i, 4)[0] * height; 39 double right = detection.get(i, 5)[0] * width; 40 double bottom = detection.get(i, 6)[0] * height; 41 42 left = Math.max(0, Math.min(left, height - thickness)); 43 top = Math.max(0, Math.min(top, width - thickness)); 44 right = Math.max(0, Math.min(right, height - thickness)); 45 bottom = Math.max(0, Math.min(bottom, width - thickness)); 46 47 System.out.println(score + " " + classId + " " + className + " " + left + " " + top + " " + right + " " + bottom); 48 Rect rect = new Rect(new double[] { left, top, right - left + 1, bottom - top + 1 }); 49 Imgproc.rectangle(dstImage, rect, color, thickness); 50}); 51 52HighGui.imshow("Input Image", dstImage); 53
# mask_rcnn_inception_v2_coco_2018_01_28.pbtxt                  略 5928 node { 5929 name: "detection_out_final" 5930 op: "DetectionOutput" 5931 input: "variance_encoded/flatten" 5932 input: "SecondStageBoxPredictor/Reshape_1/Reshape" 5933 input: "detection_out/slice/reshape" 5934 attr { 5935 key: "background_label_id" 5936 value { 5937 i: 91 5938 } 5939 }                    略 7577 node { 7578 name: "detection_masks" 7579 op: "Sigmoid" 7580 input: "SecondStageBoxPredictor_1/transpose" 7581 }                    略

試したこと

detectionMasks.get(i, classId)
等...

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Mat detection = detectionOutFinal.reshape(1, (int) detectionOutFinal.total() / detectionOutFinal.size(3));
int numDetections = detectionOutFinal.size(2);
Mat reshapeMask = detectionMasks.reshape(1,
(int) detectionMasks.total() / (detectionMasks.size(2) * detectionMasks.size(3)));

IntStream.range(0, numDetections).filter(i -> detection.get(i, 2)[0] > threshold).forEach(i -> { int classId = (int) detection.get(i, 1)[0]; // color from txt Scalar color = coloers.get(classId % coloers.size()); // axis Rect box = createRect(detection, i, cols, rows); // for rectangle txt String label = createLabel(detection, i, cols, rows, names); // it will be ractangle on image rectangleProcessing(dstImage, label, color, box); // 1 x 255 Mat objectMask = reshapeMask.row(i * detectionMasks.size(0) + classId); // 15 x 15 objectMask = objectMask.reshape(1, detectionMasks.size(2)); // it will be mask on image Mat maskImage = createMask(objectMask, dstImage.size(), color, box); maskImages.add(maskImage); maskProcessing(dstImage, maskImage, box); }); private Mat createMask(Mat objectMask, Size maskSize, Scalar maskColor, Rect box) { Imgproc.resize(objectMask, objectMask, new Size(box.width + 1, box.height + 1)); Mat maskImage = new Mat(); objectMask.convertTo(maskImage, CvType.CV_8U); Mat dstMaskImage = Mat.zeros(maskSize, CvType.CV_8UC3); List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(maskImage, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); Imgproc.drawContours(dstMaskImage, contours, -1, maskColor, Imgproc.FILLED, Imgproc.LINE_8, hierarchy, 100, new Point(box.x, box.y)); return dstMaskImage; }

投稿2020/11/24 00:29

isuzugiga

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問