1. 程式人生 > >Basler|基於OpenCV的Basler相機採集影象程式

Basler|基於OpenCV的Basler相機採集影象程式

採用Basler4.0SDK編寫,利用Event機制在回撥函式中生成灰度影象
回撥函式中影象生成程式碼,利用CCD中獲取的無符號字元型陣列轉變成
Mat型別

Mat grab( siz, CV_8UC1, ptrGrabResult->GetBuffer(), siz.width*1 ); namedWindow( "test" ); imshow( "test", grab ); cvWaitKey(300);

生成的IplImage型別
IplImage* src = cvCreateImage( siz,IPL_DEPTH_8U, 1 ); cvSetData( src, ptrGrabResult->GetBuffer(), siz.width*1 ); cvNamedWindow("test"); cvShowImage( "test", src );

設定Basler相機的內部引數以及影象獲取
// Create an instant camera object with the camera device found first. CBaslerGigEInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());  // Print the model name of the camera. cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;  // For demonstration purposes only, register another image event handler. camera.RegisterImageEventHandler( new CSampleImageEventHandler, RegistrationMode_Append, Cleanup_Delete);  // Camera event processing must be activated first, the default is off. camera.GrabCameraEvents = true;  camera.Open();  // Set the AOI:  // On some cameras the Offsets are read-only, // so we check whether we can write a value. Otherwise, we would get an exception. // GenApi has some convenience predicates to check this easily. camera.OffsetX.SetValue(0); camera.OffsetY.SetValue(0); camera.Width.SetValue(1280); camera.Height.SetValue(960);  //Disable acquisition start trigger if available { GenApi::IEnumEntry* acquisitionStart = camera.TriggerSelector.GetEntry( TriggerSelector_AcquisitionStart); if ( acquisitionStart && GenApi::IsAvailable( acquisitionStart)) {     camera.TriggerSelector.SetValue( TriggerSelector_AcquisitionStart);     camera.TriggerMode.SetValue( TriggerMode_Off);     } }  // Set pixel format to Mono8 if available. if ( GenApi::IsAvailable( camera.PixelFormat.GetEntry(PixelFormat_Mono8))) {     camera.PixelFormat.SetValue(PixelFormat_Mono8); }  //Set exposure settings camera.ExposureMode.SetValue(ExposureMode_Timed); camera.ExposureTimeRaw.SetValue(1500); //100 by default  // The parameter MaxNumBuffer can be used to control the count of buffers // allocated for grabbing. The default value of this parameter is 10. camera.MaxNumBuffer = 5;  // Start the grabbing of c_countOfImagesToGrab images. // The camera device is parameterized with a default configuration which // sets up free-running continuous acquisition. camera.StartGrabbing();  // This smart pointer will receive the grab result data. CGrabResultPtr ptrGrabResult;  // Camera.StopGrabbing() is called automatically by the RetrieveResult() method // when c_countOfImagesToGrab images have been retrieved. while ( camera.IsGrabbing()) {     camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException); }