Basler pilot series camera MFC opencv

Source: Internet
Author: User

Use MFC + opencv to obtain image data for basler pilot series cameras through a Gigabit Ethernet interface, and run the sample program to obtain data normally. However, in the example program, camera object and Data Stream object initialization and data acquisition are all in the main function. Now we want to achieve single-frame image acquisition in mfc. If we re-acquire and initialize the camera object and Data Stream object for each collection, the time is very slow and the processing result is too late. I tried to separate the initialization part from the data collection part and put it in different functions. An inexplicable error occurred during program debugging. I don't know why. It is not a syntax error.

Camera initialization code:

[Cpp]
BOOL CPylonGrabView: OnInitialCamera ()
{
Pylon: PylonAutoInitTerm autoInitTerm;
 
Try
{
CTlFactory & TlFactory = CTlFactory: GetInstance ();

/* ITransportLayer **/pTl = TlFactory. CreateTl (Camera_t: DeviceClass ());
DeviceInfoList_t devices;
If (0 = pTl-> EnumerateDevices (devices) // Enumerate GigE cameras
{
MessageBox (_ T ("The camera does not exist. Check whether the camera is correctly connected! "));
TRACE ("============================ No camera present! ===================================\ N ");
M_bIsCameraPresent = FALSE;
Return FALSE;
}
M_bIsCameraPresent = TRUE;
// Camera_t Camera = pTl-> CreateDevice (devices [0]); // Create a camera object
M_Camera.Attach (pTl-> CreateDevice (devices [0]);
M_Camera.Open (); // Open the camera object
// ============ Parameterize the camera
M_Camera.PixelFormat.SetValue (PixelFormat_BayerBG8); // Bayer BG 8 pixel format */
 
M_Camera.Width.SetValue (m_iwidth); // Maximized AOI
M_Camera.Height.SetValue (m_iheight );
M_Camera.OffsetX.SetValue (m_ioffset_x );
M_Camera.OffsetY.SetValue (m_ioffset_y );
 
// Continuous mode, software trigger used
M_Camera.TriggerSelector.SetValue (TriggerSelector_AcquisitionStart );
M_Camera.TriggerMode.SetValue (TriggerMode_On );
M_Camera.AcquisitionMode.SetValue (AcquisitionMode_SingleFrame );
M_Camera.TriggerSource.SetValue (TriggerSource_Software );
 
M_Camera.ExposureMode.SetValue (ExposureMode_Timed); // Configure exposure time and mode
M_camera.exposuretimeraw.setvalues (200 );
// Get and open a stream grabber
/* CBaslerGigECamera: StreamGrabber_t */StreamGrabber. Attach (m_Camera.GetStreamGrabber (0 ));
StreamGrabber. Open ();
Const int bufferSize = (int) m_Camera.PayloadSize ();
Const int numBuffers = 10;
StreamGrabber. MaxBufferSize = bufferSize;
StreamGrabber. MaxNumBuffer = numBuffers;
StreamGrabber. PrepareGrab ();
// Allocate and register image buffers, put them into
// Grabber's input queue
Unsigned char * ppBuffers [numBuffers];
MyContext context [numBuffers];
StreamBufferHandle handles [numBuffers];
For (int I = 0; I <numBuffers; ++ I)
{
PpBuffers [I] = new unsigned char [bufferSize];
Handles [I] = StreamGrabber. RegisterBuffer (ppBuffers [I], bufferSize );
StreamGrabber. QueueBuffer (handles [I], & context [I]);
}
 
}
Catch (genmpo: GenericException & e) // Error handling
{
TRACE ("==================== An exception occurred! =============================\ N ", e. GetDescription ());
MessageBox (e. GetDescription (), NULL, MB_ OK );
Return FALSE;
}
 
}


Image Collection code:

[Cpp]
BOOL CPylonGrabView: Grab ()
{
StartCounter ();
Try
{
// IStreamGrabber * pGrabber = m_Camera.GetStreamGrabber (0 );
// CBaslerGigECamera: StreamGrabber_t StreamGrabber =
// M_Camera.GetStreamGrabber (0 );
/* StreamGrabber. Open ();*/
// Parameterize the stream grabber
// Const int bufferSize = (int) m_Camera.PayloadSize ();
// Const int numBuffers = 10;
// StreamGrabber. MaxBuf ferSize = bufferSize;
// StreamGrabber. MaxNumBuffer = numBuffers;
// StreamGrabber. PrepareGrab ();
/// Allocate and register image buffers, put them into
/// Grabber's input queue
// Unsigned char * ppBuffers [numBuffers];
// MyContext context [numBuffers];
// StreamBufferHandle handles [numBuffers];
// For (int I = 0; I <numBuffers; ++ I)
//{
// PpBuffers [I] = new unsigned char [bufferSize];
// Handles [I] = StreamGrabber. RegisterBuffer (ppBuffers [I], bufferSize );
// StreamGrabber. QueueBuffer (handles [I], & context [I]);
//}
M_Camera.AcquisitionStart.Execute (); // Start image acquisition
M_Camera.TriggerSoftware.Execute ();

GrabResult Result;

If (StreamGrabber. GetWaitObject (). Wait (3000) // Wait for the grabbed image with a timeout of 3 seconds
{

If (! StreamGrabber. RetrieveResult (Result) // Get an item from the grabber's output queue
{
Cerr <"Failed to retrieve an item from the output queue" <endl;
Return FALSE;
}
If (Result. Succeeded ())
{

BOOL bsuccendPr = ProcessImage (unsigned char *) Result. Buffer (), Result. GetSizeX (), Result. GetSizeY ());

}
Else
{
Cerr <"Grab failed:" <Result. GetErrorDescription () <endl;
Return FALSE;
}
// Requeue the buffer
// If (I + numBuffers <m_numGrabs/* numGrabs */)
// StreamGrabber. QueueBuffer (Result. Handle (), Result. Context ());
}
Else
{
TRACE ("========== timeout occurred when waiting for a grabbed image ======== ");
Return FALSE;
}

// Finished. Stop grabbing and do clean-up
M_Camera.AcquisitionStop.Execute (); // The camera is in continuous mode, stop image acquisition
StreamGrabber. CancelGrab (); // Flush the input queue, grabbing may have failed

While (StreamGrabber. GetWaitObject (). Wait (0) // Consume all items from the output queue
{
StreamGrabber. RetrieveResult (Result );
If (Result. Status () = Canceled)
{
Cout <"Got canceled buffer" <endl;
}
}

// For (int I = 0; I <numBuffers; ++ I) // Deregister and free buffers
//{
// StreamGrabber. DeregisterBuffer (handles [I]);
// Delete [] ppBuffers [I];
//}

StreamGrabber. FinishGrab (); // Clean up
StreamGrabber. Close ();
M_Camera.Close ();
// TlFactory-> ReleaseTl (pTl );
}
Catch (genmpo: GenericException & e) // Error handling
{
TRACE ("==================== An exception occurred! =============================\ N ", e. GetDescription ());
MessageBox (e. GetDescription (), NULL, MB_ OK );
Return FALSE;
}
TRACE ("=============== StopGrab =======================\ n ");
TRACE ("=========== GetCounter () ===% f =============================\ n ", getCounter ());
Return TRUE; // Quit application
}

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.