海康工业相机参数设置与获取

通用接口介绍

拿到海康工业相机之后,通过其官方提供的MVS客户端,我们可以设置相关相机参数,来调整图像,达到我们想要的效果,但是如此众多的相机参数,我们该如何集成进入我们软件呢
在这里插入图片描述
从查询SDK文档中,可以发现,他们提供了一套通用接口,来对相机进行参数获取与设置
在这里插入图片描述
同用接口把相机参数,分成七大类,除command参数外,每一类提供Set/Get接口来设置与获取相关节点

类型 描述
Int 整数型参数
Enum 枚举型参数
float 浮点型参数
bool 布尔型参数
float 浮点型参数
string 字符串型参数
Command 命令型参数

以MV_CC_GeIntValue为例:在这里插入图片描述
代码示例,获取 Width:

//获取int型参数
     MVCC_INTVALUE struIntValue = {
    
    0}; 
    nRet = MV_CC_GetIntValue(handle, "Width", &struIntValue);
    if (MV_OK != nRet)
    {
    
    
        printf("error: GetIntValue fail [%x]\n", nRet);
        return;
    }

这里的第二个参数“Width”,作为整个接口的关键参数,文档中,告知我们要去查找xml属性表,然后对着查表进行参数设置,xml表如下所示:
在这里插入图片描述

功能 名称(GetNode: key参数) 数据类型 数值范围定义 访问模式 描述
Image Format Control Width IInteger >0 R/(W) ROI的宽

通过查表,我们可以知道key值该填什么,key值的属性是什么,但是,这样子太慢啦,我怎么知道我调的参数,在xml表的什么位置呢
通过观察MVS客户端,有一个更简单的方法能够知道相机参数的属性,类型等,可以快速方便的对参数进行操作

  • 在MVS中找到自己想要的参数,鼠标选中它,在MVS右下角,参数描述中,能看看该参数的节点名、类型、取值范围、步进等信息
    在这里插入图片描述
    上图可知,图像宽度“Width”,其节点名为“Width”,类型是“int”,取值范围是32~2048,步进是16;
    根据类型,我们就可以选用MV_CC_SetIntValue/MV_CC_GetIntValue来对该属性进行操作;
    还需注意以下两点:
  • 不同的相机参数有不同的类型,取值范围与步进
  • 不同的相机,相同的参数,有不同的取值范围与步进

常见相机参数设置获取

Int型参数-图像宽度、图像高度

//获取int型参数-Width值
MVCC_INTVALUE struIntValue = {
    
    0}; 
nRet = MV_CC_GetIntValue(handle, "Width", &struIntValue);
if (MV_OK != nRet)
{
    
    
	printf("error: GetIntValue fail [%x]\n", nRet);
}
//打印当前宽度,宽带最大值,最小值,步进
printf("Width;%d,Width_Max:%d,Width_min:%d,Width_Inc:%d\n", 
struIntValue.nCurValue,struIntValue.nMax,struIntValue.nMin,struIntValue.nInc);
//设置int型参数-Width值
unsigned int nValue = 752;
//注意点1:value值需要是步进值的整数倍,否则会失败
//注意点2:宽度、高度等参数设置时,只有在MV_CC_Startgrab接口调用前才能设置,取流过程中,不能修改宽高
//注意点3:宽度、高度等参数设置时,若有Offset X、Y偏移,应当先调用相关接口,将偏移量置0
nRet = MV_CC_SetIntValue(handle, "Width", nValue);
if (MV_OK != nRet)
{
    
    
	printf("error: SetIntValue fail [%x]\n", nRet);
}

Enum型参数-图像格式、触发模式设置

//获取Enum型参数-相机图像格式
MVCC_ENUMVALUE struEnumValue = {
    
    0}; 
nRet = MV_CC_GetEnumValue(handle, "PixelFormat", &struEnumValue);
if (MV_OK != nRet)
{
    
    
	printf("error: GetEnumValue fail [%x]\n", nRet);
}
 //设置Enum型参数-相机图像格式
 //注意点1:相机图像格式设置时,只有在MV_CC_Startgrab接口调用前才能设置,取流过程中,不能修改图像格式
 nRet = MV_CC_SetEnumValue(handle, "PixelFormat", PixelType_Gvsp_Mono12);
if (MV_OK != nRet)
{
    
    
	printf("error: SetEnumValue fail [%x]\n", nRet);
}
//设置Enum型参数-设置触发模式为on 
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_ON);
if (MV_OK != nRet)
{
    
    
	printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
}

float 型参数-曝光、增益设置

//获取float型参数-曝光参数获取
MVCC_FLOATVALUE struFloatValue = {
    
    0}; 
nRet = MV_CC_GetFloatValue(handle, "ExposureTime", &struFloatValue);
if (MV_OK != nRet)
{
    
    
    printf("error: GetFloatValue fail [%x]\n", nRet);
}
//打印曝光当前值,可设置的最大值、最小值
printf("Exp;%d,Exp_Max:%d,Exp_min:%d\n", struFloatValue.fCurValue,struFloatValue.fMax,struFloatValue.fMin);
//获取float型参数-增益参数获取
MVCC_FLOATVALUE struFloatValue = {
    
    0}; 
nRet = MV_CC_GetFloatValue(handle, "Gain", &struFloatValue);
if (MV_OK != nRet)
{
    
    
    printf("error: GetFloatValue fail [%x]\n", nRet);
}
//打印增益当前值,可设置的最大值、最小值
printf("Gain;%d,Gain_Max:%d,Gain_min:%d\n", struFloatValue.fCurValue,struFloatValue.fMax,struFloatValue.fMin);
//获取float型参数-曝光值
//注意点1:曝光设置有前置条件需要注意,例如自动曝光、曝光模式等,前置条件是否调用,可以再MVS里面观察,设置曝光时,是否提前勾选了其他参数
float fValue = 1000; 
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", fValue);
if (MV_OK != nRet)
{
    
    
	printf("error: SetFloatValue fail [%x]\n", nRet);
}
//获取float型参数-增益值
//注意点1:增益设置有前置条件需要注意,例如自动增益,前置条件是否调用,可以再MVS里面观察,设置增益时,是否提前勾选了其他参数
//注意点2:海康小部分相机增益节点不叫“Gain”,也不是用float节点进行设置,如果遇到了,就在MVS里面观察使用对应接口吧
float fValue = 5; 
nRet = MV_CC_SetFloatValue(handle, "Gain", fValue);
if (MV_OK != nRet)
{
    
    
	printf("error: SetFloatValue fail [%x]\n", nRet);
}

string型参数-用户名称

//获取string型参数
MVCC_STRINGVALUE struStringValue = {
    
    0}; 
nRet = MV_CC_GetStringValue(handle, "DeviceUserID", &struStringValue);
if (MV_OK != nRet)
{
    
    
	printf("error: GetStringValue fail [%x]\n", nRet);
}
printf("DeviceUserID:[%s]\n", struStringValue.chCurValue);
//设置string型参数-自定义用户名称
 nRet = MV_CC_SetStringValue(handle, "DeviceUserID", "HikCamera");
if (MV_OK != nRet)
{
    
    
    printf("error: SetStringValue fail [%x]\n", nRet);
}

Command型参数-软触发、参数保存命令

//设置Command型节点-发送软触发命令
nRet = MV_CC_SetCommandValue(m_handle, "TriggerSoftware");
if (MV_OK != nRet)
{
    
    
	printf("error: SetCommandValue fail [%x]\n", nRet);
}

参数保存,需要用到一些组合参数

//设置Enum型参数-相机参数保存/默认加载项
//仅需在opendevice之后,初始化执行一次 UserSetSelector/UserSetDefault即可
nRet = MV_CC_SetEnumValue(handle, "UserSetSelector", 1);
if (MV_OK != nRet)
{
    
    
	printf("Set UserSetSelector 1! nRet [0x%x]\n", nRet);
}
nRet = MV_CC_SetEnumValue(handle, "UserSetDefault", 1);
if (MV_OK != nRet)
{
    
    
	printf("Set UserSetDefault 1! nRet [0x%x]\n", nRet);
}
//设置Command型节点-发送参数保存命令-参数会保存到相机内部ROM里面
//当修改参数完成后,可以调用该节点进行参数保存
//执行成功后,掉电不会消失
nRet = MV_CC_SetCommandValue(m_handle, "UserSetSave");
if (MV_OK != nRet)
{
    
    
	printf("Set UserSetSave fail [%x]\n", nRet);
}

其他的参加参数与功能,查询MVS接口
不同的功能,需要组合不同的参数,注意调用顺序与函数位置

猜你喜欢

转载自blog.csdn.net/qq_23107577/article/details/113757126