用Matlab提供的通用相机库,不太好用。后来从Matlab官网上找到了一个第三方提供的Basler camera driver:https://cn.mathworks.com/matlabcentral/fileexchange/50681-basler-camera-driver。其包括以下常用函数:
baslerFindCamera: returns a cell arraycontaining the camera index and the camera name.
- baslerCameraInfo: returns a struct containing all parameters of the selectedcamera.
- baslerSetParameter: sets a camera parameter.
- baslerGetParameter: returns the selected camera parameter.
- baslerSetROI: sets the region of interest (ROI).
- baslerPreview: displays a preview image.
- baslerGetData: captures and returns the selected number of frames.
- baslerSaveData: captures and saves the selected number of frames to disk.
由于其用的驱动都比较旧,直接运行不能通过。所以要做些更改。
首先准备两个软件/库,一个是basler提供的pylon 5(下载最新的window版本就行,浏览器有去广告插件的可能显示不全)。另一个是[Boost C++Libraries]http://www.boost.org/。之后将下载的Boost解压后放到D盘的根目录下。依次运行bootstrap.bat,b2.exe(不知道有没有用,得运行一个多小时),bjam.exe。之后给Boost设置环境变量:在环境变量中添加变量名称:BOOST_ROOT,并将值设成D:\boost_1_66_0(注意要改成Boost对应的版本号)。之后才能运行Matlab。如果在添加环境变量的时候,Matlab还运行着,就会导致Matlab找不到BOOST_ROOT(重要的事情说三遍!作者折腾了一个多小时才发现这个问题)。
然后修改下载的Matlab_BaslerCamDriver-v0.9.2-0中的make.m文件。将其中的ipaths和lpaths用以下代码替换:
ipaths = [ '-I"',fullfile(getenv('PYLON_DEV_DIR'),'include'),'"' ...
' ',...
'-I"',fullfile(getenv('BOOST_ROOT')),'"',...
];
% Library paths
lpaths = [ '-L"',fullfile(getenv('PYLON_DEV_DIR'),'lib\x64'),'"', ...
' ','"',fullfile(getenv('BOOST_ROOT'),'stage\lib'),'"', ...
];
并在baslerGetParameter.cpp中添加
#include <boost/assign/std/map.hpp>
最后运行make.m文件,如果看到输出以下语句就证明成功了:
=> Creating Libraries
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
=> Creating Functions
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
Building with 'Microsoft Visual C++ 2015Professional'.
MEX completed successfully.
这时候,就可以直接调用生成的函数了。
比如首先寻找相机:
available_cams =baslerFindCameras();
得到available_cams中相机编号0,再运行baslerPreview(0)就能预览相机,修改其中参数就能得到不同的效果。如果大家觉得麻烦,可以直接下载我编译好的库(可以直接运行,但是不要运行make.m不然会将编译好的文件删除)
http://download.csdn.net/download/fengyuzhe13/10173074 或者在CSDN中搜索
‘win10中 Matlab 连接 Basler 相机’