opencv — как читать данные по определенным координатам в многомерном Mat: class, используя c ++?

Я пытаюсь использовать модуль MobileNet SSD + глубокая нейронная сеть (dnn) в OpenCV для обнаружения объектов. Я загрузил и успешно использовал модель. В качестве вывода net.forward я получаю объект Mat, содержащий информацию об обнаруженных объектах. К сожалению, я борюсь с «легкой частью работы», с чтением того, что именно было обнаружено.

Вот информация, которую я знаю о выходном объекте Mat:

  • Он имеет 4 измерения.
  • Размер 1 x 1 x number_of_objects_detected x 7.
  • Семь частей информации о каждом объекте были следующими: 1-й — идентификатор класса, 2-й — достоверность, 3-й — 7-й значения ограничивающего прямоугольника.

Я не могу найти ни одного примера C ++, но я нашел много примеров Python. Они читают данные так:

for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]

Какой самый простой способ сделать это в C ++? То есть Мне нужно прочитать данные по определенным координатам в многомерном Mat: class.

Спасибо за вашу помощь. Я довольно новичок в C ++ и иногда находил это подавляющим …

Я использую OpenCV 3.3.0. GitHub с SSD MobileNet, который я использую: https://github.com/chuanqi305/MobileNet-SSD.

Код моей программы:

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include <fstream>
#include <iostream>

using namespace cv;
using namespace cv::dnn;

using namespace std;

// function to create vector of class names
std::vector<String> createClaseNames() {
std::vector<String> classNames;
classNames.push_back("background");
classNames.push_back("aeroplane");
classNames.push_back("bicycle");
classNames.push_back("bird");
classNames.push_back("boat");
classNames.push_back("bottle");
classNames.push_back("bus");
classNames.push_back("car");
classNames.push_back("cat");
classNames.push_back("chair");
classNames.push_back("cow");
classNames.push_back("diningtable");
classNames.push_back("dog");
classNames.push_back("horse");
classNames.push_back("motorbike");
classNames.push_back("person");
classNames.push_back("pottedplant");
classNames.push_back("sheep");
classNames.push_back("sofa");
classNames.push_back("train");
classNames.push_back("tvmonitor");
return classNames;
}

// main function
int main(int argc, char **argv)
{
// set inputs
String modelTxt = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/python/object-detection-deep-learning/MobileNetSSD_deploy.prototxt";
String modelBin = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/python/object-detection-deep-learning/MobileNetSSD_deploy.caffemodel";
String imageFile = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/puppies.jpg";
std::vector<String> classNames = createClaseNames();

//read caffe model
Net net;
try {
net = dnn::readNetFromCaffe(modelTxt, modelBin);
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network." << std::endl;
exit(-1);
}
}

// read image
Mat img = imread(imageFile);

// create input blob
resize(img, img, Size(300, 300));
Mat inputBlob = blobFromImage(img, 0.007843, Size(300, 300), Scalar(127.5)); //Convert Mat to dnn::Blob image batch

// apply the blob on the input layer
net.setInput(inputBlob); //set the network input

// classify the image by applying the blob on the net
Mat detections = net.forward("detection_out"); //compute output

// print some information about detections
std::cout << "dims: " << detections.dims << endl;
std::cout << "size: " << detections.size << endl;

//show image
String winName("image");
imshow(winName, img);

// Wait for keypress
waitKey();

}

0

Решение

Проверьте официальный учебник OpenCV на как сканировать изображения.

Нормальный способ доступа к 3-канальному (то есть цветному) Mat способ будет использовать Mat::at() метод Mat класс, который сильно перегружен для всевозможных опций доступа. В частности, вы можете отправить в массив показателей или вектор индексов.


Вот самый простой пример создания 4D Mat и доступ к конкретному элементу:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
int size[4] = { 2, 2, 5, 7 };
cv::Mat M(4, size, CV_32FC1, cv::Scalar(1));
int indx[4] = { 0, 0, 2, 3 };
std::cout << "M[0, 0, 2, 3] = " << M.at<float>(indx) << std::endl;
}
M[0, 0, 2, 3] = 1
0

Другие решения

Кто-то может найти этот вопрос в контексте использования модуля MobileNet SSD + глубокая нейронная сеть (dnn) в OpenCV для обнаружения объектов. Поэтому здесь я выкладываю уже функциональный код обнаружения объектов. Александр Рейнольдс спасибо за вашу помощь.

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include <fstream>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

// function to create vector of class names
std::vector<String> createClaseNames() {
std::vector<String> classNames;
classNames.push_back("background");
classNames.push_back("aeroplane");
classNames.push_back("bicycle");
classNames.push_back("bird");
classNames.push_back("boat");
classNames.push_back("bottle");
classNames.push_back("bus");
classNames.push_back("car");
classNames.push_back("cat");
classNames.push_back("chair");
classNames.push_back("cow");
classNames.push_back("diningtable");
classNames.push_back("dog");
classNames.push_back("horse");
classNames.push_back("motorbike");
classNames.push_back("person");
classNames.push_back("pottedplant");
classNames.push_back("sheep");
classNames.push_back("sofa");
classNames.push_back("train");
classNames.push_back("tvmonitor");
return classNames;
}

// main function
int main(int argc, char **argv)
{
// set inputs
String modelTxt = "Path to MobileNetSSD_deploy.prototxt";
String modelBin = "Path to MobileNetSSD_deploy.caffemodel";
String imageFile = "Path to test image";
std::vector<String> classNames = createClaseNames();

//read caffe model
Net net;
try {
net = dnn::readNetFromCaffe(modelTxt, modelBin);
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network." << std::endl;
exit(-1);
}
}

// read image
Mat img = imread(imageFile);
Size imgSize = img.size();

// create input blob
Mat img300;
resize(img, img300, Size(300, 300));
Mat inputBlob = blobFromImage(img300, 0.007843, Size(300, 300), Scalar(127.5)); //Convert Mat to dnn::Blob image batch

// apply the blob on the input layer
net.setInput(inputBlob); //set the network input

// classify the image by applying the blob on the net
Mat detections = net.forward("detection_out"); //compute output

// look what the detector found
for (int i=0; i < detections.size[2]; i++) {

// print information into console
cout << "-----------------" << endl;
cout << "Object nr. " << i + 1 << endl;

// detected class
int indxCls[4] = { 0, 0, i, 1 };
int cls = detections.at<float>(indxCls);
std::cout << "class: " << classNames[cls] << endl;

// confidence
int indxCnf[4] = { 0, 0, i, 2 };
float cnf = detections.at<float>(indxCnf);
std::cout << "confidence: " << cnf * 100 << "%" << endl;

// bounding box
int indxBx[4] = { 0, 0, i, 3 };
int indxBy[4] = { 0, 0, i, 4 };
int indxBw[4] = { 0, 0, i, 5 };
int indxBh[4] = { 0, 0, i, 6 };
int Bx = detections.at<float>(indxBx) * imgSize.width;
int By = detections.at<float>(indxBy) * imgSize.height;
int Bw = detections.at<float>(indxBw) * imgSize.width - Bx;
int Bh = detections.at<float>(indxBh) * imgSize.height - By;
std::cout << "bounding box [x, y, w, h]: " << Bx << ", " << By << ", " << Bw << ", " << Bh << endl;

// draw bounding box to image
Rect bbox(Bx, By, Bw, Bh);
rectangle(img, bbox, Scalar(255,0,255),1,8,0);

}
//show image
String winName("image");
imshow(winName, img);

// Wait for keypress
waitKey();

}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector