Advertisement
Guest User

Veehmot

a guest
Mar 9th, 2009
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. //#ifdef _CRAZY_FLAG_OPTITRACK
  2. //========================================================================-----
  3. //== OptiTrack Sample Application
  4. //== Copyright (c) NaturalPoint
  5. //==
  6. //== This sample is intended to show how to properly access and control
  7. //== OptiTrack cameras.  The application does the following:
  8. //==
  9. //== 1. Initializes the OptiTrack COM Component.
  10. //== 2. Displays information about all connected cameras.
  11. //== 3. Starts the first camera in the system and displays a grayscale image
  12. //==    (grayscale is only supported on OptiTrack cameras.  TrackIR and
  13. //==     SmartNAV cameras will display filtered tracking image)
  14. //== 4. Unintializes the OptiTrack cameras, COM, and terminates.
  15.  
  16. //== This sample uses TinyPTC to display grayscale data ==================-----
  17. //== >> TinyPTC by Gaffer
  18. //== >> www.gaffer.org/tinyptc
  19.  
  20. //== INCLUDES ============================================================-----
  21.  
  22. #include "tinyptc.h"
  23.  
  24. //== NECESSARY OPTITRACK INCLUDES AND DEFINITIONS ========================-----
  25.  
  26. #include <objbase.h>
  27. #include <atlbase.h>
  28. #include "./optitrack.h"
  29. #import  "./optitrack.tlb"
  30.  
  31. // OpenCV includes.
  32. #include "cv.h"
  33. #include "highgui.h"
  34. IplImage* backbuffer = cvCreateImage( cvSize(640,480), IPL_DEPTH_8U, 4 );
  35. unsigned char *backbufferData= (unsigned char *)(backbuffer->imageData);
  36.  
  37. static int noise;
  38. static int carry;
  39. static int index;
  40. static int seed = 0x12345;
  41. static int pixel[640*480];
  42. static unsigned char frameBuffer[640*480];
  43. bool       TimeToClose = false;
  44.  
  45. //== SAMPLE APPLICATION ENTRY POINT ======================================-----
  46.  
  47. int main(int argc, char** argv)
  48. {
  49.  
  50.     //== Initialize Microsoft COM Interop ================----
  51.     CoInitialize(NULL);
  52.  
  53.     //== Initialize OptiTrack COM Component ==============----
  54.     CComPtr<INPCameraCollection> cameraCollection;
  55.     CComPtr<INPCamera>           camera;
  56.     CComPtr<INPCameraFrame>      frame;
  57.  
  58.     cameraCollection.CoCreateInstance(CLSID_NPCameraCollection);
  59.  
  60.     //== Enumerate (Identify) Available Cameras ==========----
  61.     cameraCollection->Enum();
  62.  
  63.     long cameraCount  = 0;
  64.     int  frameCounter = 0;
  65.  
  66.     //== Determine Available Cameras =====================----
  67.     cameraCollection->get_Count(&cameraCount);
  68.  
  69.     int windowWidth = 0;
  70.     int windowHeight = 0;
  71.  
  72.     //== Display Camera Information for All Cameras ======----
  73.     for(int index=0; index<cameraCount; index++)
  74.     {
  75.         cameraCollection->Item(index, &camera);
  76.  
  77.         long serial,width,height,model,revision,rate;
  78.  
  79.         camera->get_SerialNumber(&serial);
  80.         camera->get_Width       (&width);
  81.         camera->get_Height      (&height);
  82.         camera->get_Model       (&model);
  83.         camera->get_Revision    (&revision);
  84.         camera->get_FrameRate   (&rate);
  85.  
  86.         if(index==0)
  87.         {
  88.             windowWidth = width;
  89.             windowHeight = height;
  90.         }
  91.  
  92.         //== Set Some Camera Options ====================----
  93.  
  94.         //== Set Grayscale Mode =========================----
  95.         camera->SetOption(NP_OPTION_VIDEO_TYPE        , (CComVariant) 1 );
  96.  
  97.         //== Don't drop frames ==--
  98.         camera->SetOption(NP_OPTION_FRAME_DECIMATION  , (CComVariant) 0 );
  99.  
  100.         //== Display 99 on the Camera ===================----
  101.         //== Note: This only works for cameras with a display
  102.         camera->SetOption(NP_OPTION_NUMERIC_DISPLAY_ON, (CComVariant) 01);
  103.  
  104.         //== Add frame information to the top left of the frame ==--
  105.         camera->SetOption(NP_OPTION_TEXT_OVERLAY_OPTION,(CComVariant) 255);
  106.  
  107.         //== Send corrupt frames ==
  108.         //camera->SetOption(NP_OPTION_SEND_FRAME_MASK,(CComVariant) 255);
  109.  
  110.         //== Slow camera frame frame to 25% ==--
  111.         //camera->SetOption(NP_OPTION_FRAME_RATE,(CComVariant) 25);
  112.  
  113.         //== Always Clean-up COM Objects ================----
  114.         camera.Release();
  115.     }
  116.  
  117.  
  118.     //== Open the first camera ==========================----
  119.  
  120.     if(cameraCount>0)
  121.     {
  122.         if (!ptc_open("OptiTrack Grayscale Sample", windowWidth, windowHeight)) return 1;
  123.  
  124.         cameraCollection->Item(0, &camera);
  125.         {
  126.             camera->Open();
  127.             camera->Start();
  128.             camera->SetOption(NP_OPTION_EXPOSURE, (CComVariant) 1);
  129.             {
  130.                 while(!TimeToClose)
  131.                 {
  132.                     Sleep(10);
  133.  
  134.                     MSG msg;
  135.                     if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
  136.                     {
  137.                         if(GetMessage( &msg, NULL, 0, 0 ) )
  138.                         {
  139.                             TranslateMessage(&msg);
  140.                             DispatchMessage(&msg);
  141.                         }
  142.                     }
  143.  
  144.                     camera->GetFrame(0, &frame);
  145.  
  146.                     if(frame!=0)
  147.                     {
  148.                         //== New Frame Has Arrived ==========================------
  149.                         frameCounter++;
  150.  
  151.                         camera->GetFrameImage(frame, windowWidth, windowHeight, windowWidth, 8, (byte *) backbuffer->imageData);//(byte *) backbufferData);//(byte *) backbuffer->imageData);
  152.  
  153.                         cvRectangle(backbuffer, cvPoint(0, 0), cvPoint(64, 64), CV_RGB(255, 0, 0), 5);
  154.  
  155.                         /*for (int index=0; index<windowWidth*windowHeight; index++){
  156.                             pixel[index] = (unsigned char) backbuffer->imageData[index];
  157.                             //pixel[index] = backbufferData[index];
  158.                         }*/
  159.                         /*for (int index=0; index<windowWidth*windowHeight; index++) {
  160.                             noise = (unsigned char) backbuffer->imageData[index];
  161.                             pixel[index] = (noise<<16) | (noise<<8) | noise;
  162.                         }*/
  163.                         ptc_update(backbuffer->imageData);
  164.  
  165.  
  166.  
  167.                         frame->Free();
  168.                         frame.Release();
  169.                     }
  170.                 }          
  171.             }
  172.             camera->Stop();
  173.             camera->Close();
  174.         }
  175.  
  176.         camera.Release();
  177.     }
  178.  
  179.     ptc_close();
  180.  
  181.     //== Always Clean-up COM Objects ================----
  182.     cameraCollection.Release();
  183.  
  184.     //== Uninitialize Microsoft COM Interop =============----
  185.     CoUninitialize();
  186.  
  187.     ExitProcess(1);
  188. }
  189. //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement