采用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); }