2016-09-29 151 views
1

我正在做一些调试,因此我正在转储映像文件以查看预测和转换。将Caffe caffe :: Datum转换为OpenCV cv :: Mat in C++

我可以从简历创建一个朱古力::基准::垫:

cv::Mat matrix; 
// ... initialize matrix 
caffe::Datum datum; 
caffe::CVMatToDatum(matrix, &datum) 

,但如何从朱古力制作简历::垫::基准?以下代码给出致命异常“基准未编码”:

caffe::Datum datum; 
// ... initialize datum 
cv::Mat matrix; 
matrix = DecodeDatumToCVMat(datum, true); 
+0

没有这样的朱古力代码,但你可以通过在数据以BGR格式的数据将存储重组不同通道的数据写入一个在CV :: Mat对象中作为图像。 – Dale

回答

2

您可以使用以下功能。

cv::Mat DatumToCVMat(const Datum& datum){ 
int datum_channels = datum.channels(); 
int datum_height = datum.height(); 
int datum_width = datum.width(); 

string strData = datum.data(); 
cv::Mat cv_img; 
if (strData.size() != 0) 
{ 
    cv_img.create(datum_height, datum_width, CV_8UC(datum_channels)); 
    const string& data = datum.data(); 
    std::vector<char> vec_data(data.c_str(), data.c_str() + data.size()); 

    for (int h = 0; h < datum_height; ++h) { 
     uchar* ptr = cv_img.ptr<uchar>(h); 
     int img_index = 0; 
     for (int w = 0; w < datum_width; ++w) { 
      for (int c = 0; c < datum_channels; ++c) { 
       int datum_index = (c * datum_height + h) * datum_width + w; 
       ptr[img_index++] = static_cast<uchar>(vec_data[datum_index]); 
      } 
     } 
    } 
} 
else 
{ 
    cv_img.create(datum_height, datum_width, CV_32FC(datum_channels)); 
    for (int h = 0; h < datum_height; ++h) { 
     float* ptr = cv_img.ptr<float>(h); 
     int img_index = 0; 
     for (int w = 0; w < datum_width; ++w) { 
      for (int c = 0; c < datum_channels; ++c) { 
       ptr[img_index++] = static_cast<float>(datum.float_data(img_index)); 
      } 
     } 
    } 
} 
return cv_img; 

}

所有代码: http://lab.deepaivision.com/2017/06/opencv-mat-caffe-datum-datum-mat.html