Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "conversion.h"
- #include "iostream"
- Eigen::Tensor<float,4> convertImage_eigen(cv::Mat M)
- {
- int rows = M.rows;
- int cols = M.cols;
- int channels = M.channels();
- Eigen::Tensor<float,4>T(1,rows,cols,channels);
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- cv::Vec3f px = M.at<cv::Vec3f>(r,c);
- for(int ch=0; ch<channels; ch++)
- {
- T(0,r,c,ch) = px[ch];
- }
- }
- }
- return T;
- }
- Eigen::Tensor<float,4> convertImage_eigen(std::vector<cv::Mat> L)
- {
- int size = L.size();
- cv::Mat M = L[0];
- int rows = M.rows;
- int cols = M.cols;
- int channels = M.channels();
- Eigen::Tensor<float,4>T(size,rows,cols,channels);
- for(int s=0;s<size;s++)
- {
- M = L[s];
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- cv::Vec3f px = M.at<cv::Vec3f>(r,c);
- for(int ch=0; ch<channels; ch++)
- {
- T(s,r,c,ch) = px[ch];
- }
- }
- }
- }
- return T;
- }
- tensorflow::Tensor convertImage(cv::Mat M)
- {
- int rows = M.rows;
- int cols = M.cols;
- int channels = M.channels();
- tensorflow::Tensor T(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,rows,cols,channels}));
- auto T_mapped = T.tensor<float,4>();
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- cv::Vec3f px = M.at<cv::Vec3f>(r,c);
- for(int ch=0; ch<channels; ch++)
- {
- T_mapped(0,r,c,ch) = px[ch];
- }
- }
- }
- return T;
- }
- tensorflow::Tensor convertImage(std::vector<cv::Mat> L)
- {
- int size = L.size();
- cv::Mat M = L[0];
- int rows = M.rows;
- int cols = M.cols;
- int channels = M.channels();
- tensorflow::Tensor T(tensorflow::DT_FLOAT, tensorflow::TensorShape({size,rows,cols,channels}));
- auto T_mapped = T.tensor<float,4>() ;
- for(int s=0;s<size;s++)
- {
- M = L[s];
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- cv::Vec3f px = M.at<cv::Vec3f>(r,c);
- for(int ch=0; ch<channels; ch++)
- {
- T_mapped(s,r,c,ch) = px[ch];
- }
- }
- }
- }
- return T;
- }
- cv::Mat convertTensor_unitary( Eigen::Tensor<float,4> T)
- {
- int size = T.dimension(0);
- assert(size==1);
- int rows = T.dimension(1);
- int cols = T.dimension(2);
- int channels = T.dimension(3);
- int sz[2] = {rows,cols};
- int type;
- if(channels == 3)
- type = CV_32FC3;
- else
- type = CV_32F;
- cv::Mat M;
- M = cv::Mat::zeros( 2, sz, type);
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- if(channels == 3)
- {
- cv::Vec3f px(0,0,0);
- for(int ch=0; ch<channels; ch++)
- {
- px[ch] = T(0,r,c,ch);
- }
- M.at<cv::Vec3f>(r,c) = px;
- }
- else
- {
- M.at<float>(r,c)= T(0,r,c,0);
- }
- }
- }
- return M;
- }
- std::vector<cv::Mat> convertTensor( Eigen::Tensor<float,4> T)
- {
- int size = T.dimension(0);
- int rows = T.dimension(1);
- int cols = T.dimension(2);
- int channels = T.dimension(3);
- int sz[2] = {rows,cols};
- int type;
- if (channels == 3)
- type = CV_32FC3;
- else
- type = CV_32F;
- std::vector<cv::Mat> L;
- for(int s=0;s<size;s++)
- {
- cv::Mat M;
- M = cv::Mat::zeros(2, sz, type);
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- if(channels == 3)
- {
- cv::Vec3f px(0,0,0);
- for(int ch=0; ch<channels; ch++)
- {
- px[ch] = T(s,r,c,ch);
- }
- M.at<cv::Vec3f>(r,c) = px;
- }
- else
- {
- M.at<float>(r,c)= T(s,r,c,0);
- }
- }
- }
- L.push_back(M);
- }
- return L;
- }
- cv::Mat convertTensor_unitary(tensorflow::Tensor T)
- {
- auto T_M = T.matrix<float>();
- int size = T_M.dimension(0);
- assert(size==1);
- int rows = T_M.dimension(1);
- int cols = T_M.dimension(2);
- int channels = T_M.dimension(3);
- int sz[2] = {rows,cols};
- int type;
- if(channels == 3)
- type = CV_32FC3;
- else
- type = CV_32F;
- cv::Mat M;
- M = cv::Mat::zeros( 2, sz, type);
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- if(channels == 3)
- {
- cv::Vec3f px(0,0,0);
- for(int ch=0; ch<channels; ch++)
- {
- px[ch] = T_M(0,r,c,ch);
- }
- M.at<cv::Vec3f>(r,c) = px;
- }
- else
- {
- M.at<float>(r,c)= T_M(0,r,c,0);
- }
- }
- }
- return M;
- }
- std::vector<cv::Mat> convertTensor(tensorflow::Tensor T)
- {
- auto T_M = T.matrix<float>();
- int size = T_M.dimension(0);
- int rows = T_M.dimension(1);
- int cols = T_M.dimension(2);
- int channels = T_M.dimension(3);
- int sz[2] = {rows,cols};
- int type;
- if (channels == 3)
- type = CV_32FC3;
- else
- type = CV_32F;
- std::vector<cv::Mat> L;
- for(int s=0;s<size;s++)
- {
- cv::Mat M;
- M = cv::Mat::zeros(2, sz, type);
- for(int r = 0; r<rows; r++)
- {
- for(int c = 0; c<cols; c++)
- {
- if(channels == 3)
- {
- cv::Vec3f px(0,0,0);
- for(int ch=0; ch<channels; ch++)
- {
- px[ch] = T_M(s,r,c,ch);
- }
- M.at<cv::Vec3f>(r,c) = px;
- }
- else
- {
- M.at<float>(r,c)= T_M(s,r,c,0);
- }
- }
- }
- L.push_back(M);
- }
- return L;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement