Hikvision 산업용 카메라 OpenCV C++로 사용해보기 - 1 -
Hikvision 산업용 카메라 OpenCV C++로 사용해보기 - 2 -
5. 추가 기능 구현
Documentations 폴더의 MvCameraNode.xlsx 파일은 카메라를 제어하기 위해서 필요한 프로그래밍 정보가 정리되어 있다.
이 파일을 참고하면 카메라의 노출값이나 Gain, fps 등 세부적인 설정이 가능하다.
1) 노출값(exposure) 설정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
void SetExposureAuto(void* handle, bool isauto)
{
int nRet = MV_OK;
if (isauto) { // auto mode on
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 2);
}
else { // auto mode off
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 0);
}
if (MV_OK != nRet)
{
printf("Set OnExposureAuto fail! nRet [0x%x]\n", nRet);
}
}
void SetExposure(void* handle, float exposure)
{
int nRet = MV_OK;
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", exposure);
if (MV_OK != nRet)
{
printf("Set Exposure fail! nRet [0x%x]\n", nRet);
}
}
|
cs |
카메라 Open 상태 시 사용 가능.
노출값을 조절하기 위해서는 먼저 ExposureAuto 기능을 꺼야한다.
기능을 끄지않고 노출값을 변경하면 주변 환경에 맞추어 자동으로 값이 변할 수 있다.
MvCameraNode.xlsx 파일에서 SetExposureAuto에 해당하는 데이터 타입은 IEnumeration이다.
그러므로 MV_CC_SetEnumValue 함수를 사용해야한다.
ExposureTime을 설정해주는 데이터 타입은 IFloat이므로 MV_CC_SetFloatValue 함수 사용.
2) 프레임 속도(fps) 설정
1
2
3
4
5
6
7
8
9
10
|
void SetFramerate(void* handle, float framerate)
{
int nRet = MV_OK;
nRet = MV_CC_SetFloatValue(handle, "AcquisitionFrameRate", framerate);
if (MV_OK != nRet)
{
printf("Set FrameRate fail! nRet [0x%x]\n", nRet);
}
}
|
cs |
프레임 속도 설정도 똑같다.
AcquisitionFrameRate에 해당하는 데이터 타입은 IFloat이므로 MV_CC_SetFloatValue 함수를 사용하여 설정하자.
6. 결과 확인
카메라 제어에 필요한 기본 함수를 작성했으므로 직접 결과를 확인 해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
void main() {
void* handle;
handle = CreateCamera("MV-CA016-10UC"); // Create Handle and Open
SetExposureAuto(handle, false); // set Exposure Auto off
SetExposure(handle, 30000.0f); // set Exposure
SetFramerate(handle, 50.0f); // set Frame rate
// data type : BayerRG8
MV_CC_SetEnumValue(handle, "PixelFormat", 0x01080009);
GrabCamera(handle);
Mat img;
while (true) {
img = GetMatFrame(handle); // get frame
resize(img, img, Size(img.cols / 2, img.rows / 2)); // resize
imshow("camera", img);
char c = (char)waitKey(10);
if (c == 27) {
StopCamera(handle); // stop camera
CloseCamera(handle); // close camera
break;
}
}
}
|
cs |
GetMatFrame 함수를 통해 카메라 데이터를 Mat 타입으로 받아온다.
그 뒤로는 OpenCV 코드를 자유롭게 넣어주면 된다.
7. 전체 소스 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <stdio.h>
#include <string>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "MvCameraControl.h"
unsigned int g_nPayloadSize = 1440 * 1080; // cache size
unsigned char* pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize)); // cache address of picture data
using namespace std;
using namespace cv;
void* CreateCamera(string cameraname)
{
int nRet = MV_OK; // status
void* handle = NULL; // camera handle
// Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList; // device list
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST)); // initialize
nRet = MV_CC_EnumDevices(MV_USB_DEVICE, &stDeviceList); // get device list
if (MV_OK != nRet)
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
if (stDeviceList.nDeviceNum > 0) // exists connected device
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++) {
// get camera model name
String modelname((char*)stDeviceList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chModelName);
// compare and create handle
if (modelname == cameraname) {
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[i]);
break;
}
}
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
printf("Open Device fail! nRet [0x%x]\n", nRet);
return handle;
}else
return NULL;
}
void GrabCamera(void* handle)
{
int nRet = MV_OK;
// Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
}
}
// convert data stream in Mat format
Mat Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData)
{
Mat srcImage;
srcImage = Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8U, pData);
cvtColor(srcImage, srcImage, COLOR_BayerRG2RGB);
return srcImage;
}
Mat GetMatFrame(void* handle)
{
MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
Mat image;
MV_CC_GetOneFrameTimeout(handle, pData, g_nPayloadSize, &stImageInfo, 1000);
image = Convert2Mat(&stImageInfo, pData);
return image;
}
void StopCamera(void* handle)
{
int nRet = MV_OK;
// Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
}
}
void CloseCamera(void* handle)
{
int nRet = MV_OK;
// Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
}
// Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
}
}
void SetExposureAuto(void* handle, bool isauto)
{
int nRet = MV_OK;
if (isauto) { // auto mode on
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 2);
}
else { // auto mode off
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", 0);
}
if (MV_OK != nRet)
{
printf("Set OnExposureAuto fail! nRet [0x%x]\n", nRet);
}
}
void SetExposure(void* handle, float exposure)
{
int nRet = MV_OK;
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", exposure);
if (MV_OK != nRet)
{
printf("Set Exposure fail! nRet [0x%x]\n", nRet);
}
}
void SetFramerate(void* handle, float framerate)
{
int nRet = MV_OK;
nRet = MV_CC_SetFloatValue(handle, "AcquisitionFrameRate", framerate);
if (MV_OK != nRet)
{
printf("Set FrameRate fail! nRet [0x%x]\n", nRet);
}
}
void main() {
void* handle;
handle = CreateCamera("MV-CA016-10UC"); // Create Handle and Open
SetExposureAuto(handle, false); // set Exposure Auto off
SetExposure(handle, 30000.0f); // set Exposure
SetFramerate(handle, 50.0f); // set Frame rate
// data type : BayerRG8
MV_CC_SetEnumValue(handle, "PixelFormat", 0x01080009);
GrabCamera(handle);
Mat img;
while (true) {
img = GetMatFrame(handle); // get frame
resize(img, img, Size(img.cols / 2, img.rows / 2)); // resize
imshow("camera", img);
char c = (char)waitKey(10);
if (c == 27) {
StopCamera(handle); // stop camera
CloseCamera(handle); // close camera
break;
}
}
}
|
cs |
parkeh-dev/Control_Hikvision_Camera
https://parkeh-dev.tistory.com/2. Contribute to parkeh-dev/Control_Hikvision_Camera development by creating an account on GitHub.
github.com
'개발 > Computer Vision' 카테고리의 다른 글
Hikvision 산업용 카메라 OpenCV C++로 사용해보기 - 2 - (0) | 2020.01.14 |
---|---|
Hikvision 산업용 카메라 OpenCV C++로 사용해보기 - 1 - (0) | 2020.01.14 |