Yishi's Blog

All about my Google Summer of Code project

Setup PS3 eye with OpenCV

with 6 comments

In this post, i will show you how to setup PS3 eye with OpenCV by using CL SDK.

First Step

Download CL SDK & OpenCV

Second Step

Create the project.

  • Open the VS2008 (I’m using the Team System edition)
  • Create a new Win32 Console Application project ( File, New, Project… ), as named Hello_PS3.
  • When the pop up dialog ask you about the Additional options, please check Empty project, then finish the wizard.

Third Step

Coding…

  • Add a header file named main.h (Right click the Header Files folder in the Solution Explorer, Add, New Item…)
  • Write the header file, an example as shown below:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    //////////////////////
    // Date: 2011-04-29
    // File: main.h
    //////////////////////
    #pragma once
     
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0600
    #endif
     
    #pragma warning( disable: 4996 )
     
    #include <stdio.h>
    #include <windows.h>
     
    #include <cv.h>
    #include <highgui.h>
     
    #include "CLEyeMulticam.h"
  • Add a C++ source file named main.cpp (under Source Files folder)
  • Write the source file, an example as shown below:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    
    #include "main.h"
     
    double GetRandomNormalized() {
    	return (double)(rand() -(RAND_MAX>>1)) / (double)(RAND_MAX>>1);
    }
     
    // "Type" this class from CL SDK
    class CLEyeCameraCapture {
    	CHAR _windowName[256];
    	GUID _cameraGUID;
    	CLEyeCameraInstance _cam;
    	CLEyeCameraColorMode _mode;
    	CLEyeCameraResolution _resolution;
    	float _fps;
    	HANDLE _hThread;
    	bool _running;
     
    public:
    	CLEyeCameraCapture(LPSTR windowName, GUID cameraGUID, CLEyeCameraColorMode mode, CLEyeCameraResolution resolution, float fps ):
    	  _cameraGUID(cameraGUID), _cam(NULL), _mode(mode), _resolution(resolution), _fps(fps), _running(false) {
    		  strcpy( _windowName, windowName );
    	}
     
    	  bool StartCapture() {
    		  _running = true;
    		  cvNamedWindow( _windowName, CV_WINDOW_AUTOSIZE );
     
    		  // Start CLEye image capture thread
    		  _hThread = CreateThread(NULL, 0, &CLEyeCameraCapture::CaptureThread, this, 0, 0 );
    		  if ( _hThread == NULL ) {
    			  MessageBoxA( NULL, "Could not create capture thread", "CLEyeMulticamText", MB_ICONEXCLAMATION );
    			  return false;
    		  }
     
    		  return true;
    	  }
     
    	  void StopCapture() {
    		  if ( !_running ) {
    			  return;
    		  }
    		  _running = false;
    		  WaitForSingleObject( _hThread, 1000 );	// /_\/
    		  cvDestroyWindow( _windowName );
    	  }
     
    	  void Run() {
    		  int w, h;
    		  IplImage *pCapImage;
    		  PBYTE pCapBuffer = NULL;
     
    		  // Create camera instance
    		  _cam = CLEyeCreateCamera( _cameraGUID, _mode, _resolution, _fps );
    		  if ( _cam == NULL ) {	// Can not create
    			  return;
    		  }
     
    		  // Get camera frame dimensions
    		  CLEyeCameraGetFrameDimensions( _cam, w, h );
     
    		  // Depending on color mode chosen, create the appropriate OpenCV image.
    		  if ( _mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW ) {
    			  pCapImage = cvCreateImage( cvSize(w, h), IPL_DEPTH_8U, 4 );
    		  } else {
    			  pCapImage = cvCreateImage( cvSize(w, h), IPL_DEPTH_8U, 1 );
    		  }
     
    		  // Set some camera parameters
    		  CLEyeSetCameraParameter( _cam, CLEYE_GAIN, 0 );
    		  CLEyeSetCameraParameter( _cam, CLEYE_EXPOSURE, 511 );
    		  CLEyeSetCameraParameter( _cam, CLEYE_ZOOM, (int)(GetRandomNormalized() * 100.00 ));
    		  CLEyeSetCameraParameter( _cam, CLEYE_ROTATION, (int)(GetRandomNormalized() * 300.00 ));
     
    		  // Start capturing
    		  CLEyeCameraStart( _cam );
    		  cvGetImageRawData( pCapImage, &pCapBuffer );
     
    		  // Image capturing loop
    		  while( _running ) {
    			  CLEyeCameraGetFrame( _cam, pCapBuffer );
     
    			  cvShowImage( _windowName, pCapImage );
    		  }
     
    		  // Stop camera capture.
    		  CLEyeCameraStop( _cam );
     
    		  // Destroy camera object
    		  CLEyeDestroyCamera( _cam );
     
    		  // Destroy the callocated OpenCV image
    		  cvReleaseImage( &pCapImage );
    		  _cam = NULL;
    	  }
     
    	  static DWORD WINAPI CaptureThread( LPVOID instance ) {
    		  // seed the rng with current tick count and thread id
    		  srand( GetTickCount() + GetCurrentThreadId() );
     
    		  // forward thread to Capture function
    		  CLEyeCameraCapture *pThis = ( CLEyeCameraCapture *) instance;
     
    		  pThis->Run();	// toggle the Run function
     
    		  return 0;
    	  }
    };
     
    int main( int argc, char** argv ) {
     
    	CLEyeCameraCapture *cam[2] = {NULL};
    	srand( GetTickCount() );
     
    	// Query for number fo connected cameras.
    	int numCams = CLEyeGetCameraCount();
    	if ( numCams == 0 ) {
    		printf( "No PS3Eye cameras detected\n" );
    		return -1;
    	}
    	printf( "Found %d cameras\n", numCams );
     
    	for ( int i = 0; i < numCams; i++ ) {
    		char windowName[64];
    		GUID guid = CLEyeGetCameraUUID(i);	// Get Camera GUID by index
    		printf( "Camera %d GUID: [%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x]\n",
    			i+1, guid.Data1, guid.Data2, guid.Data3,
    			guid.Data4[0], guid.Data4[1], guid.Data4[2],
    			guid.Data4[3], guid.Data4[4], guid.Data4[5],
    			guid.Data4[6], guid.Data4[7]);
    		sprintf( windowName, "Camera Window %d", i+1 );
     
    		cam[i] = new CLEyeCameraCapture( windowName, guid, rand()<(RAND_MAX >> 1) ? CLEYE_COLOR_PROCESSED : CLEYE_MONO_PROCESSED,
    			rand()<(RAND_MAX >> 1 ) ? CLEYE_VGA : CLEYE_QVGA, 30 );	// Create a new camera capture.
    		printf( "Starting capture on camera %d\n", i+1);
    		cam[i]->StartCapture();	// Start the capture.
    	}
     
    	printf( "Just for test!\nUse the following keys to change camera parameters:\n"
    		"\t'1' - select camera 1\n"
    		"\t'2' - select camera 2\n"
    		"\t<ESC> key will exit the program.\n");
    	CLEyeCameraCapture *pCam = NULL;	// Set the camera pointer to NULL
    	int param = -1, key;
     
    	while( (key = cvWaitKey(0)) != 0x1b ) {	// ESC == 27
    		switch ( key ) {	// switch the camera
    			case '1':		printf( "Selected camera 1\n");	pCam = cam[0];	break;
    			case '2':		printf( "Selected camera 2\n");	pCam = cam[1];	break;
    		}
    	}
     
    	for ( int i = 0; i < numCams; i++ ) {
    		printf( "Stopping capture on camera %d\n", i+1 );
    		cam[i]->StopCapture();	// Stop capture.
    		delete cam[i];			// delete the cam[i];
    	}
     
    	return 0;
    }

Fourth Step

Additional settings before build the solution.

  • Add the Additional Include Directories values (Alt+F7, Configuration Properties, C/C++, General) as below:
    C:\Program Files\OpenCV2.2\include
    C:\Program Files\OpenCV2.2\include\opencv
    C:\Program Files\Code Laboratories\CL-Eye Platform SDK\Include
    

    (You should change the directory path to yours)

  • Make sure that the Runtime library value is set to Multi-threaded DLL(/MD) mode. (C/C++, Code Generation)
  • Add the Additional Dependencies values (Linker, Input) as shown below:
    "C:\Program Files\Code Laboratories\CL-Eye Platform SDK\Lib\CLEyeMulticam.lib"
    "C:\Program Files\OpenCV2.2\lib\opencv_core220.lib"
    "C:\Program Files\OpenCV2.2\lib\opencv_highgui220.lib"
    

    (Yeah, you should change above path, if you have different one)

Fifth Step

Okay, now finishing your project!

  • Build Solution (F7)
  • Run it, and it works! (F5)

Capture Test

My test result as shown below:

Reference

Written by e10101

April 28th, 2011 at 8:15 pm

Posted in Uncategorized

6 Responses to 'Setup PS3 eye with OpenCV'

Subscribe to comments with RSS or TrackBack to 'Setup PS3 eye with OpenCV'.

  1. Thanks for the reference. That LINK1181 article gets a lot of traffic, it’s one of my most popular articles. Good luck with your GSOC project!

      (Quote)  (Reply)

    Dave Doolin

    28 Apr 11 at 8:23 pm

  2. Dave Doolin,

    Thanks for your post. it’s really helpful!

      (Quote)  (Reply)

    e10101

    28 Apr 11 at 9:28 pm

  3. Thanks. By any chance have you checked the highest fps you can achieve with your code and PS3 eye?

      (Quote)  (Reply)

    lifespicer

    29 Sep 11 at 9:59 am

  4. lifespicer,

    If you set the resolution to 320×240 and using 4 or 6 cameras, you can get 60fps at the same time. But if you set the resolution to 640×480, it will work with 2 or 3 cameras at 30 fps at the same time.

    btw, you can check the new released code at: https://ccv-multi-cam.googlecode.com/svn/branches/test/CCV_Select_Camera/addons/ofxPS3/src/

      (Quote)  (Reply)

    e10101

    30 Sep 11 at 1:21 am

  5. When you are making threads for each camera, you are using the same HANDLE _hThread.
    So does that mean that only one thread is being made for all the camera’s?

      (Quote)  (Reply)

    Pratik

    4 Jul 12 at 4:57 pm

  6. This is just a test project, then, yes it use only one thread for all the cameras.

      (Quote)  (Reply)

    admin

    5 Jul 12 at 12:52 am

Leave a Reply