简体   繁体   English

将OpenCV灰度Mat转换为Caffe Blob

[英]Converting OpenCV grayscale Mat to Caffe blob

I've been following an example I was referred to on how to convert an OpenCV Mat into a Caffe object I could make predictions from. 我一直在遵循一个示例,该示例涉及如何将OpenCV Mat转换为可以从中进行预测的Caffe对象。 From what I understand, the first section scales the image and then initialises the caffe class TransformationParameter : 据我了解,第一部分缩放图像,然后初始化caffe类TransformationParameter

const float img_to_net_scale = 0.0039215684;
TransformationParameter input_xform_param;
input_xform_param.set_scale( img_to_net_scale );
DataTransformer<float> input_xformer( input_xform_param, TEST );

Then, the OpenCV Mat "patch" is converted into "input_blob". 然后,将OpenCV Mat“补丁”转换为“ input_blob”。 I've changed this part because I've loaded in my image in grayscale instead of colour. 之所以更改此部分,是因为我以灰度而不是颜色加载了图像。

cv::Mat patch = cv::imread( input_file_str, CV_LOAD_IMAG_GRAYSCALE  );
Blob<float> input_blob;
input_blob.Reshape(1, patch.channels(), patch.rows, patch.cols );
input_xformer.Transform( patch, &input_blob );

Finally, I'm not too sure what this section does - if I already have my OpenCV Mat converted to a Caffe blob, why do I need to push back on the "input" vector and pass it to the net? 最后,我不太确定本节的内容-如果我已经将OpenCV Mat转换为Caffe Blob,为什么还要推回“输入”向量并将其传递给网络? Can't I pass input_blob directly into the net to get my prediction back? 我不能将input_blob直接传递到网络中以重新获得预测吗?

std::vector<Blob<float>*> input;
input.push_back( &input_blob );

std::vector<Blob<float>*> output = net->Forward( input );

You need to push_back your input_blob in order to pass it to net since net is expecting its input as a std::vector of Blobs (in principle, there may be net s that need more than a single input blob to produce output). 您需要push_back您的input_blob才能将其传递给net因为net希望将其输入作为Blobsstd::vector (原则上, net可能需要多个输入Blob才能产生输出)。
Note that you are not copying input_blob into the input vector, but rather passing a pointer to it. 请注意,您不是将input_blob复制到输入向量中,而是将指针传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM