- 前言
- 相机属性树
- 1.设备控制/DeviceControl
- 1.1相机序列号获取/DeviceSerialNumber
- 1.2相机自定义命名设置与获取/DeviceUserID
- 1.3相机上电时间获取/DeviceUptime
- 2.相机格式控制/ImageFormatControl
- 2.1获取相机图像最大宽、高值
- 2.2设置相机图像ROI区域
- 2.3恢复相机图像ROI区域,恢复最大值
- 2.4获取相机支持的图像格式
- 2.5修改相机图像格式
- 2.6打开/关闭相机自带测试图像
- 2.6.1关闭测试图像(默认状态)
- 2.6.2打开测试图像
- 2.7打开/关闭相机图像X镜像、Y镜像
- 2.8打开/关闭相机嵌入式图像水印功能
- 3.采集控制/AcquisitionControl
- 3.1采集模式设置
- 3.2多帧采集设置/AcquisitionBurstframeCount
- 3.3帧率控制/AcquisitionframeRate
- 3.4触发模式设置
- 3.4.1软触发设置/Software
- 3.4.2硬触发设置/line
- 3.4.3任意触发设置/Anyway
- DigitalIOControl
- ActionControl
- FileAccessControl
- EventControl
- ChunkDataControl
- TransportLayerControl
- UserSetControl
本文简单直接粗糙的介绍下海康工业相机常用的参数的设置方法与获取方法
1.海康工业SDK简介:海康机器人工业相机sdk简介
2.海康工业相机参数设置获取通用方法:海康工业相机参数设置与获取
如果不心急,就先简单阅读上述两篇博客,可以更加熟悉海康的工业相机sdk的整体构成
参数实在是太多,有空慢慢更新啦…
MVCC_STRINGVALUE stStrValue;
memset(&stStrValue, 0, sizeof(MVCC_STRINGVALUE));
nRet = MV_CC_GetStringValue(handle, "DeviceSerialNumber", &stStrValue);
if (MV_OK != nRet)
{
printf("Get DeviceSerialNumber fail! nRet [0x%x]n", nRet);
}
printf("Current DeviceSerialNumber [%s]n", stStrValue.chCurValue);
结果:
nRet = MV_CC_SetStringValue(handle, "DeviceUserID", "UserIDChanged");
if (MV_OK != nRet)
{
printf("Set DeviceUserID fail! nRet [0x%x]n", nRet);
}
MVCC_STRINGVALUE stStrValue;
memset(&stStrValue, 0, sizeof(MVCC_STRINGVALUE));
nRet = MV_CC_GetStringValue(handle, "DeviceUserID", &stStrValue);
if (MV_OK != nRet)
{
printf("Get DeviceUserID fail! nRet [0x%x]n", nRet);
}
printf("Rename DeviceUserID [%s]n", stStrValue.chCurValue);
结果:
MVCC_INTVALUE_EX stIntValue = {0};
nRet = MV_CC_GetIntValueEx(handle, "DeviceUptime", &stIntValue);
if (MV_OK != nRet)
{
printf("Get DeviceUptime fail! nRet [0x%x]n", nRet);
}
printf("DeviceUptime [%d]n", stIntValue.nCurValue);
结果:
MVCC_INTVALUE_EX nWidthMaxValue= = {0},nHeightMaxValue = {0};
//如需获取当前相机图像宽高,需要将WidthMax替换成Width
nRet = MV_CC_GetIntValueEx(handle, "WidthMax", &nWidthMaxValue);
if (MV_OK != nRet)
{
printf("Get WidthMax fail! nRet [0x%x]n", nRet);
}
//如需获取当前相机图像宽高,需要将HeightMax替换成Height
nRet = MV_CC_GetIntValueEx(handle, "HeightMax", &nHeightMaxValue);
if (MV_OK != nRet)
{
printf("Get HeightMax fail! nRet [0x%x]n", nRet);
}
2.2设置相机图像ROI区域
注意事项:相机设置图像宽高时,相机不能取流:需要在startgrabing之前设置或者stopgrabing之后设置
unsigned int m_Width=1000,m_Height=1000;//要设置的步进值
//offset,设置的偏移值,图像左上角为原点,一般需要是2的倍数,不能是偶数
unsigned int m_OffsetX=256,m_OffsetY=256;
//获取图像宽高最大最小值,获取步进值
MVCC_INTVALUE_EX nWidthValue= {0},nHeightValue = {0};
//获取相机支持的宽高最大值
nRet = MV_CC_GetIntValueEx(handle, "Width", &nWidthValue);
nRet = MV_CC_GetIntValueEx(handle, "Height", &nHeightValue);
if(m_Width>nWidthValue.nMax||
m_Height > nHeightValue.nMax||
m_WidthnWidthValue.nMax|| //偏移值+设置值必须小于宽高的最大值
(m_Height +m_OffsetY)>nHeightValue.nMax||)
{
//判断有没有超出范围,超出范围就报错!!!
printf("Set Image Width or Height out of range !!n", nRet);
}else
{
//避免offset有偏移值,导致后续设置宽高失败
nRet = MV_CC_SetIntValue(handle,"OffsetX",0);
nRet = MV_CC_SetIntValue(handle,"OffsetY",0);
if((m_Width%nWidthValue.nInc)==0&&(m_Height%nHeightValue.nInc)==0)//符合步进值才设置进去
{
nRet = MV_CC_SetIntValue(handle,"Width",m_Width);
if(nRet != MV_OK)
{
printf("Warning: Set Width fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"Height",m_Height);
if(nRet != MV_OK)
{
printf("Warning: Set Height fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"OffsetX",m_OffsetX);
if(nRet != MV_OK)
{
printf("Warning: Set OffsetX fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"OffsetY",m_OffsetY);
if(nRet != MV_OK)
{
printf("Warning: Set OffsetY fail nRet [0x%x]!", nRet);
}
}else
{
//不符合步进,减去余数再去设置宽高,此时设置的宽高与原宽高有差异
nRet = MV_CC_SetIntValue(handle,"Width",m_Width-m_Width%nWidthValue.nInc);
if(nRet != MV_OK)
{
printf("Warning: Set Width fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"Height",m_Height-m_Height%nHeightValue.nInc);
if(nRet != MV_OK)
{
printf("Warning: Set Height fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"OffsetX",m_OffsetX);
if(nRet != MV_OK)
{
printf("Warning: Set OffsetX fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"OffsetY",m_OffsetY);
if(nRet != MV_OK)
{
printf("Warning: Set OffsetY fail nRet [0x%x]!", nRet);
}
}
}
2.3恢复相机图像ROI区域,恢复最大值
MVCC_INTVALUE_EX nWidthMaxValue= {0},nHeightMaxValue = {0};
nRet = MV_CC_GetIntValueEx(handle, "WidthMax", &nWidthMaxValue);
if (MV_OK != nRet)
{
printf("Get WidthMax fail! nRet [0x%x]n", nRet);
}
nRet = MV_CC_GetIntValueEx(handle, "HeightMax", &nHeightMaxValue);
if (MV_OK != nRet)
{
printf("Get HeightMax fail! nRet [0x%x]n", nRet);
}
//一定要先还原相机的偏移值,再去设置相机宽高
nRet = MV_CC_SetIntValue(handle,"OffsetX",0);
nRet = MV_CC_SetIntValue(handle,"OffsetY",0);
nRet = MV_CC_SetIntValue(handle,"Width",nWidthMaxValue.nCurValue);//设置为最大值
if(nRet != MV_OK)
{
printf("Warning: Set Width fail nRet [0x%x]!", nRet);
}
nRet = MV_CC_SetIntValue(handle,"Height",nHeightMaxValue.nCurValue);//设置为最大值
if(nRet != MV_OK)
{
printf("Warning: Set Height fail nRet [0x%x]!", nRet);
}
2.4获取相机支持的图像格式
MV_XML_NODE_FEATURE stNodeFeature;
MV_XML_FEATURE_Enumeration stFeatureEnm;
memset(&stNodeFeature,0,sizeof(MV_XML_NODE_FEATURE));
memset(&stFeatureEnm, 0, sizeof(MV_XML_FEATURE_Enumeration));
strncpy(stNodeFeature.strName, "PixelFormat", strlen("PixelFormat"));
stNodeFeature.enType = IFT_IEnumeration;
nRet = MV_XML_GetNodeFeature(handle,&stNodeFeature,&stFeatureEnm);
if (MV_OK != nRet)
{
return nRet;
}else
{
for (int i = 0; i < stFeatureEnm.nSymbolicNum; i++)
{
printf("PixelFormat:%s rn", stFeatureEnm.strSymbolic[i]);//打印出相机支持的所有像素格式
}
for (int i = 0; i < stFeatureEnm.nSymbolicNum; i++)
{
if (strcmp(stFeatureEnm.strSymbolic[i] ,"BayerBG8")==0||
strcmp(stFeatureEnm.strSymbolic[i], "BayerRG8")==0||
strcmp(stFeatureEnm.strSymbolic[i], "BayerGB8")==0||
strcmp(stFeatureEnm.strSymbolic[i], "BayerGR8")==0||
strcmp(stFeatureEnm.strSymbolic[i], "RGB8Packed")==0||
strcmp(stFeatureEnm.strSymbolic[i], "BGR8Packed")==0)//比较相机参数格式,这里仅比较了bayer与RGB
{
printf("This is a Color camera n");
break;
}
}
}
结果:
//设置Enum型参数-相机图像格式
//注意点1:相机图像格式设置时,只有在MV_CC_Startgrab接口调用前才能设置,取流过程中,不能修改图像格式
nRet = MV_CC_SetEnumValue(handle, "PixelFormat", PixelType_Gvsp_Mono12);
if (MV_OK != nRet)
{
printf("error: Set PixelFormat fail [%x]n", nRet);
}
2.6打开/关闭相机自带测试图像
2.6.1关闭测试图像(默认状态)
nRet = MV_CC_SetEnumValue(handle, "TestPattern", 0);
if (MV_OK != nRet)
{
printf("Set TestPattern fail! nRet [0x%x]n", nRet);
break;
}
2.6.2打开测试图像
nRet = MV_CC_SetEnumValue(handle, "TestPattern", 9);
//0:off,9:ColorBar,14:ObliqueMonoBar,17:TestImage1
if (MV_OK != nRet)
{
printf("Set TestPattern fail! nRet [0x%x]n", nRet);
break;
}
效果:
nRet = MV_CC_SetBoolValue(handle, "ReverseX", true);
if (MV_OK != nRet)
{
printf("Set ReverseX fail! nRet [0x%x]n", nRet);
}
//Y镜像需要注意两点
//1.部分相机不支持,所以可能设置不了,具体看相机规格书spec
//2.Y镜像需要停止采集状态下设置,不像X镜像一样可以实时设置,一般在MV_CC_StartGrabbing接口之前调用
nRet = MV_CC_SetBoolValue(handle, "ReverseY", true);
if (MV_OK != nRet)
{
printf("Set ReverseY fail! nRet [0x%x]n", nRet);
}
效果:
相机的图像水印信息,用于打印记录相机内部信息,水印信息在取流接口中,图像结构体里面获取
相机水印的本质原理是取代图像头部开始的小部分有效信息,占用约40个字节的真实数据信息,供信息传输
| 参数 | 关键词 | 参数值 |
|---|---|---|
| 时间戳 | Timestamp | 0 |
| 增益 | Gain | 1 |
| 曝光 | Exposure | 2 |
| 平均亮度 | BrightnessInfo | 3 |
| 白平衡 | WhiteBalance | 4 |
| 帧号 | framecounter | 5 |
| 触发计数 | ExtTriggerCount | 6 |
| IO输入输出电平状态 | LineInputOutput | 7 |
| ROI区域 | ROIPosition | 8 |
//必须停止采集状态下设置,一般在MV_CC_StartGrabbing接口之前调用
//开启相机图像水印,如下,打开了Exposure、BrightnessInfo、framecounter
nRet = MV_CC_SetEnumValue(handle, "frameSpecInfoSelector", 2);//Exposure
nRet = MV_CC_SetBoolValue(handle, "frameSpecInfo", true);
nRet = MV_CC_SetEnumValue(handle, "frameSpecInfoSelector", 3);//BrightnessInfo
nRet = MV_CC_SetBoolValue(handle, "frameSpecInfo", true);
nRet = MV_CC_SetEnumValue(handle, "frameSpecInfoSelector", 5);//framecounter
nRet = MV_CC_SetBoolValue(handle, "frameSpecInfo", true);
if (MV_OK != nRet)
{
printf("Set frameSpecInfofail! nRet [0x%x]n", nRet);
}
效果:
主动取流中,图像结构体中获取水印信息
关闭水印功能
//必须停止采集状态下设置,一般在MV_CC_StartGrabbing接口之前调用 nRet = MV_CC_SetEnumValue(handle, "frameSpecInfoSelector", 5);//framecounter nRet = MV_CC_SetBoolValue(handle, "frameSpecInfo", false);3.采集控制/AcquisitionControl 3.1采集模式设置
| 参数 | 关键词 | 参数值 | 说明 |
|---|---|---|---|
| 单帧采集 | Singleframe | 0 | 一般用于visionpro等特殊软件,正常不推荐使用这个模式 |
| 连续采集 | Continuous | 2 | 常用的模式,软触发的单帧采集,硬触发采集,连续拍照,都使用这个模式 |
nRet = MV_CC_SetEnumValue(handle, "AcquisitionMode", 2);
if (MV_OK != nRet)
{
printf("Set frameSpecInfofail! nRet [0x%x]n", nRet);
}
3.2多帧采集设置/AcquisitionBurstframeCount
AcquisitionBurstframeCount:在触发模式下,触发一次,采集多少帧,默认为1;
假设设置为2,相机收到一个信号,自动采集两帧,采集频率根据帧率变化
nRet = MV_CC_SetIntValue(handle,"AcquisitionBurstframeCount",1);
if(nRet != MV_OK)
{
printf("Warning: Set AcquisitionBurstframeCountfail nRet [0x%x]!", nRet);
}
3.3帧率控制/AcquisitionframeRate
//设置相机帧率,需注意不要超过相机支持的最大的帧率(相机规格书),超过了也没有意义
nRet =MV_CC_SetFloatValue(handle, "AcquisitionframeRate", 5);
if(nRet != MV_OK)
{
printf("Warning: Set AcquisitionBurstframeCountfail nRet [0x%x]!", nRet);
}
//帧率控制使能,true表示打开,false标识关闭
nRet = MV_CC_SetBoolValue(handle, "AcquisitionframeRateEnable", true);
if(nRet != MV_OK)
{
printf("Warning: Set AcquisitionBurstframeCountfail nRet [0x%x]!", nRet);
}
3.4触发模式设置
3.4.1软触发设置/Software
// ch:设置触发模式为on | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_ON);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]n", nRet);
}
// ch:设置触发源为软触发 | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerSource",MV_TRIGGER_SOURCE_SOFTWARE);//TriggerSource:7
if (MV_OK != nRet)
{
printf("Set Trigger Source fail! nRet [0x%x]n", nRet);
}
在需要拍摄图片的时刻,执行command命令,执行一次,相机拍摄一张图片(需在startgrabing命令开启后才行)
nRet = MV_CC_SetCommandValue(handle, "TriggerSoftware");//相机拍照
if (MV_OK != nRet)
{
printf("TriggerSoftware fail! nRet [0x%x]n", nRet);
}
3.4.2硬触发设置/line
3.4.3任意触发设置/Anyway
DigitalIOControl
ActionControl
FileAccessControl
EventControl
ChunkDataControl
TransportLayerControl
UserSetControl


