Archive

Archive for the ‘My CV Project’ Category

Face Recognition Demo

November 4, 2012 Leave a comment

Advertisements
Categories: My CV Project

Census Transform – C++ Implementation

September 8, 2012 4 comments

Census Transform is a non-parametric local transform proposed by Zabih and Woodfill[1] in 1994.  Robust and insensitive to various brightness and contrast images is the primary advantage of the transform. The census transform is rather simple

12 58 66          1 1 1

50 70 81   =>  1    0  => 111 1 0 101b  => 245d

35 92 11          1 0 1

For example, we define a 3×3 sliding window to scan the entire image, and compare every pixels with center pixel.

If(PixelIntensity < PixelIntensityCenter)

bit =  1;

else

bit = 0;

then shift the bit to the left.

Do the same job to complete the all elements in the window and move the window to next pixel.

As we can see, the transform only reflect the relative brightness of a pixel to its neighbors. So it is an ideal way to reveal the structure of an image.

imL_original

imL_highcontrast

imL_lowcontrast

////////////////////////////////////////////////////////////////

img33_original

img33_highcontrast

img33_lowcontrast

Pay attention to the low-texture area, the census transform is still powerful to extract the structure inside such areas, which would facilitate stereo matching algorithms to find similarities between image pairs even in low-texture areas.

=======================

I continuously get wrong calculation results from my code at one time, so be careful.

Mat imgIn = imread(imgName, 0);
Mat imgTemp = imgIn;//try to copy the mat, but it is wrong

 #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"

 using namespace std;
 using namespace cv;

int main(int argc, char** argv)
 {
 const char* imgName = "imL_re.png";
 Mat imgIn = imread(imgName, 0);
 imshow("input", imgIn);
 Size imgSize = imgIn.size();
 Mat imgTemp = Mat::zeros(imgSize, CV_8U);

 //If (CurrentPixelIntensity<CentrePixelIntensity) boolean bit=0
 //else boolean bit=1
 //450jx375i

 unsigned int census = 0;
 unsigned int bit = 0;
 int m = 3;
 int n = 3;//window size
 int i,j,x,y;
 int shiftCount = 0;
 for (x = m/2; x < imgSize.height - m/2; x++)
 {
   for(y = n/2; y < imgSize.width - n/2; y++)
   {
     census = 0;
     shiftCount = 0;
     for (i = x - m/2; i <= x + m/2; i++)
     {
       for (j = y - n/2; j <= y + n/2; j++)
       {

         if( shiftCount != m*n/2 )//skip the center pixel
         {
         census <<= 1;
         if( imgIn.at<uchar>(i,j) < imgIn.at<uchar>(x,y) )//compare pixel values in the neighborhood
         bit = 1;
         else
         bit = 0;
         census = census + bit;
         //cout<<census<<" ";*/

         }
         shiftCount ++;
       }
     }
    //cout<<endl;

    imgTemp.ptr<uchar>(x)[y] = census;
   }
 }

 imshow("output", imgTemp);
 waitKey();
 return 0;

}

===================
[1]http://www.cs.duke.edu/courses/spring06/cps296.1/handouts/Zabih%20Woodfill%201994.pdf

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

July 19, 2012 7 comments

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>

Categories: My CV Project

Camshift Infrared Tracking – F-22

April 23, 2012 Leave a comment

Infrared object tracking project, which utilizes OpenCV library and developed in VC++ environment. Camshift algorithm is applied to find the local maximum in the histogram segmented binary frame. To predict the movement of object and smooth the tracking box, I added Kalman filter to the output coordinates..

Source code link

 

===========================