gpt4 book ai didi

c++ - 如何读取高维Mat :class using c++?中特定坐标处的数据

转载 作者:行者123 更新时间:2023-11-28 01:42:07 26 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 中的 MobileNet SSD + 深度神经网络 ( dnn ) 模块进行对象检测。我成功加载并使用了该模型。作为 net.forward 的输出,我获得了 Mat 对象,其中包含有关检测到的对象的信息。不幸的是,我在“工作中最简单的部分”上苦苦挣扎,无法准确阅读检测到的内容。

这是我知道的关于输出 Mat 对象的信息:

  • 它有 4 个维度。
  • 大小为 1 x 1 x number_of_objects_detected x 7。
  • 关于每个对象的七条信息是:第 1 条是类 ID,第 2 条是置信度,第 3-7 条是边界框值。

我找不到任何 C++ 示例,但我找到了很多 Python 示例。他们这样读取数据:

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

在 C++ 中,最简单的方法是什么? IE。我需要读取高维Mat:class中特定坐标的数据。

感谢您的热心帮助。我是 C++ 的新手,有时发现它不知所措......

我正在使用 OpenCV 3.3.0。我正在使用的带有 MobileNet SSD 的 GitHub: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();

}

最佳答案

查看官方 OpenCV 教程 how to scan images .

您访问 3 channel (即颜色)Mat 的正常方式是使用 Mat::at() Mat 类的方法,它对各种访问器选项都重载了。具体来说,您可以发送 array of indicesvector of indices .


这是创建 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

关于c++ - 如何读取高维Mat :class using c++?中特定坐标处的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46728231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com