Advertisement
Alpy

conversion.cc

Aug 18th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include "conversion.h"
  2.  
  3. #include "iostream"
  4.  
  5. Eigen::Tensor<float,4> convertImage_eigen(cv::Mat M)
  6. {
  7. int rows = M.rows;
  8. int cols = M.cols;
  9. int channels = M.channels();
  10.  
  11. Eigen::Tensor<float,4>T(1,rows,cols,channels);
  12. for(int r = 0; r<rows; r++)
  13. {
  14. for(int c = 0; c<cols; c++)
  15. {
  16. cv::Vec3f px = M.at<cv::Vec3f>(r,c);
  17. for(int ch=0; ch<channels; ch++)
  18. {
  19. T(0,r,c,ch) = px[ch];
  20. }
  21. }
  22. }
  23. return T;
  24. }
  25.  
  26. Eigen::Tensor<float,4> convertImage_eigen(std::vector<cv::Mat> L)
  27. {
  28. int size = L.size();
  29. cv::Mat M = L[0];
  30. int rows = M.rows;
  31. int cols = M.cols;
  32. int channels = M.channels();
  33.  
  34. Eigen::Tensor<float,4>T(size,rows,cols,channels);
  35.  
  36. for(int s=0;s<size;s++)
  37. {
  38. M = L[s];
  39. for(int r = 0; r<rows; r++)
  40. {
  41. for(int c = 0; c<cols; c++)
  42. {
  43. cv::Vec3f px = M.at<cv::Vec3f>(r,c);
  44. for(int ch=0; ch<channels; ch++)
  45. {
  46. T(s,r,c,ch) = px[ch];
  47. }
  48. }
  49. }
  50. }
  51. return T;
  52. }
  53.  
  54. tensorflow::Tensor convertImage(cv::Mat M)
  55. {
  56. int rows = M.rows;
  57. int cols = M.cols;
  58. int channels = M.channels();
  59.  
  60. tensorflow::Tensor T(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,rows,cols,channels}));
  61. auto T_mapped = T.tensor<float,4>();
  62. for(int r = 0; r<rows; r++)
  63. {
  64. for(int c = 0; c<cols; c++)
  65. {
  66. cv::Vec3f px = M.at<cv::Vec3f>(r,c);
  67. for(int ch=0; ch<channels; ch++)
  68. {
  69. T_mapped(0,r,c,ch) = px[ch];
  70. }
  71. }
  72. }
  73. return T;
  74. }
  75.  
  76. tensorflow::Tensor convertImage(std::vector<cv::Mat> L)
  77. {
  78. int size = L.size();
  79. cv::Mat M = L[0];
  80. int rows = M.rows;
  81. int cols = M.cols;
  82. int channels = M.channels();
  83.  
  84. tensorflow::Tensor T(tensorflow::DT_FLOAT, tensorflow::TensorShape({size,rows,cols,channels}));
  85. auto T_mapped = T.tensor<float,4>() ;
  86.  
  87. for(int s=0;s<size;s++)
  88. {
  89. M = L[s];
  90. for(int r = 0; r<rows; r++)
  91. {
  92. for(int c = 0; c<cols; c++)
  93. {
  94. cv::Vec3f px = M.at<cv::Vec3f>(r,c);
  95. for(int ch=0; ch<channels; ch++)
  96. {
  97. T_mapped(s,r,c,ch) = px[ch];
  98. }
  99. }
  100. }
  101. }
  102. return T;
  103. }
  104.  
  105. cv::Mat convertTensor_unitary( Eigen::Tensor<float,4> T)
  106. {
  107. int size = T.dimension(0);
  108. assert(size==1);
  109. int rows = T.dimension(1);
  110. int cols = T.dimension(2);
  111. int channels = T.dimension(3);
  112.  
  113. int sz[2] = {rows,cols};
  114. int type;
  115. if(channels == 3)
  116. type = CV_32FC3;
  117. else
  118. type = CV_32F;
  119. cv::Mat M;
  120. M = cv::Mat::zeros( 2, sz, type);
  121.  
  122. for(int r = 0; r<rows; r++)
  123. {
  124. for(int c = 0; c<cols; c++)
  125. {
  126. if(channels == 3)
  127. {
  128. cv::Vec3f px(0,0,0);
  129. for(int ch=0; ch<channels; ch++)
  130. {
  131. px[ch] = T(0,r,c,ch);
  132. }
  133. M.at<cv::Vec3f>(r,c) = px;
  134. }
  135. else
  136. {
  137. M.at<float>(r,c)= T(0,r,c,0);
  138. }
  139. }
  140. }
  141. return M;
  142. }
  143.  
  144. std::vector<cv::Mat> convertTensor( Eigen::Tensor<float,4> T)
  145. {
  146. int size = T.dimension(0);
  147. int rows = T.dimension(1);
  148. int cols = T.dimension(2);
  149. int channels = T.dimension(3);
  150.  
  151. int sz[2] = {rows,cols};
  152. int type;
  153. if (channels == 3)
  154. type = CV_32FC3;
  155. else
  156. type = CV_32F;
  157. std::vector<cv::Mat> L;
  158. for(int s=0;s<size;s++)
  159. {
  160. cv::Mat M;
  161. M = cv::Mat::zeros(2, sz, type);
  162. for(int r = 0; r<rows; r++)
  163. {
  164. for(int c = 0; c<cols; c++)
  165. {
  166. if(channels == 3)
  167. {
  168. cv::Vec3f px(0,0,0);
  169. for(int ch=0; ch<channels; ch++)
  170. {
  171. px[ch] = T(s,r,c,ch);
  172. }
  173. M.at<cv::Vec3f>(r,c) = px;
  174. }
  175. else
  176. {
  177. M.at<float>(r,c)= T(s,r,c,0);
  178. }
  179. }
  180. }
  181. L.push_back(M);
  182. }
  183. return L;
  184. }
  185.  
  186. cv::Mat convertTensor_unitary(tensorflow::Tensor T)
  187. {
  188. auto T_M = T.matrix<float>();
  189. int size = T_M.dimension(0);
  190. assert(size==1);
  191. int rows = T_M.dimension(1);
  192. int cols = T_M.dimension(2);
  193. int channels = T_M.dimension(3);
  194.  
  195. int sz[2] = {rows,cols};
  196. int type;
  197. if(channels == 3)
  198. type = CV_32FC3;
  199. else
  200. type = CV_32F;
  201. cv::Mat M;
  202. M = cv::Mat::zeros( 2, sz, type);
  203.  
  204. for(int r = 0; r<rows; r++)
  205. {
  206. for(int c = 0; c<cols; c++)
  207. {
  208. if(channels == 3)
  209. {
  210. cv::Vec3f px(0,0,0);
  211. for(int ch=0; ch<channels; ch++)
  212. {
  213. px[ch] = T_M(0,r,c,ch);
  214. }
  215. M.at<cv::Vec3f>(r,c) = px;
  216. }
  217. else
  218. {
  219. M.at<float>(r,c)= T_M(0,r,c,0);
  220. }
  221. }
  222. }
  223. return M;
  224. }
  225.  
  226.  
  227. std::vector<cv::Mat> convertTensor(tensorflow::Tensor T)
  228. {
  229. auto T_M = T.matrix<float>();
  230. int size = T_M.dimension(0);
  231. int rows = T_M.dimension(1);
  232. int cols = T_M.dimension(2);
  233. int channels = T_M.dimension(3);
  234.  
  235. int sz[2] = {rows,cols};
  236. int type;
  237. if (channels == 3)
  238. type = CV_32FC3;
  239. else
  240. type = CV_32F;
  241. std::vector<cv::Mat> L;
  242. for(int s=0;s<size;s++)
  243. {
  244. cv::Mat M;
  245. M = cv::Mat::zeros(2, sz, type);
  246. for(int r = 0; r<rows; r++)
  247. {
  248. for(int c = 0; c<cols; c++)
  249. {
  250. if(channels == 3)
  251. {
  252. cv::Vec3f px(0,0,0);
  253. for(int ch=0; ch<channels; ch++)
  254. {
  255. px[ch] = T_M(s,r,c,ch);
  256. }
  257. M.at<cv::Vec3f>(r,c) = px;
  258. }
  259. else
  260. {
  261. M.at<float>(r,c)= T_M(s,r,c,0);
  262. }
  263. }
  264. }
  265. L.push_back(M);
  266. }
  267. return L;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement