海康工業相機引數設定與獲取
- 通用介面介紹
- 常見相機引數設定獲取
- Int型引數-影像寬度、影像高度
- Enum型引數-影像格式、觸發模式設定
- float 型引數-曝光、增益設定
- string型引數-用戶名稱
- Command型引數-軟觸發、引數保存命令
通用介面介紹
拿到海康工業相機之后,通過其官方提供的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介面
不同的功能,需要組合不同的引數,注意呼叫順序與函式位置
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263364.html
標籤:其他
上一篇:藍橋杯練習——2.22
