Created
May 30, 2018 09:03
-
-
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
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
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