-
-
Save Dobiasd/7ef20a0ad47d3f8dc1654a0ca5d1c77c to your computer and use it in GitHub Desktop.
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
// Example code for how to: | |
// - convert an OpenCV image to an fdeep::tensor5 | |
// - convert an fdeep::tensor5 to an OpenCV image | |
// compile with: | |
// g++ -std=c++14 -O3 tensor3_to_cv_mat.cpp -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -o tensor3_to_cv_mat | |
#include <fdeep/fdeep.hpp> | |
#include <opencv2/opencv.hpp> | |
int main() | |
{ | |
const cv::Mat image1 = cv::imread("image.jpg"); | |
// convert cv::Mat to fdeep::tensor5 (image1 to tensor) | |
const fdeep::tensor5 tensor = | |
fdeep::tensor5_from_bytes(image1.ptr(), | |
image1.rows, image1.cols, image1.channels()); | |
// choose the correct pixel type for cv::Mat (gray or RGB/BGR) | |
assert(tensor.shape().depth_ == 1 || tensor.shape().depth_ == 3); | |
const int mat_type = tensor.shape().depth_ == 1 ? CV_8UC1 : CV_8UC3; | |
// convert fdeep::tensor5 to cv::Mat (tensor to image2) | |
const cv::Mat image2( | |
cv::Size(tensor.shape().width_, tensor.shape().height_), mat_type); | |
fdeep::tensor5_into_bytes(tensor, | |
image2.data, image2.rows * image2.cols * image2.channels()); | |
// show both images for visual verification | |
cv::imshow("image1", image1); | |
cv::imshow("image2", image2); | |
cv::waitKey(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment