cap_pattern.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include <opencv2/imgproc.hpp>
  42. #include <opencv2/highgui.hpp>
  43. #include <opencv2/structured_light.hpp>
  44. #include <iostream>
  45. #include <stdio.h>
  46. using namespace cv;
  47. using namespace std;
  48. static const char* keys =
  49. { "{@path | | Path of the folder where the captured pattern images will be save }"
  50. "{@proj_width | | Projector width }"
  51. "{@proj_height | | Projector height }" };
  52. static void help()
  53. {
  54. cout << "\nThis example shows how to use the \"Structured Light module\" to acquire a graycode pattern"
  55. "\nCall (with the two cams connected):\n"
  56. "./example_structured_light_cap_pattern <path> <proj_width> <proj_height> \n"
  57. << endl;
  58. }
  59. int main( int argc, char** argv )
  60. {
  61. structured_light::GrayCodePattern::Params params;
  62. CommandLineParser parser( argc, argv, keys );
  63. String path = parser.get<String>( 0 );
  64. params.width = parser.get<int>( 1 );
  65. params.height = parser.get<int>( 2 );
  66. if( path.empty() || params.width < 1 || params.height < 1 )
  67. {
  68. help();
  69. return -1;
  70. }
  71. // Set up GraycodePattern with params
  72. Ptr<structured_light::GrayCodePattern> graycode = structured_light::GrayCodePattern::create( params );
  73. // Storage for pattern
  74. vector<Mat> pattern;
  75. graycode->generate( pattern );
  76. cout << pattern.size() << " pattern images + 2 images for shadows mask computation to acquire with both cameras"
  77. << endl;
  78. // Generate the all-white and all-black images needed for shadows mask computation
  79. Mat white;
  80. Mat black;
  81. graycode->getImagesForShadowMasks( black, white );
  82. pattern.push_back( white );
  83. pattern.push_back( black );
  84. // Setting pattern window on second monitor (the projector's one)
  85. namedWindow( "Pattern Window", WINDOW_NORMAL );
  86. resizeWindow( "Pattern Window", params.width, params.height );
  87. moveWindow( "Pattern Window", params.width + 316, -20 );
  88. setWindowProperty( "Pattern Window", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN );
  89. // Open camera number 1, using libgphoto2
  90. VideoCapture cap1( CAP_GPHOTO2 );
  91. if( !cap1.isOpened() )
  92. {
  93. // check if cam1 opened
  94. cout << "cam1 not opened!" << endl;
  95. help();
  96. return -1;
  97. }
  98. // Open camera number 2
  99. VideoCapture cap2( 1 );
  100. if( !cap2.isOpened() )
  101. {
  102. // check if cam2 opened
  103. cout << "cam2 not opened!" << endl;
  104. help();
  105. return -1;
  106. }
  107. // Turning off autofocus
  108. cap1.set( CAP_PROP_SETTINGS, 1 );
  109. cap2.set( CAP_PROP_SETTINGS, 1 );
  110. int i = 0;
  111. while( i < (int) pattern.size() )
  112. {
  113. cout << "Waiting to save image number " << i + 1 << endl << "Press any key to acquire the photo" << endl;
  114. imshow( "Pattern Window", pattern[i] );
  115. Mat frame1;
  116. Mat frame2;
  117. cap1 >> frame1; // get a new frame from camera 1
  118. cap2 >> frame2; // get a new frame from camera 2
  119. if( ( frame1.data ) && ( frame2.data ) )
  120. {
  121. Mat tmp;
  122. cout << "cam 1 size: " << Size( ( int ) cap1.get( CAP_PROP_FRAME_WIDTH ), ( int ) cap1.get( CAP_PROP_FRAME_HEIGHT ) )
  123. << endl;
  124. cout << "cam 2 size: " << Size( ( int ) cap2.get( CAP_PROP_FRAME_WIDTH ), ( int ) cap2.get( CAP_PROP_FRAME_HEIGHT ) )
  125. << endl;
  126. cout << "zoom cam 1: " << cap1.get( CAP_PROP_ZOOM ) << endl << "zoom cam 2: " << cap2.get( CAP_PROP_ZOOM )
  127. << endl;
  128. cout << "focus cam 1: " << cap1.get( CAP_PROP_FOCUS ) << endl << "focus cam 2: " << cap2.get( CAP_PROP_FOCUS )
  129. << endl;
  130. cout << "Press enter to save the photo or an other key to re-acquire the photo" << endl;
  131. namedWindow( "cam1", WINDOW_NORMAL );
  132. resizeWindow( "cam1", 640, 480 );
  133. namedWindow( "cam2", WINDOW_NORMAL );
  134. resizeWindow( "cam2", 640, 480 );
  135. // Moving window of cam2 to see the image at the same time with cam1
  136. moveWindow( "cam2", 640 + 75, 0 );
  137. // Resizing images to avoid issues for high resolution images, visualizing them as grayscale
  138. resize( frame1, tmp, Size( 640, 480 ), 0, 0, INTER_LINEAR_EXACT);
  139. cvtColor( tmp, tmp, COLOR_RGB2GRAY );
  140. imshow( "cam1", tmp );
  141. resize( frame2, tmp, Size( 640, 480 ), 0, 0, INTER_LINEAR_EXACT);
  142. cvtColor( tmp, tmp, COLOR_RGB2GRAY );
  143. imshow( "cam2", tmp );
  144. bool save1 = false;
  145. bool save2 = false;
  146. int key = waitKey( 0 );
  147. // Pressing enter, it saves the output
  148. if( key == 13 )
  149. {
  150. ostringstream name;
  151. name << i + 1;
  152. save1 = imwrite( path + "pattern_cam1_im" + name.str() + ".png", frame1 );
  153. save2 = imwrite( path + "pattern_cam2_im" + name.str() + ".png", frame2 );
  154. if( ( save1 ) && ( save2 ) )
  155. {
  156. cout << "pattern cam1 and cam2 images number " << i + 1 << " saved" << endl << endl;
  157. i++;
  158. }
  159. else
  160. {
  161. cout << "pattern cam1 and cam2 images number " << i + 1 << " NOT saved" << endl << endl << "Retry, check the path"<< endl << endl;
  162. }
  163. }
  164. // Pressing escape, the program closes
  165. if( key == 27 )
  166. {
  167. cout << "Closing program" << endl;
  168. }
  169. }
  170. else
  171. {
  172. cout << "No frame data, waiting for new frame" << endl;
  173. }
  174. }
  175. // the camera will be deinitialized automatically in VideoCapture destructor
  176. return 0;
  177. }