准备工作:
1、安装软件
下载并安装OpenCV库:登录Releases - OpenCV并下载OpenCV-4.6.0,完成后按提示安装即可。
2、相关配置
2.1、OpenCV库的相关配置
2.1.1、配置Windows系统环境变量
此电脑->属性->高级系统设置->高级->环境变量->系统变量->Path,在OpenCV的默认安装路径找到C:\opencv\build\x64\vc15\bin并将该路径添加到Path中然后单击确定。
2.1.2配置VS C++控制台项目属性
项目->属性->配置属性->VC++目录->可执行文件目录,添加OpenCV的安装路径:
C:\opencv\build\x64\vc15\bin
项目->属性->配置属性->VC++目录->包含目录,添加OpenCV的安装路径:
C:\Users\wenxi\opencv\build\include\opencv2
C:\Users\wenxi\opencv\build\include\opencv
C:\Users\wenxi\opencv\build\include
项目->属性->配置属性->VC++目录->库目录,添加OpenCV的安装路径:
C:\opencv\build\x64\vc15\lib
项目->属性->配置属性->链接器->输入->附加依赖项,添加C:\opencv\build\x64\vc15\lib中的opencv_world451d.lib文件。
2.2、配置MVS软件SDK
2.2.1、配置VS C++控制台项目属性
项目->属性->配置属性->链接器->常规->附加库目录,添加SDK的安装目录:
C:\Program Files (x86)\Softwares\MVS\Development\Libraries\win64,
C:\Program Files (x86)\Softwares\MVS\Development\Includes
项目->属性->配置属性->链接器->输入->附加依赖项,添加SDK的文件:MvCameraControl.lib
该文件在C:\Program Files (x86)\MVS\Development\Libraries\win64路径下:
参考文档:
海康威视工业相机SDK二次开发(VS+Opencv+QT+海康SDK+C++)(一)
海康工业相机SDK+OpenCV实例(1):海康SDK和OPENCV的安装与编译
OpenCV (一)—安装与配置 VS2019和OpenCV4.5.1
解决方案-Visual Studio设置通用配置(包含路径+依赖库)
开始编程与调试
安装好MVS后,在安装路径C:\Program Files (x86)\MVS\Development\Documentations可以找到官方SDK的使用说明,打开工业相机SDK开发指南V4.0.0(C)可以查看海康威视相机二次开发教程:
此处选择GrabImage.cpp进行调试:
#include
#include
#include
#include
#include "MvCameraControl.h"
bool g_bExit = false;
// ch:等待按键输入 | en:Wait for key press
void WaitForKeyPress(void)
{
while(!_kbhit())
{
Sleep(10);
}
_getch();
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
printf("Not support.\n");
}
return true;
}
static unsigned int __stdcall WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT stOutFrame = {0};
while(true)
{
nRet = MV_CC_GetImageBuffer(pUser, &stOutFrame, 1000);
if (nRet == MV_OK)
{
printf("Get Image Buffer: Width[%d], Height[%d], FrameNum[%d]\n",
stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum);
nRet = MV_CC_FreeImageBuffer(pUser, &stOutFrame);
if(nRet != MV_OK)
{
printf("Free Image Buffer fail! nRet [0x%x]\n", nRet);
}
}
else
{
printf("Get Image fail! nRet [0x%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
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;
}
printf("Please Input camera index(0-%d):", stDeviceList.nDeviceNum-1);
unsigned int nIndex = 0;
scanf_s("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Input error!\n");
break;
}
// ch:选择设备并创建句柄 | en: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;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
// ch:设置触发模式为off | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
//ch:获取Enum型节点指定值的符号 | en:Get the symbol of the specified value of the Enum type node
MVCC_ENUMVALUE stEnumValue = {0};
MVCC_ENUMENTRY stEnumEntry = {0};
nRet = MV_CC_GetEnumValue(handle, "PixelFormat", &stEnumValue);
if (MV_OK != nRet)
{
printf("Get PixelFormat's value fail! nRet [0x%x]\n", nRet);
break;
}
stEnumEntry.nValue = stEnumValue.nCurValue;
nRet = MV_CC_GetEnumEntrySymbolic(handle, "PixelFormat", &stEnumEntry);
if (MV_OK != nRet)
{
printf("Get PixelFormat's symbol fail! nRet [0x%x]\n", nRet);
break;
}
else
{
printf("PixelFormat:%s\n", stEnumEntry.chSymbolic);
}
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nThreadID = 0;
void* hThreadHandle = (void*) _beginthreadex( NULL , 0 , WorkThread , handle, 0 , &nThreadID );
if (NULL == hThreadHandle)
{
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
g_bExit = true;
Sleep(1000);
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
MV_CC_DestroyHandle(handle);
handle = NULL;
}
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}
报错1:
没有将准备工作中的MVS SDK配置好,
项目->属性->配置属性->链接器->常规->附加库目录,添加SDK的安装目录:
C:\Program Files (x86)\Softwares\MVS\Development\Libraries\win64,
C:\Program Files (x86)\Softwares\MVS\Development\Includes
项目->属性->配置属性->链接器->输入->附加依赖项,添加SDK的文件:MvCameraControl.lib即可。
报错2:
项目->属性->配置属性->常规->公共语言运行时支持,选择无公共语言运行时支持即可。
参考文档:
海康威视相机MVS软件SDK文档
C++通过DLL调用C#代码
编译器警告(等级 1 和等级 3)C4793
链接器工具错误 LNK2019