Advertisement
Guest User

videostill.cpp

a guest
Aug 4th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright: Jan Kučera, 2014
  2. // Code available under GPLv3 license
  3. // License text available at: http://www.gnu.org/copyleft/gpl.html
  4.  
  5. // Program:
  6. // saving stereo stills from webcam in PPM image format for stereo calibration
  7.  
  8. #include "opencv2/opencv.hpp"
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. using namespace cv;
  13. using namespace std;
  14.  
  15. int main()
  16. {
  17.    
  18.     ofstream myfile;
  19.     myfile.open ("filelist.txt");
  20.    
  21.     int index = 0;
  22.    
  23.     // note: camera sides (LEFT and RIGHT) are considered when you are FACING the cameras (looking into their lens), not from camera's point of view
  24.    
  25.     // open cameras
  26.     VideoCapture cap1(1);       // LEFT camera
  27.     VideoCapture cap2(0);       // RIGHT camera
  28.    
  29.     // check if we succeeded
  30.     if(!cap1.isOpened()) return -1;
  31.     if(!cap2.isOpened()) return -1;
  32.    
  33.     // create windows
  34.     namedWindow("edges1",1);
  35.     namedWindow("edges2",1);
  36.  
  37.     // main loop
  38.     for(;;) {
  39.        
  40.         // create matrices for camera images
  41.         Mat frame1;
  42.         Mat frame2;
  43.        
  44.         // detect key pressed
  45.         long key = waitKey(1000);
  46.        
  47.         // get a new frame from cameras
  48.         cap1 >> frame1;    
  49.         cap2 >> frame2;
  50.        
  51.         // show camera images
  52.         imshow("edges1", frame1);
  53.         imshow("edges2", frame2);
  54.  
  55.         // waitkey was originaly here
  56.        
  57.         // for debugging - print key code
  58.         // std::cout << "Key code: " << key;
  59.        
  60.         // if key was pressed
  61.         if (key >= 0) {
  62.            
  63.             // if ESC is pressed - quit
  64.             if (key == 1048603) {
  65.                 myfile.close();
  66.                 return 0;
  67.             }
  68.            
  69.             // if SPACE pressed - save images
  70.             if (key == 1048608) {
  71.                
  72.                 // increment counter
  73.                 index++;
  74.                
  75.                 // define variables for strings
  76.                 std::string fileName1;
  77.                 std::string fileName2;
  78.                 // define variables for conversion of int to string
  79.                 std::stringstream string1;
  80.                 std::stringstream string2;
  81.                
  82.                 // create file paths and names from constant and numbered index
  83.                 // string1 << "images/image_L_" << index << ".ppm";
  84.                 // string2 << "images/image_R_" << index << ".ppm";
  85.                 string1 << "images/image_L_" << index << ".jpg";
  86.                 string2 << "images/image_R_" << index << ".jpg";
  87.                
  88.                 // convert it to string
  89.                 fileName1 = string1.str();
  90.                 fileName2 = string2.str();
  91.                
  92.                 // save image files
  93.                 imwrite(fileName1, frame1);
  94.                 imwrite(fileName2, frame2);
  95.                
  96.                 myfile << fileName1 << "\n";
  97.                 myfile << fileName2 << "\n";
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement