OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 2001|回复: 1

有人跑过opencv3.x的structured-light相关例程吗?求有偿指导

[复制链接]
发表于 2017-4-21 22:23:07 | 显示全部楼层 |阅读模式
  1. #include <opencv2/imgproc.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/structured_light.hpp>
  4. #include <iostream>
  5. #include <stdio.h>

  6. using namespace cv;
  7. using namespace std;

  8. static const char* keys =
  9. { "{@path | | Path of the folder where the captured pattern images will be save }"
  10.      "{@proj_width      | | Projector width            }"
  11.      "{@proj_height     | | Projector height           }" };

  12. static void help()
  13. {
  14.     cout << "\nThis example shows how to use the "Structured Light module" to acquire a graycode pattern"
  15.          "\nCall (with the two cams connected):\n"
  16.          "./example_structured_light_cap_pattern <path> <proj_width> <proj_height> \n"
  17.          << endl;
  18. }

  19. int main( int argc, char** argv )
  20. {

  21.         structured_light::GrayCodePattern::Params params;

  22.         CommandLineParser parser(argc, argv, keys);
  23.         String path = parser.get<String>(0);
  24.         params.width = parser.get<int>(1);
  25.         params.height = parser.get<int>(2);

  26.         if( path.empty() || params.width < 1 || params.height < 1 )
  27.         {
  28.                 return -1;
  29.         }

  30.         // Set up GraycodePattern with params
  31.         Ptr<structured_light::GrayCodePattern> graycode = structured_light::GrayCodePattern::create( params );

  32.         // Storage for pattern
  33.         vector<Mat> pattern;
  34.         graycode->generate( pattern );

  35.         cout << pattern.size() << " pattern images + 2 images for shadows mask computation to acquire with both cameras"
  36.                         << endl;

  37.         // Generate the all-white and all-black images needed for shadows mask computation
  38.         Mat white;
  39.         Mat black;
  40.         graycode->getImagesForShadowMasks( black, white );

  41.         pattern.push_back( white );
  42.         pattern.push_back( black );

  43.         // Setting pattern window on second monitor (the projector's one)
  44.         namedWindow( "Pattern Window", WINDOW_NORMAL );
  45.         resizeWindow( "Pattern Window", params.width, params.height );
  46.         moveWindow( "Pattern Window", params.width + 316, -20 );
  47.         setWindowProperty( "Pattern Window", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN );

  48.         // Open camera number 1, using libgphoto2
  49.         VideoCapture cap1( CAP_GPHOTO2 );

  50.         if( !cap1.isOpened() )
  51.         {
  52.                 // check if cam1 opened
  53.                 cout << "cam1 not opened!" << endl;
  54.                 help();
  55.                 return -1;
  56.         }

  57.         // Open camera number 2
  58.         VideoCapture cap2( 1 );

  59.         if( !cap2.isOpened() )
  60.         {
  61.                 // check if cam2 opened
  62.                 cout << "cam2 not opened!" << endl;
  63.                 help();
  64.                 return -1;
  65.         }

  66.         // Turning off autofocus
  67.         cap1.set( CAP_PROP_SETTINGS, 1 );
  68.         cap2.set( CAP_PROP_SETTINGS, 1 );

  69.         int i = 0;
  70.         while( i < (int) pattern.size() )
  71.         {
  72.                 cout << "Waiting to save image number " << i + 1 << endl << "Press any key to acquire the photo" << endl;
  73.                 imshow( "Pattern Window", pattern[i] );

  74.                 Mat frame1;
  75.                 Mat frame2;

  76.                 cap1 >> frame1;  // get a new frame from camera 1
  77.                 cap2 >> frame2;  // get a new frame from camera 2

  78.                 if( ( frame1.data ) && ( frame2.data ) )
  79.                 {

  80.                         Mat tmp;
  81.                         cout << "cam 1 size: " << Size( ( int ) cap1.get( CAP_PROP_FRAME_WIDTH ), ( int ) cap1.get( CAP_PROP_FRAME_HEIGHT ) )
  82.                                 << endl;

  83.                         cout << "cam 2 size: " << Size( ( int ) cap2.get( CAP_PROP_FRAME_WIDTH ), ( int ) cap2.get( CAP_PROP_FRAME_HEIGHT ) )
  84.                                 << endl;

  85.                         cout << "zoom cam 1: " << cap1.get( CAP_PROP_ZOOM ) << endl << "zoom cam 2: " << cap2.get( CAP_PROP_ZOOM )
  86.                                 << endl;

  87.                         cout << "focus cam 1: " << cap1.get( CAP_PROP_FOCUS ) << endl << "focus cam 2: " << cap2.get( CAP_PROP_FOCUS )
  88.                                 << endl;

  89.                         cout << "Press enter to save the photo or an other key to re-acquire the photo" << endl;

  90.                         namedWindow( "cam1", WINDOW_NORMAL );
  91.                         resizeWindow( "cam1", 640, 480 );

  92.                         namedWindow( "cam2", WINDOW_NORMAL );
  93.                         resizeWindow( "cam2", 640, 480 );

  94.                         // Moving window of cam2 to see the image at the same time with cam1
  95.                         moveWindow( "cam2", 640 + 75, 0 );

  96.                         // Resizing images to avoid issues for high resolution images, visualizing them as grayscale
  97.                         resize( frame1, tmp, Size( 640, 480 ) );
  98.                         cvtColor( tmp, tmp, COLOR_RGB2GRAY );
  99.                         imshow( "cam1", tmp );
  100.                         resize( frame2, tmp, Size( 640, 480 ) );
  101.                         cvtColor( tmp, tmp, COLOR_RGB2GRAY );
  102.                         imshow( "cam2", tmp );

  103.                         bool save1 = false;
  104.                         bool save2 = false;

  105.                         int key = waitKey( 0 );

  106.                         // Pressing enter, it saves the output
  107.                         if( key == 13 )
  108.                         {
  109.                         ostringstream name;
  110.                         name << i + 1;

  111.                         save1 = imwrite( path + "pattern_cam1_im" + name.str() + ".png", frame1 );
  112.                         save2 = imwrite( path + "pattern_cam2_im" + name.str() + ".png", frame2 );

  113.                         if( ( save1 ) && ( save2 ) )
  114.                         {
  115.                                 cout << "pattern cam1 and cam2 images number " << i + 1 << " saved" << endl << endl;
  116.                                 i++;
  117.                         }
  118.                         else
  119.                         {
  120.                                 cout << "pattern cam1 and cam2 images number " << i + 1 << " NOT saved" << endl << endl << "Retry, check the path"<< endl << endl;
  121.                         }
  122.                         }
  123.                         // Pressing escape, the program closes
  124.                         if( key == 27 )
  125.                         {
  126.                         cout << "Closing program" << endl;
  127.                         }
  128.                 }
  129.                 else
  130.                 {
  131.                         cout << "No frame data, waiting for new frame" << endl;
  132.                 }
  133.                 }

  134.                 // the camera will be deinitialized automatically in VideoCapture destructor
  135.                 return 0;
  136. }
复制代码


回复

使用道具 举报

发表于 2018-1-4 09:38:31 | 显示全部楼层
楼主好,我也在做这方面,你的问题解决了吗?structured_light.hpp文件怎么获得呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-5-3 23:39 , Processed in 0.012374 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表