CCV uses two PGR Firefly camera source code (2) -- add a thread

Source: Internet
Author: User

Disclaimer: Anyone and organizations are welcome to repost the articles in this blog, but the original links and author information must be marked.

Link: http://blog.csdn.net/li_007/archive/2009/12/18/5033184.aspx

Pioneering little turtle -------> csdn

Now we have added a thread to process the source code for the camera to obtain data, so that you can easily discuss it and make some modifications to the code of the previous blog. It mainly inherits the ofxthread class of openframeworks, and ofxthread is very convenient for us to add threads. However, it is a bit difficult to test it independently. Of course, you can establish an openframeworks process for testing. The Code is as follows. I hope you can discuss it, leave a message, make improvements, and make mutual progress!

// <Br/> // pgrfireflymulticamera. h <br/> // </P> <p> // <br/> // written by leezhm, 15th Dec, 2009 <br/> // contact: leezhm@126.com <br/> // last modified by leezhm, 18th dec, 2009 <br/> // copyright (c) leezhm All Rights Reserved <br/> // </P> <p> # ifndef _ h_pgrfireflymulticamera __< BR/> # DEFINE _ h_pgrfireflymulticamera __</P> <p> # include "pgrflycapture. H "<br/> # include" pgrflycaptureplus. H "</P> <p> # include" O Fxthread. H "</P> <p> # include <iostream> </P> <p> Using STD: cout; <br/> Using STD: Endl; </P> <p> const unsigned int max_camera_count = 2; </P> <p> class pgrfireflymulticamera: Public ofxthread <br/>{< br/> PRIVATE: <br/> static unsigned int camcount; <br/> int camheight; <br/> int camwidth; </P> <p> flycapturecontext context [max_camera_count]; </P> <p> Public: <br/> flycaptureimageplus * fcimageplus; </P> <p> Public: <br /> Pgrfireflymulticamera (); <br/> ~ Pgrfireflymulticamera (); </P> <p> Public: <br/> static unsigned int getcameracount (); </P> <p> inline void showerrormessage (flycaptureerror & err, char * pfun) <br/>{< br/> If (flycapture_ OK! = ERR) <br/>{< br/> cout <"error:" <* pfun <"-->" <flycaptureerrortostring (ERR) <Endl; <br/>}</P> <p> void start () {<br/> startthread (true, false); // blocking, verbose <br/>}</P> <p> void stop () {<br/> stopthread (); <br/>}</P> <p> public: <br/> void initfireflycamera (INT width, int height, int framerate); </P> <p> int getcamwidth (); <br/> int getcamheight (); <br/> void listdevices (); </P> <p> bool isframenew (); <br/> unsigned char * getpixels (); </P> <p> void threadedfunction (); </P> <p >}; </P> <p> # endif/_ h_pgrfireflymulticamera __

The following is the CPP file, which is basically unchanged. Pay attention to stopping the thread in the destructor.

// <Br/> // pgrfireflymulticamera. CPP <br/> // </P> <p> # include "pgrfireflymulticamera. H "</P> <p> unsigned int pgrfireflymulticamera: camcount = max_camera_count; </P> <p> pgrfireflymulticamera: pgrfireflymulticamera () <br/>{< br/> fcimageplus = NULL; <br/>}</P> <p> pgrfireflymulticamera ::~ Pgrfireflymulticamera () <br/>{< br/> flycaptureerror err; <br/> unsigned int uicamera = 0; </P> <p> If (isthreadrunning ()) <br/>{< br/> stop (); <br/>}</P> <p> If (null! = Fcimageplus-> image. pdata) <br/>{< br/> Delete [] fcimageplus-> image. pdata; <br/>}</P> <p> If (null! = Fcimageplus) <br/>{< br/> Delete fcimageplus; <br/>}</P> <p> // <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 ()"); </P> <p> err = :: flycapturedestroycontext (context [uicamera]); <br/> showerrormessage (ERR, "flycapturebuse Numeratecameras () "); <br/>}</P> <p> void pgrfireflymulticamera: listdevices () <br/>{< br/> flycaptureerror err; <br/> flycaptureinfoex info [max_camera_count]; </P> <p> // <br/> // enumerate the bus and get the Count of camera <br/> // <br/> err = :: flycapturebusenumeratecamerasex (Info, & camcount); </P> <p> 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 "; <br/>}</P> <p> unsigned int pgrfireflymulticamera: getcameracount () <br/>{< br/> If (0 = camcount) <br/>{< br/>: flycapturebuscameracount (& camcount ); <br/>}</P> <p> return camcount; <br/>}</P> <p> void pgrfireflymulticamera: initfireflycamera (INT width, int height, int framerate) <br/>{< br/> flycaptureerror err; <br/> unsigned int uicamera = 0; </P> <p> // <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 ()"); </P> <p> cout <"Initializing camera" <uicamera <Endl; </P> <p> err =: flycaptureinitialize (context [uicamera], uicamera); <br/> showerrormessage (err, "Flycaptureinitializeplus ()"); <br/>}</P> <p> 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/>}</P> <p> // <br/> // start all of the cameras grabbing <br/> // <Br/> for (uicamera = 0; uicamera <camcount; uicamera ++) <br/>{< br/> cout <"Starting camera. /n "; <br/> err =: flycapturestartlocknext (context [uicamera], role, setframerate); <br/> showerrormessage (ERR," flycapturestart () "); <br/>}</P> <p> // <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 ()"); </P> <p> If (2 = camcount) <br/> {<br/> If (null = fcimageplus) <br/>{< br/> fcimageplus = new flycaptureimageplus (); <br/>}</P> <p> Fcimageplus-> image. icols = width; <br/> fcimageplus-> image. irows = height; </P> <p> 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/> else <br/>{< br/> cout <"there are more than 2 cameras! /N "; <br/> getchar (); </P> <p> return; <br/>}</P> <p> int pgrfireflymulticamera: getcamwidth () <br/>{< br/> camwidth = fcimageplus-> image. icols; </P> <p> return camwidth; <br/>}</P> <p> int pgrfireflymulticamera: getcamheight () <br/>{< br/> camheight = fcimageplus-> image. irows; </P> <p> return camheight; <br/>}</P> <p> unsigned char * pgrfireflymulticamera: getpixels (void) <br/>{< br/> return fcimageplus-> IMA Ge. pdata; <br/>}</P> <p> bool pgrfireflymulticamera: isframenew () <br/>{< br/> return isthreadrunning (); <br/>}</P> <p> void pgrfireflymulticamera: threadedfunction () <br/>{< br/> unsigned int uicamera = 0; </P> <p> flycaptureerror err; <br/> flycaptureimageplus tmpimage [max_camera_count]; </P> <p> while (isthreadrunning ()) <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/>}</P> <p> # pragma OMP parallel for <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); </P> <p> memcpy (fcimageplus-> image. pdata + (row * camwidth) + Camwidth/2), <br/> (tmpimage [1]. image. pdata + (row * camwidth/2), camwidth/2 ); <br/>}</P> <p> // <br/> // unlock all of the images into tively 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/>

 

After the thread is added this time, the video latency problem is solved. Up to 30 frames can be tested.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.