kshaaban 0 Newbie Poster

Hi everyone,
So this is my first post :S and as you will probably tell from my code, I'm pretty new to coding in c++. My main areas are web and online video streaming and encoding etc. however I have taken it upon myself to learn c++. This is part of an assignment for my final year at university. Essentially this code utilises the CLEye-multicam SDK from Code laboratorieshttp://codelaboratories.com which allows for multiple PS3 Eye camera instances to be created. The code is based off some SDK sample code which I have managed to hash together with my limited understanding. It is working however, I wish to hard code each camera instance based on its GUID. The project is going to be a permanent installation so I would like it if the cameras were initialised in a specific sequence.

I kinda want to do this: http://codelaboratories.com/forums/viewthread/114/

However I currently lack the understanding of the GUID type and c++ to create this. I am currently understanding the theorey more than the practical. The CL-EYE forum has been pretty quiet recently so I thought I would try here. I've used this site so many times, its about time I started giving back (if I can :$)

Also any feedback that anyone can give regarding the general coding would be great. I'm really enjoying learning C++ and would appreciate any feedback to help me develop my abilities. code is below...

#include "CLEyeMulticam.h"
#include "highgui.h"
#include "cv.h"
#include <iostream>
#include "stdafx.h"

using namespace std;


IplImage *_pCapImage[8]; 
HANDLE hEvents[5]; 



// Sample camera capture and processing class
class CLEyeStereoCameraCapture
{
    CHAR _windowName[256];
    int _idx; // Index of GUID to obtain
    GUID _cameraGUID;
    CLEyeCameraInstance _cam;
    CLEyeCameraColorMode _mode;
    CLEyeCameraResolution _resolution;
    float _fps; 
    int _imageidx; // index of _pCapImage
    HANDLE _hThread;
    bool _running;
public:
    CLEyeStereoCameraCapture(LPSTR windowName, int idx, CLEyeCameraResolution resolution, float fps, int imageidx) :
      _idx(idx), _mode(CLEYE_MONO_RAW), _resolution(resolution), _fps(fps), _imageidx(imageidx), _running(false)
      {
          strcpy_s(_windowName, windowName);
          _cameraGUID = CLEyeGetCameraUUID(_idx); // Get current camera guid - change to obtain it form array!!!
		
	  }
	  
      bool StartCapture()
      {
          _running = true;
          cvNamedWindow(_windowName, CV_WINDOW_AUTOSIZE);

          // Start CLEye image capture thread
          _hThread = CreateThread(NULL, 0, &CLEyeStereoCameraCapture::CaptureThread, this, 0, 0);
          if(_hThread == NULL)
          {
              //MessageBox(NULL,"Could not create capture thread","CLEyeStereoCameraCapture", MB_ICONEXCLAMATION);
              return false;
          }
         

		  printf("\nThread %d created",_imageidx); //DEBUG ONLY
          return true;
      }

/*-----------------------INCREMENT PARAMETER----------------------*/
	void IncrementCameraParameter(int param)
	{
		if(!_cam)	return;
		printf("CLEyeGetCameraParameter %d\n", CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param));
		CLEyeSetCameraParameter(_cam, (CLEyeCameraParameter)param, CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param)+1);
	}
/*----------------------DECREMENT PARAMETER-----------------------*/	
	void DecrementCameraParameter(int param)
	{
		if(!_cam)	return;
		printf("CLEyeGetCameraParameter %d\n", CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param));
		CLEyeSetCameraParameter(_cam, (CLEyeCameraParameter)param, CLEyeGetCameraParameter(_cam, (CLEyeCameraParameter)param)-1);
	}
      void StopCapture()
      {
          if(!_running)    return;
          _running = false;
          WaitForSingleObject(_hThread, 1000);

          cvDestroyWindow(_windowName);
      }
      void Run()
      {
          printf("\ninside run %d",_imageidx); //DEBUG ONLY
          int w, h;
          // Create camera instance
          _cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps);
          if(_cam == NULL)    return;
          // Get camera frame dimensions
          CLEyeCameraGetFrameDimensions(_cam, w, h);
          // Create the OpenCV image
          _pCapImage[_imageidx] = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
          
          printf("\n_pcamImage[%d] created\n",_imageidx); //DEBUG ONLY
          // Set some camera parameters
          CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 0);
          CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 511);

          // Start capturing
          CLEyeCameraStart(_cam);

          // image capturing loop
          while(_running)
          {
              PBYTE pCapBuffer;
              // Capture camera image
              cvGetImageRawData(_pCapImage[_imageidx], &pCapBuffer);
              CLEyeCameraGetFrame(_cam, pCapBuffer);
			  //
              // Process the image here
			  cvShowImage(_windowName, _pCapImage[_imageidx]);
              }

          // Stop camera capture
          CLEyeCameraStop(_cam);
          // Destroy camera object
          CLEyeDestroyCamera(_cam);
          // Destroy the allocated OpenCV image
          //cvReleaseImage(&_pCapImage);
          cvReleaseImage(&_pCapImage[_imageidx]);
          _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
          CLEyeStereoCameraCapture *pThis = (CLEyeStereoCameraCapture *)instance;
          pThis->Run();
          return 0;
      }
};

int runstereo()
{
    CLEyeStereoCameraCapture *cam[5]= { NULL };
    // Query for number of connected cameras
    int numCams = CLEyeGetCameraCount();

    printf("%d cameras found\n", numCams);

    //    char windowName[64];

    if(numCams < 4)
    {
        printf("ERROR: Need four PS3Eye cameras to run\n");
        return -1;
    }
    // Create camera capture objects
    cam[0] = new CLEyeStereoCameraCapture("Raw Camera 1", 0, CLEYE_QVGA, 30, 0);
    cam[1] = new CLEyeStereoCameraCapture("Raw Camera 2", 1, CLEYE_QVGA, 30, 1);
    cam[2] = new CLEyeStereoCameraCapture("Raw Camera 3", 2, CLEYE_QVGA, 30, 2);
    cam[3] = new CLEyeStereoCameraCapture("Raw Camera 4", 3, CLEYE_QVGA, 30, 3);
	cam[4] = new CLEyeStereoCameraCapture("Raw Camera 5", 4, CLEYE_QVGA, 30, 4);
    printf("Starting capture\n");
    cam[0]->StartCapture();
    cam[1]->StartCapture();
    cam[2]->StartCapture();
    cam[3]->StartCapture();
	cam[4]->StartCapture();
    
    
   printf("Use the following keys to change camera parameters:\n"
		"\t'1' - select camera 1\n"
		"\t'2' - select camera 2\n"
		"\t'3' - select camera 3\n"
		"\t'4' - select camera 4\n"
		"\t'4' - select camera 5\n"
//Add rows here for extra cameras.
		"\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
	
	CLEyeStereoCameraCapture *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 '3':				printf("Selected camera 3\n");	pCam = cam[2];			break;
			case '4':				printf("Selected camera 4\n");	pCam = cam[3];			break;
			case '5':				printf("Selected camera 5\n");	pCam = cam[4];			break;
			case '+':	if(pCam)	pCam->IncrementCameraParameter(param);		break;
			case '-':	if(pCam)	pCam->DecrementCameraParameter(param);		break;
		}
	}
    

    // Stop capture
    cam[0]->StopCapture();
    cam[1]->StopCapture();
    cam[2]->StopCapture();
    cam[3]->StopCapture();
	cam[4]->StopCapture();
    delete cam[0];
    delete cam[1];
    delete cam[2];
    delete cam[3];
	delete cam[4];
    return 0;
}