Home > My CV Project > Stereo vision: Grab Frames with OpenCV + 2 x PS3 Eye

Stereo vision: Grab Frames with OpenCV + 2 x PS3 Eye

PS3 Eye is a cheap but powerful camera with 640×480 resolution and 120FPS capacity. It is one of the most ideal cheap webcams to implement computer vision projects. However, there is no official drivers for Windows/Mac/Linux. We can choose 3rd party drivers for development.

Windows:

Code Laboratories

http://codelaboratories.com/downloads/

Drivers and SDK are both needed for stereo vision. If you will use >2 PS3 eye, you have to pay for the license.

Mac, and Linux please google them.

If you use only one PS3 Eye, you don’t have to use the SDK. Default OpenCV(Version 2.4.2) Videocapture funtion works great for capturing video streams. But it doesn’t work for two cameras. You have to use CL SDK to make them work together.

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

Code for stereo capturing, with CL SDK.


</pre>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>

#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/contrib/contrib.hpp"

#include <CLEyeMulticam.h>
#include <time.h>

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <WinUser.h>

using namespace std;
using namespace cv;

void delay(double seconds)
{
 clock_t t = clock();
 while((double)(clock() - t) / (double)CLOCKS_PER_SEC<seconds);
}

// Sample camera capture class
class CLEyeStereoCameraCapture
{
 CHAR _windowNameL[256];
 CHAR _windowNameR[256];
 GUID _cameraGUID[2];
 CLEyeCameraInstance _cam[2];
 CLEyeCameraColorMode _mode;
 CLEyeCameraResolution _resolution;
 float _fps;
 HANDLE _hThread;
 bool _running;
public:
 CLEyeStereoCameraCapture( CLEyeCameraColorMode mode, CLEyeCameraResolution resolution, float fps) :
 _mode(mode), _resolution(resolution), _fps(fps), _running(false)
 {
 strcpy(_windowNameL, "Capture L");
 strcpy(_windowNameR, "Capture R");
 for(int i = 0; i < 2; i++)
 _cameraGUID[i] = CLEyeGetCameraUUID(i);
 }
 bool StartCapture()
 {
 _running = true;
 cvNamedWindow(_windowNameL, CV_WINDOW_AUTOSIZE);
 cvNamedWindow(_windowNameR, 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","CLEyeMulticamTest", MB_ICONEXCLAMATION);
 return false;
 }
 return true;
 }
 void StopCapture()
 {
 if(!_running) return;
 _running = false;
 WaitForSingleObject(_hThread, 1000);
 cvDestroyWindow(_windowNameL);
 cvDestroyWindow(_windowNameR);
 }
 void IncrementCameraParameter(int param)
 {
 for(int i = 0; i < 2; i++)
 {
 if(!_cam[i]) continue;
 CLEyeSetCameraParameter(_cam[i], (CLEyeCameraParameter)param, CLEyeGetCameraParameter(_cam[i], (CLEyeCameraParameter)param)+10);
 }
 }
 void DecrementCameraParameter(int param)
 {
 for(int i = 0; i < 2; i++)
 {
 if(!_cam[i]) continue;
 CLEyeSetCameraParameter(_cam[i], (CLEyeCameraParameter)param, CLEyeGetCameraParameter(_cam[i], (CLEyeCameraParameter)param)-10);
 }
 }
 void Run()
 {
 int w, h;
 IplImage *pCapImage[2];
 PBYTE pCapBuffer = NULL;

// Create camera instance
 for(int i = 0; i < 2; i++)
 {
 _cam[i] = CLEyeCreateCamera(_cameraGUID[i], _mode, _resolution, _fps);
 if(_cam[i] == NULL) return;
 // Get camera frame dimensions
 CLEyeCameraGetFrameDimensions(_cam[i], w, h);
 // Depending on color mode chosen, create the appropriate OpenCV image
 if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW)
 pCapImage[i] = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4);
 else
 pCapImage[i] = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);

// Set some camera parameters
 CLEyeSetCameraParameter(_cam[i], CLEYE_GAIN, 0);
 CLEyeSetCameraParameter(_cam[i], CLEYE_EXPOSURE, 127);

// Start capturing
 CLEyeCameraStart(_cam[i]);

 }
 Mat edges;
 char fileName[255];
 char *ext = ".jpg";
 int frame = 0;
 //char* prefix="D:\\test\\"
 // image capturing loop
 while(_running)
 {

 frame++;
 for(int i = 0; i < 2; i++)
 {

 cvGetRawData(pCapImage[i], &pCapBuffer);//cvGetRawData v2.3, cvGetImageRawData v2.2
 CLEyeCameraGetFrame(_cam[i], pCapBuffer);

 }

 Mat pCapImageMatL = pCapImage[0];
 Mat pCapImageMatR = pCapImage[1];
 imshow(_windowNameL, pCapImageMatL);
 imshow(_windowNameR, pCapImageMatR);

strcpy(fileName, "");
 sprintf(fileName, "L%d%s",frame, ext);
 cout<<fileName<<" ";
 imwrite(fileName, pCapImageMatL);

strcpy(fileName, "");
 sprintf(fileName, "R%d%s",frame, ext);
 cout<<fileName<<"\n";
 imwrite(fileName, pCapImageMatR);
 delay(1);

}
 for(int i = 0; i < 2; i++)
 {
 // Stop camera capture
 CLEyeCameraStop(_cam[i]);
 // Destroy camera object
 CLEyeDestroyCamera(_cam[i]);
 // Destroy the allocated OpenCV image
 cvReleaseImage(&pCapImage[i]);

 _cam[i] = 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;
 }
};

&nbsp;

int main(int argc, const char* argv[])
{

 ////////////////////////////////////////////////////////////////////////////////////
 CLEyeStereoCameraCapture *cam = NULL;

 int numCams = CLEyeGetCameraCount();
 if(numCams == 0)
 {
 printf("No PS3Eye cameras detected\n");
 return -1;
 }

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

// Query unique camera uuid

 // Create camera capture object

 cam = new CLEyeStereoCameraCapture(CLEYE_COLOR_RAW, CLEYE_VGA, 30);
 printf("Starting capture on camera...\n");
 cam->StartCapture();

 CLEyeStereoCameraCapture *pCam = NULL;

&nbsp;

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 '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;
 }
 }

printf("Stopping capture on camera...\n");
 cam->StopCapture();
 delete cam;
}
<pre>

About these ads
Categories: My CV Project
  1. April 23, 2013 at 4:30 am | #1

    It’s difficult to find educated people on this topic, however, you sound like you know what you’re talking about!
    Thanks

  2. John
    July 25, 2013 at 6:40 pm | #2

    Very informative thank you!
    Is the stereo vision code for a c++ IDE like Visual Studio yes?

  3. John
    July 26, 2013 at 6:22 pm | #4

    Is it between lines 130 and 131 that one would add in openCV image processing code?

    I’ll post here if I ever create a cool application for it. :D

  4. Nuyf
    February 10, 2014 at 4:20 pm | #5

    Thank you very much for your extremely useful post. Do you know if there is away to acces two ps3 eye cameras on Linux?

    Thanks again!

  1. February 10, 2014 at 4:46 pm | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: