CCV使用两个PGR Firefly摄像头源码(二)----添加线程

本文介绍了一个使用线程处理多摄像头数据的方法,通过增加线程解决了视频延时问题,并实现了30帧的流畅显示。该方法适用于需要从多个摄像头同时获取图像的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。  

本文链接:http://blog.youkuaiyun.com/li_007/archive/2009/12/18/5033184.aspx

开拓进取的小乌龟------->优快云点滴点点滴滴Blog

现在将增加了线程来处理摄像头获取数据的源码贴出来,方便大家讨论,与上一篇blog的代码有一点点修改。主要是继承了openframeworks的ofxThread类,ofxThread很方便我们增加线程。但是如果单独测试有点麻烦的,当然你可以建立一个openframeworks的过程来测试。代码如下,希望大家讨论,留言,改进,互相进步!

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

下面是CPP文件,基本没变动的,注意下析构函数中停止线程。

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

 

这次增加了线程后,解决了视频延时的问题。可以测试达到30帧。 

转载于:https://www.cnblogs.com/leezhm/archive/2009/12/18/2560310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值