Skip to content

Instantly share code, notes, and snippets.

@CasiaFan
Created May 30, 2018 09:03
Show Gist options
  • Save CasiaFan/26d417008569bb87a80b26d05bef4a29 to your computer and use it in GitHub Desktop.
Save CasiaFan/26d417008569bb87a80b26d05bef4a29 to your computer and use it in GitHub Desktop.
Convert cv::Mat into tensorflow::Tensor with uint8 datatype using tensorflow c++ API
tensorflow::Tensor matToTensor(const cv::Mat &frame){
int height = mat.rows;
int width = mat.cols;
int depth = mat.channels();
Tensor inputTensor(tensorflow::DT_UINT8, tensorflow::TensorShape({1, height, width, depth}));
auto inputTensorMapped = inputTensor.tensor<tensorflow::uint8, 4>();
cv::Mat frame;
mat.convertTo(frame, CV_8UC3);
const tensorflow::uint8* source_data = (tensorflow::uint8*) frame.data;
for (int y; y<height; y++){
const tensorflow::uint8* source_row = source_data + (y*width*depth);
for (int x; x<width; x++){
const tensorflow::uint8* source_pixel = source_row + (x*depth);
for (int c; c<depth; c++){
const tensorflow::uint8* source_value = source_pixel + c;
inputTensorMapped(0, y, x, c) = *source_value;
}
}
}
return inputTensor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment