CCV使用兩個PGR Firefly網路攝影機源碼

來源:互聯網
上載者:User

聲明:歡迎任何人和組織轉載本blog中文章,但必須標記文章原始連結和作者資訊。  

本文連結:http://blog.csdn.net/li_007/archive/2009/12/17/5024675.aspx

開拓進取的小烏龜------->CSDN點滴點點滴滴Blog

 

   最近因為項目需要,需要在ccv中使用多網路攝影機。在官方的ccv 1.3中是直接支援兩個Sony PlayStation 3 Eye Camera的,但是經過測試發現PS 3網路攝影機達不到我們要求,所以只有還是採用以前一直用的PGR Firefly@MV網路攝影機,自己來改寫代碼支援多個。

現在將代碼貼出來,大家互相學習交流討論,希望大蝦不吝賜教。

//<br />// PGRFireFlyMultiCamera.h<br />//<br />//<br />// Written by Leezhm, 15th Dec, 2009<br />// Contact : Leezhm@126.com<br />// Last Modified by Leezhm, 16th Dec, 2009<br />// Copyright(c) Leezhm All rights reserved<br />//<br />#ifndef __h_PGRFireflyMultiCamera__<br />#define __h_PGRFireflyMultiCamera__<br />#include "../PGRSDK/include/PGRFlyCapture.h"<br />#include "../PGRSDK/include/PGRFlyCapturePlus.h"<br />#include <iostream><br />using std::cout;<br />using std::endl;<br />const unsigned int MAX_CAMERA_COUNT = 2;<br />class PGRFireflyMultiCamera<br />{<br />private:<br />static unsigned int camCount;<br />int camHeight;<br />int camWidth;<br />FlyCaptureContext context[MAX_CAMERA_COUNT];<br />public:<br />FlyCaptureImagePlus * fcImagePlus;<br />public:<br />PGRFireflyMultiCamera();<br />~PGRFireflyMultiCamera();<br />public:<br />static unsigned int getCameraCount();<br />inline void showErrorMessage(FlyCaptureError & err, char * pFun)<br />{<br />if (FLYCAPTURE_OK != err)<br />{<br />cout<<"Error : "<<*pFun<<" --> "<<flycaptureErrorToString(err)<<endl;<br />}<br />}<br />public:<br />void initFireflyCamera(int width,int height, int framerate);<br />int getCamWidth();<br />int getCamHeight();<br />void listDevices();<br />void grabFrame();<br />};<br />#endif // __h_PGRFireflyMultiCamera__

下面是CPP檔案

//<br />// PGRFireFlyMultiCamera.cpp<br />//<br />//<br />// Written by Leezhm, 15th Dec, 2009<br />// Contact : Leezhm@126.com<br />// Last Modified by Leezhm, 16th Dec, 2009<br />// Copyright(c) Leezhm All rights reserved<br />//<br />#include "PGRFireFlyMultiCamera.h"<br />unsigned int PGRFireflyMultiCamera::camCount = MAX_CAMERA_COUNT;<br />PGRFireflyMultiCamera::PGRFireflyMultiCamera()<br />{<br />fcImagePlus = NULL;<br />}<br />PGRFireflyMultiCamera::~PGRFireflyMultiCamera()<br />{<br />FlyCaptureError err;<br />unsigned int uiCamera = 0;<br />if (NULL != fcImagePlus->image.pData)<br />{<br />delete [] fcImagePlus->image.pData;<br />}<br />if (NULL != fcImagePlus)<br />{<br />delete fcImagePlus;<br />}<br />//<br />// Stop all cameras from grabbing and destroy their contexts.<br />//<br />for( uiCamera = 0; uiCamera < camCount; uiCamera++ )<br />{<br />err = ::flycaptureStop(context[uiCamera] );<br />showErrorMessage(err, "flycaptureStop()");<br />err = ::flycaptureDestroyContext(context[uiCamera]);<br />showErrorMessage(err, "flycaptureBusEnumerateCameras()");<br />}<br />}<br />void PGRFireflyMultiCamera::listDevices()<br />{<br />FlyCaptureError err;<br />FlyCaptureInfoEx info[MAX_CAMERA_COUNT];<br />//<br />// Enumerate the bus and get the count of camera<br />//<br />err = ::flycaptureBusEnumerateCamerasEx(info, &camCount);<br />if (FLYCAPTURE_OK != err)<br />{<br />cout<<"PGRFireflyMultiCamera::flycaptureBusEnumerateCamerasEx() : "<br /> <<flycaptureErrorToString(err)<<endl;<br />}<br />else<br />{<br />for( unsigned int uiBusIndex = 0; uiBusIndex < camCount; uiBusIndex++ )<br />{<br />FlyCaptureInfoEx* pinfo = &info[uiBusIndex];<br />cout<<"Index "<<uiBusIndex<<": "<<pinfo->pszModelName<br /><<", SerialNumber: "<<pinfo->SerialNumber<<endl;<br />}<br />cout<<"end of listing FFMV/n/n";<br />}<br />}<br />unsigned int PGRFireflyMultiCamera::getCameraCount()<br />{<br />if (0 == camCount)<br />{<br />::flycaptureBusCameraCount(&camCount);<br />}<br />return camCount;<br />}<br />void PGRFireflyMultiCamera::initFireflyCamera(int width,int height, int framerate)<br />{<br />FlyCaptureError err;<br />unsigned int uiCamera = 0;<br />//<br />// Create a context for and initialize every camera on the bus.<br />//<br />for( uiCamera = 0; uiCamera < camCount; uiCamera++ )<br />{<br />err = ::flycaptureCreateContext(&context[uiCamera]);<br />showErrorMessage(err, "flycaptureCreateContext()");<br />cout<<"Initializing camera "<<uiCamera<<endl;<br />err = ::flycaptureInitialize(context[uiCamera], uiCamera);<br />showErrorMessage(err, "flycaptureInitializePlus()");<br />}<br />FlyCaptureFrameRate setFrameRate;<br />if (60 == framerate)<br />{<br />setFrameRate = FLYCAPTURE_FRAMERATE_60;<br />}<br />else if (30 == framerate)<br />{<br />setFrameRate = FLYCAPTURE_FRAMERATE_30;<br />}<br />else if (15 == framerate)<br />{<br />setFrameRate = FLYCAPTURE_FRAMERATE_15;<br />}<br />//<br />// Start all of the cameras grabbing<br />//<br />for( uiCamera = 0; uiCamera < camCount; uiCamera++ )<br />{<br />cout<<"Starting camera./n/n";<br />err = ::flycaptureStartLockNext(context[uiCamera], FLYCAPTURE_VIDEOMODE_640x480Y8, setFrameRate);<br />showErrorMessage(err, "flycaptureStart()");<br />}<br />//<br />// Having started all of the cameras synchronize all of their buffers.<br />// Please note that cameras running at the same frame rate on the same<br />// bus will automatically synchronize to each other. This call is for<br />// purposes of synchronizing the buffers.<br />//<br />err = ::flycaptureSyncForLockNext(context, camCount);<br />showErrorMessage(err, "flycaptureSyncForLockNext()");<br />if (2 == camCount)<br />{<br />if (NULL == fcImagePlus)<br />{<br />fcImagePlus = new FlyCaptureImagePlus();<br />}</p><p>fcImagePlus->image.iCols = camCount * width;<br />fcImagePlus->image.iRows = height;<br />if (NULL == fcImagePlus->image.pData)<br />{<br />fcImagePlus->image.pData = new unsigned char[fcImagePlus->image.iRows * fcImagePlus->image.iCols];<br />memset(fcImagePlus->image.pData, 0, fcImagePlus->image.iRows * fcImagePlus->image.iCols);<br />}<br />}<br />else<br />{<br />cout<<"there are more than 2 cameras!/n";<br />getchar();<br />return ;<br />}<br />}<br />int PGRFireflyMultiCamera::getCamWidth()<br />{<br />camWidth = fcImagePlus->image.iCols;<br />return camWidth;<br />}<br />int PGRFireflyMultiCamera::getCamHeight()<br />{<br />camHeight = fcImagePlus->image.iRows;<br />return camHeight;<br />}<br />void PGRFireflyMultiCamera::grabFrame()<br />{<br />unsigned int uiCamera = 0;<br />FlyCaptureError err;<br />FlyCaptureImagePlus tmpImage[MAX_CAMERA_COUNT];<br />//<br />// Lock images.<br />//<br />for (uiCamera = 0; uiCamera < camCount; uiCamera ++)<br />{<br />err = flycaptureLockNext(context[uiCamera], &tmpImage[uiCamera]);<br />if( err != FLYCAPTURE_OK )<br />{<br />cout<<"flycaptureLockNext(): "<<flycaptureErrorToString(err)<<endl; </p><p>return ;<br />}<br />}<br />for (int row = 0; row < camHeight; row ++)<br />{<br />memcpy((fcImagePlus->image.pData + (row * camWidth)),<br /> (tmpImage[0].image.pData + (row * camWidth / 2)), camWidth / 2);<br />memcpy((fcImagePlus->image.pData + (row * camWidth) + camWidth / 2),<br /> (tmpImage[1].image.pData + (row * camWidth / 2)), camWidth / 2);<br />}<br />//<br />// Unlock all of the images effectively handing them back to the buffer pool.<br />//<br />for( uiCamera = 0; uiCamera < camCount; uiCamera++ )<br />{<br />err = ::flycaptureUnlock(context[uiCamera], tmpImage[uiCamera].uiBufferIndex );<br />if( err != FLYCAPTURE_OK )<br />{<br />cout<<"flycaptureUnlock(): "<<flycaptureErrorToString(err)<<endl; </p><p>return ;<br />}<br />}<br />}<br />

 以上是整個class的實現。下面是我的測試工程代碼

// PGRMultiCameraTest.cpp : Defines the entry point for the console application.<br />//<br />#include "stdafx.h"<br />#include "cv.h"<br />#include "highgui.h"<br />#include "./MyClass/ofxffmv.h"<br />#include "./MyClass/PGRFireFlyMultiCamera.h"<br />PGRFireflyMultiCamera * camera = new PGRFireflyMultiCamera();<br />const char * pTitle = "Capture Video From PGR Camera";<br />int cHeight = 0;<br />int cWidth = 0;<br />bool running = true;<br />DWORD WINAPI Capture(LPVOID)<br />{<br />IplImage * pImage = cvCreateImage(cvSize(cWidth, cHeight), IPL_DEPTH_8U, 1);<br />while(running)<br />{<br />pImage->imageData = (char *)(((camera->fcImagePlus)->image).pData);<br />camera->grabFrame();<br />cvShowImage(pTitle, pImage);<br />}<br />cvReleaseImage(&pImage);<br />return 0;<br />}<br />int _tmain(int argc, _TCHAR* argv[])<br />{<br />camera->listDevices();<br />camera->initFireflyCamera(640, 480, 30);<br />cHeight = camera->getCamHeight();<br />cWidth = camera->getCamWidth();<br />cvNamedWindow(pTitle, CV_WINDOW_AUTOSIZE);<br />HANDLE handle = CreateThread(NULL, 0, Capture, NULL, 0, 0);<br />while(running)<br />{<br />int key = cvWaitKey(0);<br />switch(key)<br />{<br />case 0x1b:<br />{<br />printf("Exiting.../n");<br />// Stop the capture thread<br />running = false;<br />// Wait for thread to exit<br />WaitForSingleObject(handle, 3000);<br />printf("Thread exited/n");<br />delete camera;<br />camera = NULL;<br />break;<br />}<br />}<br />}<br />if (NULL != camera)<br />{<br />delete camera;<br />camera = NULL;<br />}<br />return 0;<br />}<br />

 

  下面是測試

 

   歡迎有興趣的朋友來測試修改討論。我放到ccv中去測試,發現視頻有點延時,估計是因為線程問題。我會繼續將grabFrame建立成獨立線程後再在ccv中測試,然後結果和大家分享。

 

   BTW:現在已經有了PGR Firefly SDK 2.0了,我看了它內建的例子,發現代碼簡潔了很多,但是我在測試多網路攝影機的時候發現Camera::StartSyncCapture(...)這個函數在執行時死掉,程式不能繼續運行也不報錯。不知道為什麼,大家可以試試,等有了結果,我會將上面代碼更新到基於SDK 2.0的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.