Last active
September 20, 2017 03:16
-
-
Save 1292765944/6dc5cdd89de58ffca0bc2a5fe89ba9f8 to your computer and use it in GitHub Desktop.
convert caffe::Datum to cv::Mat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment