Advertisement
Guest User

OpenCV_manta

a guest
Oct 3rd, 2014
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. // --------------------------------------------------------------------------------
  2. // Some remarks for ensuring the correct working of the interface between camera
  3. // and the pc from which you will capture data
  4. //
  5. // FIRST CONFIGURE IP SETTINGS
  6. // - Change the IP address of your pc to 169.254.1.1
  7. // - Change the subnet mask of your pc to 255.255.0.0
  8. // - Change the gateway of your pc to 169.254.1.2
  9. //
  10. // CHANGE SOME NETWORK CARD SETTINGS
  11. // - sudo ifconfig [eth0] mtu 9000 - or 9016 ideally if your card supports that
  12. //
  13. // MAKE SURE THE CORRECT INCLUDES ARE MADE IN THE FOLLOWING ORDER
  14. // - /opt/AVT_sdk/lib-pc/x64/4.5/libPvAPI.a
  15. // - /usr/lib/x86_64-linux-gnu/librt.a
  16. // - /usr/lib/x86_64-linux-gnu/libpthread.a
  17. //
  18. // ADD THE PVAPI HEADER TO YOUR PROJECT
  19. //
  20. // LINK THE OPENCV DEPENDENT LIBRARIES USING THE ADDITIONAL LINKER OPTION
  21. //  `pkg-config opencv --libs`
  22. // --------------------------------------------------------------------------------
  23.  
  24. // Tell the system that you are on a 64 bit linux based system --> needed for PvAPI
  25. #define _x64 1
  26. #define _LINUX 1
  27.  
  28. // Link to the AVT technologies camera library for AVT Manta
  29. // Make sure you download the PvApi from: http://www.alliedvisiontec.com/us/products/legacy.html
  30. #include "PvApi.h"
  31.  
  32. // Link to the necessary OpenCV libraries
  33. #include "opencv2/core/core.hpp"
  34. #include "opencv2/imgproc/imgproc.hpp"
  35. #include "opencv2/highgui/highgui.hpp"
  36.  
  37. // Functionality to provide extra output
  38. #include <string.h>
  39. #include <iostream>
  40. #include <time.h>
  41.  
  42. using namespace std;
  43. using namespace cv;
  44.  
  45. // camera's data type definition
  46. typedef struct
  47. {
  48.     unsigned long   UID;
  49.     tPvHandle       Handle;
  50.     tPvFrame        Frame;
  51.  
  52. } tCamera;
  53.  
  54. #define CH_MONO 1   // Single channel for mono images
  55.  
  56. int main(int argc, char* argv[])
  57. {
  58.     tCamera     myCamera;
  59.     tPvCameraInfo   cameraInfo;
  60.     unsigned long   frameSize;
  61.     tPvErr      Errcode;
  62.  
  63.     if( argc == 1 ){
  64.         cout << "This script will stream data from an AVT MANTA GigE camera using OpenCV C++ style interface." << endl;
  65.             cout << "AVT_manta_camera.exe <resolution width> <resolution heigth>" << endl;
  66.         return 0;
  67.     }
  68.  
  69.     // Be sure to move the windows to correct location
  70.     // This is done to ensure that the window takes as much of the screen as possible, but can be commented out
  71.     namedWindow("View window",1);
  72.     moveWindow("View window", 50, 50);
  73.  
  74.     // Initialize the API
  75.     if(!PvInitialize())
  76.     {
  77.         // Wait for the response from a camera after the initialization of the driver
  78.         // This is done by checking if camera's are found yet
  79.         ////////////////////////////////////////////////////////////
  80.         while(PvCameraCount() == 0)
  81.         {
  82.             waitKey(15);
  83.         }
  84.  
  85.         // Debugging output to see if camera was found or not
  86.         cout << "Camera found, start initialisation." << endl;
  87.  
  88.         /////////////////////////////////////////////////////////////
  89.         if ( PvCameraList(&cameraInfo, 1, NULL) == 1)
  90.         {
  91.             // Get the camera ID
  92.             myCamera.UID = cameraInfo.UniqueId;
  93.  
  94.             // Open the camera
  95.             if( !PvCameraOpen(myCamera.UID, ePvAccessMaster, &(myCamera.Handle)) )
  96.             {
  97.                 //debug
  98.                 cout << "Camera opened succesfully." << endl;
  99.  
  100.                 // Get the image size of every capture
  101.                 PvAttrUint32Get(myCamera.Handle, "TotalBytesPerFrame", &frameSize);
  102.  
  103.                 // Allocate a buffer to store the image
  104.                 memset(&myCamera.Frame, 0, sizeof(tPvFrame));
  105.                 myCamera.Frame.ImageBufferSize = frameSize;
  106.                 myCamera.Frame.ImageBuffer = new char[frameSize];
  107.  
  108.                 // Set maximum camera parameters - camera specific
  109.                 // Code will generate an input window from the center with the size you want
  110.                 // Therefore you need to fill in the maximal camera resolution
  111.                 int max_width = 1624;
  112.                 int max_heigth = 1234;
  113.  
  114.                 int center_x = max_width / 2;
  115.                 int center_y = max_heigth / 2;
  116.  
  117.                 int frame_width = atoi(argv[1]);
  118.                 int frame_heigth = atoi(argv[2]);
  119.  
  120.                 // Set the manta camera parameters to get wanted frame size retrieved
  121.                 PvAttrUint32Set(myCamera.Handle, "RegionX", center_x - (frame_width / 2) );
  122.                 PvAttrUint32Set(myCamera.Handle, "RegionY", center_y - (frame_heigth / 2));
  123.                 PvAttrUint32Set(myCamera.Handle, "Width", frame_width);
  124.                 PvAttrUint32Set(myCamera.Handle, "Height", frame_heigth);
  125.  
  126.                 // Start the camera
  127.                 PvCaptureStart(myCamera.Handle);
  128.  
  129.                 // Set the camera to capture continuously
  130.                 PvAttrEnumSet(myCamera.Handle, "AcquisitionMode", "Continuous");
  131.                 PvCommandRun(myCamera.Handle, "AcquisitionStart");
  132.                 PvAttrEnumSet(myCamera.Handle, "FrameStartTriggerMode", "Freerun");
  133.  
  134.                 // Create infinite loop - break out when condition is met
  135.                 for(;;)
  136.                 {
  137.                     if(!PvCaptureQueueFrame(myCamera.Handle, &(myCamera.Frame), NULL))
  138.                     {
  139.                         while(PvCaptureWaitForFrameDone(myCamera.Handle, &(myCamera.Frame), 100) == ePvErrTimeout)
  140.                         {
  141.                         }
  142.  
  143.                         ////////////////////////////////////////////////////////
  144.                         // Here comes the OpenCV functionality for each frame //
  145.                         ////////////////////////////////////////////////////////
  146.  
  147.                         // Create an image header (mono image)
  148.                         // Push ImageBuffer data into the image matrix
  149.                         Mat image = Mat(frame_heigth, frame_width, CV_8UC1);
  150.                         image.data = (uchar *)myCamera.Frame.ImageBuffer;
  151.  
  152.                         // Show the actual frame
  153.                         // Wait 10 ms to have an actual window visualisation
  154.                         imshow("View window", image);
  155.                         waitKey(10);
  156.  
  157.                         // Release the image data
  158.                         image.release();
  159.                     }
  160.                 }
  161.  
  162.                 // Stop the acquisition & free the camera
  163.                 Errcode = PvCommandRun(myCamera.Handle, "AcquisitionStop");
  164.                 if (Errcode != ePvErrSuccess)
  165.                     throw Errcode;
  166.  
  167.                 PvCaptureEnd(myCamera.Handle);
  168.                 PvCameraClose(myCamera.Handle);
  169.  
  170.                 cout << endl << "finished" << endl;
  171.             }
  172.             else
  173.             cout << "open camera error" << endl;
  174.         }
  175.         else
  176.         cout << "camera not found" << endl;
  177.     }
  178.     else
  179.     cout << "failed to initialise the API" << endl;
  180.  
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement