The CL Eye Platform SDK C++ SampleAuthor: AlexPDate: 01-02-2010
In this article we describe a simple way of using the CL-Eye Platform C++ API in C++ applications. The sample described here is the part of our CL-Eye Platform SDK and demonstrates CL-Eye Multicam API use in a simple console application with OpenCV...
The C++ Sample
This C++ sample uses the CL-Eye Multicam API that is the part of our Platform SDK. For the in-depth details regarding CL-Eye Multicam API read this article. Below is the C++ sample console mode application as included with the SDK. For the sake of simplicity we have split the file into two parts.
The CLEyeCameraCapture Class
First, lets take a look at the CLEyeCameraCapture class. The class encapsulates the creation of the OpenCV display window, camera initialization and capture thread. Most of the methods are self descriptive. We will only take a look at the constructor. The key parameters here are the windowName and cameraGUID. The windowName has to be unique per instance of this class. The cameraGUID uniquely identifies the camera and is the same value obtained by calling CLEyeGetCameraUUID function later on in the main function. The StartCapture then is used to create OpenCV window and create capture thread. The thread will then create the camera, create IplImage that will hold captured frame, and start camera capture. Thread's main loop waits for camera's frame and then display the image in the OpenCV window. It will continue to do so until StopCapture is called from the main program.
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This file is part of CL-EyeMulticam SDK
//
// C++ Sample Application
//
// It allows the use of multiple CL-Eye cameras in your own applications
//
// For updates and file downloads go to: codelaboratories.com/research/view/cl-eye-muticamera-sdk
//
// Copyright 2008-2010 (c) Code Laboratories, Inc. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
double GetRandomNormalized()
{
return (double)(rand()-(RAND_MAX>>1))/(double)(RAND_MAX>>1);
}
// Sample camera capture class
class CLEyeCameraCapture
{
CHAR _windowName[256];
GUID _cameraGUID;
CLEyeCameraInstance _cam;
CLEyeCameraColorMode _mode;
CLEyeCameraResolution _resolution;
int _fps;
HANDLE _hThread;
bool _running;
public:
CLEyeCameraCapture(LPSTR windowName,
GUID cameraGUID,
CLEyeCameraColorMode mode,
CLEyeCameraResolution resolution, int 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)
{
MessageBox(NULL,"Could not create capture thread","CLEyeMulticamTest", MB_ICONEXCLAMATION);
return false;
}
return true;
}
void StopCapture()
{
if(!_running) return;
_running = false;
WaitForSingleObject(_hThread, 1000);
cvDestroyWindow(_windowName);
}
void IncrementCameraParameter(int param)
{
if(!_cam) return;
CLEyeSetCameraParameter(_cam,
(CLEyeCameraParameter)param,
CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param)+10);
}
void DecrementCameraParameter(int param)
{
if(!_cam) return;
CLEyeSetCameraParameter(_cam,
(CLEyeCameraParameter)param,
CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param)-10);
}
void Capture()
{
int w, h;
IplImage *pCapImage;
PBYTE pCapBuffer = NULL;
// Create camera instance
_cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps);
if(_cam == NULL) return;
// Get camera frame dimensions
CLEyeCameraGetFrameDimensions(_cam, w, h);
// Depending on color mode chosen, create the appropriate OpenCV image
if(_mode == CLEYE_COLOR)
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, 20);
CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 511);
CLEyeSetCameraParameter(_cam, CLEYE_ZOOM, (int)(GetRandomNormalized()*100.0));
CLEyeSetCameraParameter(_cam, CLEYE_ROTATION, (int)(GetRandomNormalized()*300.0));
// Start capturing
CLEyeCameraStart(_cam);
// image capturing loop
while(_running)
{
cvGetImageRawData(pCapImage, &pCapBuffer);
CLEyeCameraGetFrame(_cam, pCapBuffer);
cvShowImage(_windowName, pCapImage);
}
// Stop camera capture
CLEyeCameraStop(_cam);
// Destroy camera object
CLEyeDestroyCamera(_cam);
// Destroy the allocated 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->Capture();
return 0;
}
};
The main() function
This is where our sample program begins. We start by querying the CL-Eye Multicam API for number of available cameras on the system. If this is zero we display the appropriate message and exit the program. Otherwise, we proceed by querying each camera UUID and displaying that information in the console output. We also create one CLEyeCameraCapture object per camera and call its StartCapture method. While each camera thread is now running, capturing and displaying the image, in our main loop we process any keyboard input and depending on the key pressed we dynamically modify selected camera and selected parameter. We do this until the escape key is pressed on the keyboard, upon which the program will stop capturing, close all cameras and exit.
// Main program entry point
int main(int argc, char *argv[])
{
CLEyeCameraCapture *cam[2] = ;
srand(GetTickCount());
// Query for number of 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];
// Query unique camera uuid
GUID guid = CLEyeGetCameraUUID(i);
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);
// Create camera capture object
// Randomize resolution and color mode
cam[i] = new CLEyeCameraCapture(windowName,
guid,
rand()<(RAND_MAX>>1) ? CLEYE_COLOR : CLEYE_GRAYSCALE,
rand()<(RAND_MAX>>1) ? CLEYE_VGA : CLEYE_QVGA, 30);
printf("Starting capture on camera %d\n", i+1);
cam[i]->StartCapture();
}
printf("Use the following keys to change camera parameters:\n"
"\t'1' - select camera 1\n"
"\t'2' - select camera 2\n"
"\t'g' - select gain parameter\n"
"\t'e' - select exposure parameter\n"
"\t'z' - select zoom parameter\n"
"\t'r' - select rotation parameter\n"
"\t'+' - increment selected parameter\n"
"\t'+' - decrement selected parameter\n");
// The <ESC> key will exit the program
CLEyeCameraCapture *pCam = NULL;
int param = -1, key;
while((key = cvWaitKey(0)) != 0x1b)
{
switch(key)
{
case 'g': case 'G': printf("Parameter Gain\n"); param = CLEYE_GAIN; break;
case 'e': case 'E': printf("Parameter Exposure\n"); param = CLEYE_EXPOSURE; break;
case 'z': case 'Z': printf("Parameter Zoom\n"); param = CLEYE_ZOOM; break;
case 'r': case 'R': printf("Parameter Rotation\n"); param = CLEYE_ROTATION; break;
case '1': printf("Selected camera 1\n"); pCam = cam[0]; break;
case '2': printf("Selected camera 2\n"); pCam = cam[1]; break;
case '+': if(pCam) pCam->IncrementCameraParameter(param); break;
case '-': if(pCam) pCam->DecrementCameraParameter(param); break;
}
}
for(int i = 0; i < numCams; i++)
{
printf("Stopping capture on camera %d\n", i+1);
cam[i]->StopCapture();
delete cam[i];
}
return 0;
}
Conclusion
In this very simple C++ sample we have demonstrated the use of complete CL-Eye Multicam API as well as simple integration with OpenCV image processing framework.
For the latest CL-Eye Driver and Platform SDK and more code samples visit out downloads page: CL-Eye Platform Downloads