1:安装相机驱动
1. 机器视觉-下载中心 如下图所示
2. 下载相机驱动(客户端)
3. 下载简单示例程序(SDK)
.
下载完成后文件下如下图所示
.顺序点击Development文件夹,Includes文件夹
注意:我们在开发中需要使用这8个头文件
2:配置 Visual Studio(2017及以上版本)
1. 新建C++控制台应用
2. 在该项目下,新建立 lib,bin,include三个文件夹
3. 将之前8个头文件放在include文件夹中,以及C++ SDK包中的include文件夹
4. 把Development\Libraries\Win64下的MvCameraControl.lib放在lib文件夹中,以及C++ SDK包中的lib文件夹
5. 将C++ SDK包中的bin文件夹下所有文件复制到现在项目bin目录下
3.代码开发
1. 查看是否有可用的设备,成功会列举出设备,失败会返回错误码。
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset
(&stDeviceList,
0
,
sizeof
(MV_CC_DEVICE_INFO_LIST));
//
搜索所有的可⽤设备,并返回状态码,成功返回
0
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
//
如果搜索设备失败,返回特定的状态码。
if
(MV_OK != nRet)
{
printf
(
"Enum Devices fail! nRet [0x%x]\n"
, nRet);
break
;
}
//
打印所有的可⽤设备的信息
if
(stDeviceList.nDeviceNum >
0
)
{
for
(
unsigned int
i =
0
; i < stDeviceList.nDeviceNum; i++)
{
printf
(
"[device %d]:\n"
, i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if
(
NULL
== pDeviceInfo)
{
break
;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf
(
"Find No Devices!\n"
);
break
;
}
1.1. 失败,显示没有可用的设备
1.2. 成功,显示可用的设备为设备0,并且显示了当前的IP地址,以及设备名称。
2. 选择使用一个相机,并且打开它。
//
输入数字,选择⼀个特定的相机
printf
(
"Please Input camera index:"
);
unsigned int
nIndex =
0
;
scanf_s(
"%d"
, &nIndex);
if
(nIndex >= stDeviceList.nDeviceNum)
{
printf
(
"Input error!\n"
);
break
;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if
(MV_OK != nRet)
{
printf
(
"Create Handle fail! nRet [0x%x]\n"
, nRet);
break
;
}
// Open device
nRet = MV_CC_OpenDevice(handle);
if
(MV_OK != nRet)
{
printf
(
"Open Device fail! nRet [0x%x]\n"
, nRet);
break
;
}
2.1. 失败,根据错误码进行纠错
2.2. 成功,获取了一帧图像
3. 一秒采集一张图片,进行预测
修改采样的频率
// Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if
(MV_OK != nRet)
{
printf
(
"Stop Grabbing fail! nRet [0x%x]\n"
, nRet);
break
;
}
// change time if you want, such as 2 seconds is 2000
// 5 scconds is 5000
// 0.5 seconds is 500
Sleep(
1000
);