Advertisement
Guest User

pvapi example template

a guest
Dec 16th, 2013
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. /**********************************************************************************
  2. * Copyright (C) 2008 Metron Hong Kong Limited.  All Rights Reserved.
  3. *
  4. * Reproduction or disclosure of this file or its contents is granted without
  5. * prior written consent of Metron Hong Kong Limited.
  6. ***
  7. * Code name: CaptureSaveMono.cpp
  8. * Written by: Antonio Yu, Chief Consultant
  9. * Date: 1 May, 2008
  10. * Version: 1.0
  11. *
  12. * Adapted by Stijn De Beugher on 2/04/2012
  13. * Tested in combination with a AVT MANTA 201 MonoChrome camera
  14. ***
  15. * Adapted by Steven Puttemans on 03/05/2013
  16. * Changed compatibility to windows environments
  17. **********************************************************************************/
  18.  
  19. #include <PvApi.h>
  20. #include <cxcore.h>
  21. #include <cv.h>
  22. #include <highgui.h>
  23. #include <string.h>
  24. #include <iostream>
  25. #include <time.h>
  26.  
  27. // Added this library in order to be windows compatible
  28. #include <io.h>
  29.  
  30. using namespace std;
  31.  
  32. // camera's data type definition
  33. typedef struct
  34. {
  35.     unsigned long   UID;
  36.     tPvHandle       Handle;
  37.     tPvFrame        Frame;
  38.  
  39. } tCamera;
  40.  
  41. #define CH_MONO 1   // Single channel for mono images
  42.  
  43. int main(int argc, char* argv[])
  44. {
  45.     string input = "";
  46.     string output_folder = "";
  47.     int maxframecounter  = 100;
  48.     string extension = ".pgm";  
  49.     tCamera myCamera;
  50.     tPvCameraInfo   cameraInfo;
  51.     unsigned long   frameSize;
  52.     tPvErr  Errcode;
  53.    
  54.     cout << "Frame capturing configured, starting the actual capture." << endl;
  55.  
  56.     // Initialize the API
  57.     if(!PvInitialize())
  58.     {
  59.         // Wait for the response from a camera after the initialization of the driver
  60.         ////////////////////////////////////////////////////////////       
  61.         clock_t startT, endT;
  62.         startT = clock();
  63.         float start = float(startT/CLOCKS_PER_SEC);
  64.         while(PvCameraCount() == 0)
  65.         {          
  66.             endT = clock();
  67.             float end = float(endT/CLOCKS_PER_SEC);
  68.             if(end>(start+5))
  69.             {              
  70.                 break;
  71.             }
  72.         }
  73.        
  74.         /////////////////////////////////////////////////////////////
  75.         unsigned long numCams = PvCameraList(&cameraInfo, 1, NULL);
  76.         cout << "numcams = \t" << numCams << endl;     
  77.         if (numCams == 1)
  78.         {
  79.             // Get the camera ID
  80.             myCamera.UID = cameraInfo.UniqueId;
  81.             cout << "Camera ID =  \t " << myCamera.UID << endl;    
  82.  
  83.             // Open the camera
  84.             if(!PvCameraOpen(myCamera.UID, ePvAccessMaster, &(myCamera.Handle)))
  85.             {
  86.                 // Get the image size of every capture
  87.                 Errcode = PvAttrUint32Get(myCamera.Handle, "TotalBytesPerFrame", &frameSize);
  88.                 if (Errcode != ePvErrSuccess)
  89.                     throw Errcode;
  90.                
  91.                 cout << "Frame size = \t" << frameSize << endl;
  92.  
  93.                 // Allocate a buffer to store the image
  94.                 memset(&myCamera.Frame, 0, sizeof(tPvFrame));
  95.                 myCamera.Frame.ImageBufferSize = frameSize;
  96.                 myCamera.Frame.ImageBuffer = new char[frameSize];
  97.  
  98.                 // Get the width & height of the image
  99.                 tPvUint32 width, height;
  100.                 PvAttrUint32Get(myCamera.Handle, "Width", &width);
  101.                 PvAttrUint32Get(myCamera.Handle, "Height", &height);
  102.            
  103.                 cout << "Frame width = \t" << width << endl;
  104.                 cout << "Frame height = \t" << height << endl;
  105.  
  106.                 // Start the camera
  107.                 PvCaptureStart(myCamera.Handle);
  108.  
  109.                 // Set the camera to capture continuously
  110.                 Errcode = PvAttrEnumSet(myCamera.Handle, "AcquisitionMode", "Continuous");
  111.                 if (Errcode != ePvErrSuccess)
  112.                     throw Errcode;
  113.                
  114.                 cout <<"start the acquisition" << endl;
  115.                 Errcode = PvCommandRun(myCamera.Handle, "AcquisitionStart");
  116.                 if (Errcode != ePvErrSuccess)
  117.                     throw Errcode;
  118.  
  119.                 Errcode = PvAttrEnumSet(myCamera.Handle, "FrameStartTriggerMode", "Freerun");
  120.                 if (Errcode != ePvErrSuccess)
  121.                     throw Errcode;
  122.                
  123.                 int framecounter = 0;
  124.                 for(framecounter =0;framecounter<maxframecounter;framecounter++)
  125.                 {                  
  126.                     cout << "\r"<< framecounter << flush;                              
  127.                    
  128.                     if(!PvCaptureQueueFrame(myCamera.Handle, &(myCamera.Frame), NULL))
  129.                     {  
  130.                         while(PvCaptureWaitForFrameDone(myCamera.Handle, &(myCamera.Frame), 100) == ePvErrTimeout)
  131.                         {
  132.                         }
  133.                        
  134.                         /////////////////////////////////////////
  135.                         // Here comes the OpenCV part          //
  136.                         // Trying to convert to C++ interface  //
  137.                         /////////////////////////////////////////
  138.  
  139.                         // Create an image header (mono image)
  140.                         IplImage *image = cvCreateImageHeader(cvSize((int)width, (int)height), IPL_DEPTH_8U, CH_MONO);
  141.                        
  142.                         // Set the width for each image
  143.                         image->widthStep = (int)width;
  144.                         image->imageData = (char *)myCamera.Frame.ImageBuffer;  // Points the image to the buffer of the camera
  145.  
  146.                         // Create a unique blueprint for the name!
  147.                         time_t t = time(0);   // get time now
  148.                         struct tm * now = localtime( & t );
  149.                         stringstream date;
  150.                         date << (now->tm_year + 1900) << (now->tm_mon + 1) << now->tm_mday << now->tm_hour << now->tm_min << now->tm_sec;
  151.  
  152.                         stringstream filename;
  153.                         filename << output_folder << "frame_" << date.str() << "_" << framecounter << extension;
  154.  
  155.                         // Show the image in a window
  156.                         cvShowImage("View window", image); cvWaitKey(10);
  157.  
  158.                         // Save the image & release the image
  159.                         cvSaveImage(filename.str().c_str(), image);
  160.                         cvReleaseImageHeader(&image);  
  161.                     }
  162.                 }
  163.  
  164.                 // Stop the acquisition & free the camera
  165.                 Errcode = PvCommandRun(myCamera.Handle, "AcquisitionStop");
  166.                 if (Errcode != ePvErrSuccess)
  167.                     throw Errcode;
  168.  
  169.                 PvCaptureEnd(myCamera.Handle);
  170.                 PvCameraClose(myCamera.Handle);
  171.                
  172.                 cout << endl << "finished" << endl;
  173.             }
  174.             else
  175.                 cout << "open camera error" << endl;
  176.         }
  177.         else
  178.             cout << "camera not found" << endl;        
  179.     }
  180.     else
  181.         cout << "failed to initialise the API" << endl;    
  182.        
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement