采用MFC+ opencv 對basler pilot系列相機,通過千兆以太網接口實現圖像數據獲取,運行示例程序能夠正常獲取數據。但是示例程序中相機對象及數據流對象的初始化和數據的獲取都是在主函數中的。現在想在mfc中實現單幀圖像采集,如果每次采集都去重新獲取和初始化相機對象和數據流對象,時間很慢,處理結果就來不及了。嘗試著把初始化部分和數據采集部分分開,放在不同的函數中,程序調試時出現莫名的錯誤。想不明白是為什麼。不是語法錯誤。
相機初始化代碼:
[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("相機不存在,請確認相機是否正確連接!"));
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.SetValue( 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 the
// 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( GenICam::GenericException &e )// Error handling
{
TRACE("==========================An exception occurred!==================\n", e.GetDescription());
MessageBox(e.GetDescription(),NULL,MB_OK );
return FALSE;
}
}
圖像采集代碼:
[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 the
//// 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( GenICam::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
}