Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2016, e-Con Systems.
  4. //
  5. // All rights reserved.
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS.
  8. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  9. // ANY DIRECT/INDIRECT DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF
  10. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////
  15.  
  16. /**********************************************************************
  17. PointCloudApp: Defines the methods to view the pointcloud image of the
  18. scene.
  19. **********************************************************************/
  20.  
  21. #include "PointCloudApp.h"
  22.  
  23. pcl::visualization::CloudViewer *g_PclViewer;//("Stereo Point Cloud Viewer");
  24.  
  25. //Constructor
  26. PointCloudApp::PointCloudApp(void)
  27. {
  28. //init the variables
  29. Trans_x = 0.0;
  30. Trans_y = 0.0;
  31. Trans_z = 15.0;
  32. Rot_x = 0.0;
  33. Rot_y = 0.0;
  34. Rot_z = 0.0;
  35. m_MinDepth = 10000.0;
  36. }
  37.  
  38. //Initialises all the necessary files
  39. int PointCloudApp::Init()
  40. {
  41. cout << " Point Cloud " << endl << endl;
  42. cout << " 3D point cloud is rendered in the window!" << endl << endl;
  43. cout << " Scroll/Move/Zoom(In/Out) on the window to view the Point Cloud data.!" << endl ;
  44. cout << " Press 'Alt + R' on the window to view the initial data!" << endl << endl;
  45.  
  46. //Initialise the camera
  47. if(!_Disparity.InitCamera(true, true))
  48. {
  49. if(DEBUG_ENABLED)
  50. cout << "Camera Initialisation Failed\n";
  51. return FALSE;
  52. }
  53.  
  54. //Camera Streaming
  55. CameraStreaming();
  56.  
  57. return TRUE;
  58. }
  59.  
  60. //Streams the input from the camera
  61. int PointCloudApp::CameraStreaming()
  62. {
  63. char WaitKeyStatus;
  64. bool statusShowPointCloud = TRUE;
  65. int BrightnessVal = 4; //Default value
  66. int ManualExposure = 0;
  67.  
  68. //Window creation
  69. namedWindow("Disparity Map", cv::WINDOW_AUTOSIZE);
  70. namedWindow("Left Image", cv::WINDOW_AUTOSIZE);
  71. namedWindow("Right Image", cv::WINDOW_AUTOSIZE);
  72.  
  73. g_PclViewer = new pcl::visualization::CloudViewer("3D Viewer");
  74.  
  75. cout << endl << "Press q/Q/Esc on the Image Window to quit the application!" << endl;
  76. cout << endl << "Press b/B on the Image Window to change the brightness of the camera" << endl;
  77. cout << endl << "Press a/A on the Image Window to change to Auto exposure of the camera" << endl;
  78. cout << endl << "Press e/E on the Image Window to change the exposure of the camera" << endl << endl;
  79.  
  80. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  81. string Inputline;
  82.  
  83. //streaming
  84. while(1)
  85. {
  86. if(!_Disparity.GrabFrame(&LeftFrame, &RightFrame)) //Reads the frame and returns the rectified image
  87. {
  88. delete g_PclViewer;
  89. cv::destroyAllWindows();
  90. break;
  91. }
  92.  
  93. //Get disparity
  94. _Disparity.GetDisparity(LeftFrame, RightFrame, &gDisparityMap, &gDisparityMap_viz);
  95.  
  96. //Pointcloud display
  97. statusShowPointCloud = ShowPointCloud();
  98.  
  99. imshow("Disparity Map", gDisparityMap_viz);
  100. imshow("Left Image", LeftFrame) ;
  101. imshow("Right Image", RightFrame);
  102.  
  103. //waits for the Key input
  104. WaitKeyStatus = cv::waitKey(1);
  105.  
  106. if(WaitKeyStatus == 'q' || WaitKeyStatus == 'Q' || WaitKeyStatus == 27 || statusShowPointCloud == FALSE)
  107. {
  108. delete g_PclViewer;
  109. cv::destroyAllWindows(); //Closes all the windows
  110. break;
  111. }
  112. else if(WaitKeyStatus == 'b' || WaitKeyStatus == 'B') //Brightness
  113. {
  114. cout << endl << "Enter the Brightness Value, Range(1 to 7): " << endl;
  115.  
  116. BrightnessVal = 0;
  117. cin >> ws; //Ignoring whitespaces
  118.  
  119. while(getline(std::cin, Inputline)) //To avoid floats and Alphanumeric strings
  120. {
  121. std::stringstream ss(Inputline);
  122. if (ss >> BrightnessVal)
  123. {
  124. if (ss.eof())
  125. {
  126. //Setting up the brightness of the camera
  127. if (BrightnessVal >= 1 && BrightnessVal <= 7)
  128. {
  129. //Setting up the brightness
  130. //In opencv-linux 3.1.0, the value needs to be normalized by max value (7)
  131. _Disparity.SetBrightness((double)BrightnessVal / 7.0);
  132. }
  133. else
  134. {
  135. cout << endl << " Value out of Range - Invalid!!" << endl;
  136. }
  137. break;
  138. }
  139. }
  140. BrightnessVal = -1;
  141. break;
  142. }
  143.  
  144. if(BrightnessVal == -1)
  145. {
  146. cout << endl << " Value out of Range - Invalid!!" << endl;
  147. }
  148. }
  149. //Sets up Auto Exposure
  150. else if(WaitKeyStatus == 'a' || WaitKeyStatus == 'A' ) //Auto Exposure
  151. {
  152. _Disparity.SetAutoExposure();
  153. }
  154. else if(WaitKeyStatus == 'e' || WaitKeyStatus == 'E') //Set Exposure
  155. {
  156. cout << endl << "Enter the Exposure Value Range(10 to 1000000 micro seconds): " << endl;
  157.  
  158. ManualExposure = 0;
  159. cin >> ws; //Ignoring whitespaces
  160.  
  161. while(getline(std::cin, Inputline)) //To avoid floats and Alphanumeric strings
  162. {
  163. std::stringstream ss(Inputline);
  164. if (ss >> ManualExposure)
  165. {
  166. if (ss.eof())
  167. {
  168. if(ManualExposure >= SEE3CAM_STEREO_EXPOSURE_MIN && ManualExposure <= SEE3CAM_STEREO_EXPOSURE_MAX)
  169. {
  170. //Setting up the exposure
  171. _Disparity.SetExposure(ManualExposure);
  172. }
  173. else
  174. {
  175. cout << endl << " Value out of Range - Invalid!!" << endl;
  176. }
  177. break;
  178. }
  179. }
  180. ManualExposure = -1;
  181. break;
  182. }
  183.  
  184. if(ManualExposure == -1)
  185. {
  186. cout << endl << " Value out of Range - Invalid!!" << endl;
  187. }
  188. }
  189. }
  190.  
  191. return TRUE;
  192. }
  193.  
  194. int PointCloudApp::ShowPointCloud()
  195. {
  196. #pragma region For Point Cloud Rendering
  197.  
  198. cv::Mat xyz;
  199. reprojectImageTo3D(gDisparityMap, xyz, _Disparity.DepthMap, true);
  200.  
  201. PointCloud<PointXYZRGB>::Ptr point_cloud_ptr (new PointCloud<PointXYZRGB>);
  202. const double max_z = 1.0e4;
  203.  
  204. for (int Row = 0; Row < xyz.rows; Row++)
  205. {
  206. for (int Col = 0; Col < xyz.cols-100; Col++)
  207. {
  208. PointXYZRGB point;
  209. uchar Pix=(uchar)255;
  210. //Just taking the Z Axis alone
  211. cv::Vec3f Depth = xyz.at<cv::Vec3f>(Row,Col);
  212. point.x = Depth[0];
  213. point.y = -Depth[1];
  214. point.z = -Depth[2];
  215.  
  216. if(fabs(Depth[2] - max_z) < FLT_EPSILON || fabs(Depth[2]) > max_z|| Depth[2] < 0 || Depth[2] > m_MinDepth)
  217. continue;
  218.  
  219. Pix = LeftFrame.at<uchar>(Row, Col);
  220. uint32_t rgb = (static_cast<uint32_t>(Pix) << 16 |static_cast<uint32_t>(Pix) << 8 | static_cast<uint32_t>(Pix));
  221.  
  222. point.rgb= *reinterpret_cast<float*>(&rgb);
  223. point_cloud_ptr->points.push_back (point);
  224. }
  225. }
  226. point_cloud_ptr->width = (int) point_cloud_ptr->points.size();
  227. point_cloud_ptr->height = 1;
  228.  
  229. Eigen::Affine3f Transform_Matrix = Eigen::Affine3f::Identity();
  230.  
  231. // Define a translation of 2.5 meters on the x axis.
  232. Transform_Matrix.translation() << Trans_x, Trans_y, Trans_z;
  233.  
  234. // The same rotation matrix as before; tetha radians arround Z axis
  235. Transform_Matrix.rotate (Eigen::AngleAxisf (Rot_x, Eigen::Vector3f::UnitX()));
  236. Transform_Matrix.rotate (Eigen::AngleAxisf (Rot_y, Eigen::Vector3f::UnitY()));
  237. Transform_Matrix.rotate (Eigen::AngleAxisf (Rot_z, Eigen::Vector3f::UnitZ()));
  238.  
  239. PointCloud<PointXYZRGB>::Ptr point_cloud_Transformed_ptr (new PointCloud<PointXYZRGB>);
  240. transformPointCloud (*point_cloud_ptr, *point_cloud_Transformed_ptr, Transform_Matrix);
  241.  
  242. if(!g_PclViewer->wasStopped())
  243. {
  244. g_PclViewer->showCloud(point_cloud_Transformed_ptr);
  245. }
  246. else
  247. {
  248. printf ("\nQuitting PclViewer\n");
  249. return FALSE;
  250. }
  251.  
  252. #pragma endregion For Point Cloud Rendering
  253.  
  254. return TRUE;
  255. }
  256.  
  257. //Main Function
  258. int main()
  259. {
  260. if(DEBUG_ENABLED)
  261. {
  262. cout << "Point Cloud - Application\n";
  263. cout << "-------------------------\n\n";
  264. }
  265.  
  266.  
  267. //Object creation
  268. PointCloudApp _PointCloud;
  269. _PointCloud.Init();
  270.  
  271. if(DEBUG_ENABLED)
  272. cout << "Exit - Point Cloud Application\n";
  273. return TRUE;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement