cnn_3dobj.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. By downloading, copying, installing or using the software you agree to this license.
  3. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
  9. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  10. Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
  11. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  12. Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  13. Copyright (C) 2015, Itseez Inc., all rights reserved.
  14. Third party copyrights are property of their respective owners.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. * Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19. * Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22. * Neither the names of the copyright holders nor the names of the contributors
  23. may be used to endorse or promote products derived from this software
  24. without specific prior written permission.
  25. This software is provided by the copyright holders and contributors "as is" and
  26. any express or implied warranties, including, but not limited to, the implied
  27. warranties of merchantability and fitness for a particular purpose are disclaimed.
  28. In no event shall copyright holders or contributors be liable for any direct,
  29. indirect, incidental, special, exemplary, or consequential damages
  30. (including, but not limited to, procurement of substitute goods or services;
  31. loss of use, data, or profits; or business interruption) however caused
  32. and on any theory of liability, whether in contract, strict liability,
  33. or tort (including negligence or otherwise) arising in any way out of
  34. the use of this software, even if advised of the possibility of such damage.
  35. */
  36. #ifndef __OPENCV_CNN_3DOBJ_HPP__
  37. #define __OPENCV_CNN_3DOBJ_HPP__
  38. #ifdef __cplusplus
  39. #include <string>
  40. #include <fstream>
  41. #include <vector>
  42. #include <stdio.h>
  43. #include <math.h>
  44. #include <iostream>
  45. #include <set>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #include <dirent.h>
  49. #define CPU_ONLY
  50. #include <caffe/blob.hpp>
  51. #include <caffe/common.hpp>
  52. #include <caffe/net.hpp>
  53. #include <caffe/proto/caffe.pb.h>
  54. #include <caffe/util/io.hpp>
  55. #include "opencv2/viz/vizcore.hpp"
  56. #include "opencv2/highgui.hpp"
  57. #include "opencv2/highgui/highgui_c.h"
  58. #include "opencv2/imgproc.hpp"
  59. /** @defgroup cnn_3dobj 3D object recognition and pose estimation API
  60. As CNN based learning algorithm shows better performance on the classification issues,
  61. the rich labeled data could be more useful in the training stage. 3D object classification and pose estimation
  62. is a jointed mission aiming at separate different posed apart in the descriptor form.
  63. In the training stage, we prepare 2D training images generated from our module with their
  64. class label and pose label. We fully exploit the information lies in their labels
  65. by using a triplet and pair-wise jointed loss function in CNN training.
  66. As CNN based learning algorithm shows better performance on the classification issues,
  67. the rich labeled data could be more useful in the training stage. 3D object classification and pose estimation
  68. is a jointed mission aiming at separate different posea apart in the descriptor form.
  69. In the training stage, we prepare 2D training images generated from our module with their
  70. class label and pose label. We fully exploit the information that lies in their labels
  71. by using a triplet and pair-wise jointed loss function in CNN training.
  72. Both class and pose label are in consideration in the triplet loss. The loss score
  73. will be smaller when features from the same class and same pose is more similar
  74. and features from different classes or different poses will lead to a much larger loss score.
  75. This loss is also jointed with a pair wise component to make sure the loss is never be zero
  76. and have a restriction on the model scale.
  77. About the training and feature extraction process, it is a rough implementation by using OpenCV
  78. and Caffe from the idea of Paul Wohlhart. The principal purpose of this API is constructing
  79. a well labeled database from .ply models for CNN training with triplet loss and extracting features
  80. with the constructed model for prediction or other purpose of pattern recognition, algorithms into two main Class:
  81. **icoSphere: methods belonging to this class generates 2D images from a 3D model, together with their class and pose from camera view labels.
  82. **descriptorExtractor: methods belonging to this class extract descriptors from 2D images which is
  83. discriminant on category prediction and pose estimation.
  84. @note This API need Caffe with triplet version which is designed for this module
  85. <https://github.com/Wangyida/caffe/tree/cnn_triplet>.
  86. */
  87. namespace cv
  88. {
  89. namespace cnn_3dobj
  90. {
  91. //! @addtogroup cnn_3dobj
  92. //! @{
  93. /** @brief Icosohedron based camera view data generator.
  94. The class create some sphere views of camera towards a 3D object meshed from .ply files @cite hinterstoisser2008panter .
  95. */
  96. /************************************ Data Generation Class ************************************/
  97. class CV_EXPORTS_W icoSphere
  98. {
  99. private:
  100. /** @brief X position of one base point on the initial Icosohedron sphere,
  101. Y is set to be 0 as default.
  102. */
  103. float X;
  104. /** @brief Z position of one base point on the initial Icosohedron sphere.
  105. */
  106. float Z;
  107. /** @brief A threshold for the dupicated points elimination.
  108. */
  109. float diff;
  110. /** @brief Temp camera position for duplex position elimination.
  111. */
  112. std::vector<cv::Point3d> CameraPos_temp;
  113. /** @brief Make all view points having the same distance from the focal point used by the camera view.
  114. */
  115. CV_WRAP void norm(float v[]);
  116. /** @brief Add a new view point.
  117. */
  118. CV_WRAP void add(float v[]);
  119. /** @brief Generate new view points from all triangles.
  120. */
  121. CV_WRAP void subdivide(float v1[], float v2[], float v3[], int depth);
  122. public:
  123. /** @brief Camera position on the sphere after duplicated points elimination.
  124. */
  125. std::vector<cv::Point3d> CameraPos;
  126. /** @brief Generating a sphere by mean of a iteration based points selection process.
  127. @param radius_in Another radius used for adjusting the view distance.
  128. @param depth_in Number of interations for increasing the points on sphere.
  129. */
  130. icoSphere(float radius_in, int depth_in);
  131. /** @brief Get the center of points on surface in .ply model.
  132. @param cloud Point cloud used for computing the center point.
  133. */
  134. CV_WRAP cv::Point3d getCenter(cv::Mat cloud);
  135. /** @brief Get the proper camera radius from the view point to the center of model.
  136. @param cloud Point cloud used for computing the center point.
  137. @param center center point of the point cloud.
  138. */
  139. CV_WRAP float getRadius(cv::Mat cloud, cv::Point3d center);
  140. /** @brief Suit the position of bytes in 4 byte data structure for particular system.
  141. */
  142. CV_WRAP static int swapEndian(int val);
  143. /** @brief Create header in binary files collecting the image data and label.
  144. @param num_item Number of items.
  145. @param rows Rows of a single sample image.
  146. @param cols Columns of a single sample image.
  147. @param headerPath Path where the header will be stored.
  148. */
  149. CV_WRAP static void createHeader(int num_item, int rows, int cols, const char* headerPath);
  150. /** @brief Write binary files used for training in other open source project including Caffe.
  151. @param filenameImg Path which including a set of images.
  152. @param binaryPath Path which will output a binary file.
  153. @param headerPath Path which header belongs to.
  154. @param num_item Number of samples.
  155. @param label_class Class label of the sample.
  156. @param x Pose label of X.
  157. @param y Pose label of Y.
  158. @param z Pose label of Z.
  159. @param isrgb Option for choice of using RGB images or not.
  160. */
  161. CV_WRAP static void writeBinaryfile(String filenameImg, const char* binaryPath, const char* headerPath, int num_item, int label_class, int x, int y, int z, int isrgb);
  162. };
  163. /** @brief Caffe based 3D images descriptor.
  164. A class to extract features from an image. The so obtained descriptors can be used for classification and pose estimation goals @cite wohlhart15.
  165. */
  166. /************************************ Feature Extraction Class ************************************/
  167. class CV_EXPORTS_W descriptorExtractor
  168. {
  169. private:
  170. caffe::Net<float>* convnet;
  171. cv::Size input_geometry;
  172. int num_channels;
  173. bool net_set;
  174. int net_ready;
  175. cv::Mat mean_;
  176. String deviceType;
  177. int deviceId;
  178. /** @brief Load the mean file in binaryproto format if it is needed.
  179. @param mean_file Path of mean file which stores the mean of training images, it is usually generated by Caffe tool.
  180. */
  181. void setMean(const String& mean_file);
  182. /** @brief Wrap the input layer of the network in separate cv::Mat objects(one per channel).
  183. This way we save one memcpy operation and we don't need to rely on cudaMemcpy2D.
  184. The last preprocessing operation will write the separate channels directly to the input layer.
  185. */
  186. void wrapInput(std::vector<cv::Mat>* input_channels);
  187. /** @brief Convert the input image to the input image format of the network.
  188. */
  189. void preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels);
  190. public:
  191. /** @brief Set the device for feature extraction, if the GPU is used, there should be a device_id.
  192. @param device_type CPU or GPU.
  193. @param device_id ID of GPU.
  194. */
  195. descriptorExtractor(const String& device_type, int device_id = 0);
  196. /** @brief Get device type information for feature extraction.
  197. */
  198. String getDeviceType();
  199. /** @brief Get device ID information for feature extraction.
  200. */
  201. int getDeviceId();
  202. /** @brief Set device type information for feature extraction.
  203. Useful to change device without the need to reload the net.
  204. @param device_type CPU or GPU.
  205. */
  206. void setDeviceType(const String& device_type);
  207. /** @brief Set device ID information for feature extraction.
  208. Useful to change device without the need to reload the net. Only used for GPU.
  209. @param device_id ID of GPU.
  210. */
  211. void setDeviceId(const int& device_id);
  212. /** @brief Initiate a classification structure, the net work parameter is stored in model_file,
  213. the network structure is stored in trained_file, you can decide whether to use mean images or not.
  214. @param model_file Path of caffemodel which including all parameters in CNN.
  215. @param trained_file Path of prototxt which defining the structure of CNN.
  216. @param mean_file Path of mean file(option).
  217. */
  218. void loadNet(const String& model_file, const String& trained_file, const String& mean_file = "");
  219. /** @brief Extract features from a single image or from a vector of images.
  220. If loadNet was not called before, this method invocation will fail.
  221. @param inputimg Input images.
  222. @param feature Output features.
  223. @param feature_blob Layer which the feature is extracted from.
  224. */
  225. void extract(InputArrayOfArrays inputimg, OutputArray feature, String feature_blob);
  226. };
  227. //! @}
  228. }
  229. }
  230. #endif /* CNN_3DOBJ_HPP_ */
  231. #endif