怎么样才能创建自己的网站,做网站开发 用的最多的语言,合肥网站建站,东莞房价还会涨吗basler工业相机做双目视觉用#xff0c;出现很多问题记录一下#xff1a;
首先是多看手册#xff1a;https://zh.docs.baslerweb.com/software
手册内有所有的源码和参考示例#xff0c;实际上在使用过程中#xff0c;大部分都是这些源码#xff0c;具体项目选择对应的…basler工业相机做双目视觉用出现很多问题记录一下
首先是多看手册https://zh.docs.baslerweb.com/software
手册内有所有的源码和参考示例实际上在使用过程中大部分都是这些源码具体项目选择对应的示例代码。
一、相机和镜头选型
可以通过balser的镜头选型工具按照自己需要测量目标的距离目标大小等信息进行配置选择地址在这界面如下 相机的焦距大小一般和测量的目标距离有关系一般对应的选择方式总结如下 1焦距越小测量的范围越大相应的目标一般都看起来很小类似于手机拍照的最小倍数拍摄目标 2焦距越大测量范围就越窄对远距离小目标的检查就比较好但是太大的焦距会导致大目标容易拍不全类似与手机拍照放大倍数拍摄目标 3具体有计算公式可以参考建议直接通过basler的配置工具进行选择
二、相机安装和使用
1balser相机使用需要网线和电源线12V供电可以在官网找对应的线材 2相机接上电源网线连接计算机后去官网下载相机软件和驱动一般windows下安装后会在目录下生成对应的文件夹包括C对应的库和参考代码示例等linux下直接下载tar.gz包解压防止到对应的文件目录下执行source pylon目录/bin/pylon-setup-env.sh ./pylon目录 即可例如我安装的是pylon7.3.0解压后的目录为/opt/pylon7,里面直接就是inlcude这些文件夹在linux下目录执行
source /opt/pylon7/bin/pylon-setup-env.sh /opt/pylon7完成linux下相机驱动和环境的安装。 3配置相机IPwindows下配置相机IP可以使用basler的工具 ipconfiguratorLinux下如果安装的系统有GUI界面一样可以使用配置相机IP地址保证IP地址和接计算机的接口IP地址在同一网段即可子网掩码推荐255。255.255.0厂家说其他的可能会出现异常断开错误 4IP配置完成后在windows下启动pylon Viewer工具查看相机能找到相机说明IP配置正确确保计算机和相机处于同一个网段内 5要保持basler相机的帧率需要保证千兆网如果是两个相机连交换机之后就要求是2000M的网依次类推才可以达到最大帧率否则会出现相机丢帧等问题直连相机到计算机不影响开启巨帧数就行了。
三、C调用相机环境部署
1.相机驱动和软件安装完成后有以下文件夹 开发环境在development文件夹下linux下解压之后是 2.可以使用visua studio等C IDE开发工具进行编程将依赖库和头文件等信息通过链接include和lib文件即可开始相机调用
四、C相机调用
1.相机初始化
相机初始化包括寻找相机匹配对应相机还可以设置相机的曝光帧率增益等两个相机的初始化依据API文档整理如下 参加APIGrab_MultipleCameras
void initCamera()
{try{PylonInitialize(); //初始化CTlFactory tlFactory CTlFactory::GetInstance();pTL dynamic_castIGigETransportLayer*(tlFactory.CreateTl( BaslerGigEDeviceClass ));if (pTL NULL){throw RUNTIME_EXCEPTION( No GigE cameras available. );}DeviceInfoList_t allDeviceInfos;if (pTL-EnumerateDevices( allDeviceInfos ) 0){throw RUNTIME_EXCEPTION( No GigE cameras available. );}DeviceInfoList_t usableDeviceInfos;string left_camera_ip172.16.105.21//left_camera_ip 用于区分两个相机可以通过配置的IP序列号等进行区分if (string(allDeviceInfos[0].GetIpAddress()) left_camera_ip) {usableDeviceInfos.push_back(allDeviceInfos[0]);subnet allDeviceInfos[0].GetSubnetAddress();//主相机usableDeviceInfos.push_back(allDeviceInfos[1]);LOG(INFO)主相机allDeviceInfos[0].GetIpAddress()endl;LOG(INFO)副相机allDeviceInfos[1].GetIpAddress()endl;}else if(string(allDeviceInfos[1].GetIpAddress()) left_camera_ip) {usableDeviceInfos.push_back(allDeviceInfos[1]);subnet allDeviceInfos[1].GetSubnetAddress();//主相机usableDeviceInfos.push_back(allDeviceInfos[0]);LOG(INFO)主相机IPallDeviceInfos[1].GetIpAddress()endl;LOG(INFO)SubnetAddressallDeviceInfos[1].GetSubnetAddress()endl;LOG(INFO)DefaultGatewayallDeviceInfos[1].GetDefaultGateway()endl;LOG(INFO)SubnetMaskallDeviceInfos[1].GetSubnetMask()endl;LOG(INFO)副相机IPallDeviceInfos[0].GetIpAddress()endl;LOG(INFO)SubnetAddressallDeviceInfos[0].GetSubnetAddress()endl;LOG(INFO)DefaultGateway: allDeviceInfos[0].GetDefaultGateway()endl;LOG(INFO)SubnetMaskallDeviceInfos[0].GetSubnetMask()endl;}else{LOG(INFO) Camera IP is error ,please set IP endl;}// CInstantCameraArray cameras { 2 };//初始化两个相机for (size_t i 0; i 2; i){cameras[i].Attach(tlFactory.CreateDevice(usableDeviceInfos[i]));const CBaslerGigEDeviceInfo di cameras[i].GetDeviceInfo();LOG(INFO) Camera serial: di.GetSerialNumber() endl;}// srand( (unsigned) time( NULL ) );
// DeviceKey rand();
// GroupKey 0x112233;for (size_t i 0; i cameras.GetSize(); i){cameras[i].Attach( tlFactory.CreateDevice( usableDeviceInfos[i] ) );//cameras[i].RegisterConfiguration( new CActionTriggerConfiguration( DeviceKey, GroupKey, AllGroupMask ), RegistrationMode_Append, Cleanup_Delete );//cameras[i].SetCameraContext( i );const CBaslerGigEDeviceInfo di cameras[i].GetDeviceInfo();cout Using camera i : di.GetSerialNumber() ( di.GetIpAddress() ) endl;}cameras.Open();//相机基本设置SetCamera(cameras[0], Type_Basler_ExposureTimeAbs, expore_time_l); //曝光时间SetCamera(cameras[0], Type_Basler_GainRaw, gain_l); //增益SetCamera(cameras[0], Type_Basler_AcquisitionFrameRateAbs, fps_l); //频率SetCamera(cameras[0], Type_Basler_Width, 2448);SetCamera(cameras[0], Type_Basler_Height, 2048);SetCamera(cameras[1], Type_Basler_ExposureTimeAbs, expore_time_r); //曝光时间SetCamera(cameras[1], Type_Basler_GainRaw, gain_r); //增益SetCamera(cameras[1], Type_Basler_AcquisitionFrameRateAbs, fps_r); //频率SetCamera(cameras[1], Type_Basler_Width, 2448);SetCamera(cameras[1], Type_Basler_Height, 2048);//设置相机触发模式 TriggerSelector//TriggerSoftware//主相机设置为软件触发输出设置为exposure active//SetCamera(cameras[0], Type_Basler_Freerun, 0);//从相机设置触发模式为外触发IO设置为1//SetCamera(cameras[1], Type_Basler_Line1, 0);}catch (const GenericException e){if(cameras.IsGrabbing())cameras.StopGrabbing();// Error handlingLOG(INFO) init,An exception occurred. endl e.GetDescription() endl;}
}
2.调用相机
调用相机时最容易出现的问题是grab image丢帧在抓取图像丢帧的原因中主要是由相机帧率设置过大带宽不足等问题导致。 其中 if(cameras.IsGrabbing()) 这句可以改成while循环这样就可以持续输出if表示就近输出了根据实际使用情况而定基本上抓取一帧在50ms左右。其中 cameras.StartGrabbing(可以放在初始化中这样就不停的抓取能够保证1秒20帧不用频繁开始抓取和停止抓取实际上很耗时。
void GetCameraImage() {try {//pTL-IssueActionCommand(DeviceKey, GroupKey, AllGroupMask, subnet );//1秒内抓取了多少张图全部存下来int skiptime 1000;//LOG(INFO)采集图像的最长时间skiptime msendl;cameras.StartGrabbing(GrabStrategy_OneByOne,GrabLoop_ProvidedByUser);if(cameras.IsGrabbing()) {std::chrono::high_resolution_clock::time_point tStartTime(std::chrono::high_resolution_clock::now());int lTimeAloInterval 0;count_grab_once;cameras[0].RetrieveResult(skiptime, ptrGrabResultl, TimeoutHandling_ThrowException);cameras[1].RetrieveResult(skiptime, ptrGrabResultr, TimeoutHandling_ThrowException);if (ptrGrabResultl-GrabSucceeded() ptrGrabResultr-GrabSucceeded() ) {intptr_t cameraContextValuel ptrGrabResultl-GetCameraContext();intptr_t cameraContextValuer ptrGrabResultr-GetCameraContext();const uint8_t *pImageBufferl (uint8_t *) ptrGrabResultl-GetBuffer();const uint8_t *pImageBufferr (uint8_t *) ptrGrabResultr-GetBuffer();// 将 pylon image转成OpenCV image.Mat SaveImagel cv::Mat(ptrGrabResultl-GetHeight(), ptrGrabResultl-GetWidth(), CV_8UC1,(uint8_t *) pImageBufferl);Mat SaveImager cv::Mat(ptrGrabResultr-GetHeight(), ptrGrabResultr-GetWidth(), CV_8UC1,(uint8_t *) pImageBufferr);}lTimeAloInterval std::chrono::duration_caststd::chrono::durationdouble, std::ratio1, 1000 (std::chrono::high_resolution_clock::now() - tStartTime).count();LOG(INFO) ------------ single Grab image cost time:---------------- lTimeAloInterval ms endl;}cameras.StopGrabbing();}catch (const GenericException e){if(cameras.IsGrabbing())cameras.StopGrabbing();// Error handlingLOG(INFO) An exception occurred. endl e.GetDescription() endl;}}3.关闭相机
相机及时关闭
void CloseCamera()
{//最后终止Pylon相机即调用PylonTerminate。//关闭摄像头try{if (cameras.IsOpen()) {cameras.DetachDevice();cameras.Close();cameras.DestroyDevice();//关闭库LOG(INFO) SBaslerCameraControl deleteAll: PylonTerminate;PylonTerminate();}}catch (const Pylon::GenericException e){LOG(INFO) close camera failed... e.what();}
}五、常见问题
1.相机连不上
IP配置不正确确保在同一网段子网掩码相同。
2.连接后丢帧 Error: e1004 The bufer was incopletely gratbed. This can be caused by perfomnane problens of the metwork hardware used,fer underuns can also case ina loss.To fix this, us the pylonbioEtonfigurator tol to optinize your setip and use more uffers for aratbin in your aplication to prerent buferunderruns常见于连接多个相机的时候出现确保开启巨帧相机帧率和网络传输的帧率是否满足要求参加API手册
3.相机连接后使用过程中出现找得到相机但卡住不动了
这种问题不知道是什么原因导致的可能是长时间没调用导致相机休眠关机重启下可解决。
4.grab time out 抓取超时
一般是设置waittime时间太短导致可以改大一些 cameras[1].RetrieveResult(waittime, ptrGrabResultr, TimeoutHandling_ThrowException);还有一种可能是相机连上之后一直么有抓取成功导致等待时间过长需要检查代码常见有时候做触发的操作导致。