-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathframegrabber.cpp
220 lines (198 loc) · 5.97 KB
/
framegrabber.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "framegrabber.h"
FrameGrabber::FrameGrabber(QObject *parent) : QObject(parent)
{
initCamera();
}
FrameGrabber::~FrameGrabber()
{
if (pylonCamera->IsOpen())
{
if (pylonCamera->IsGrabbing()) pylonCamera->StopGrabbing();
pylonCamera->Close();
cameraConnected = false;
}
delete pylonCamera;
Pylon::PylonTerminate();
}
void FrameGrabber::initCamera()
{
Pylon::PylonInitialize();
pylonCamera = new Camera_t;
cameraConnected = false;
startGrabbing = false;
grabMode = 'N';
}
void FrameGrabber::receiveConnectCamera()
{
try
{
// Attach connected camera to the pylon camera object
pylonCamera->Attach(CTlFactory::GetInstance().CreateFirstDevice());
// Open the camera, but not yet start grabbing
pylonCamera->Open();
// Warm up camera for 0.1s or 100ms
usleep(100000);
if(pylonCamera->IsOpen())
{
cameraConnected = true;
configureCamera();
}
else
{
cameraConnected = false;
qDebug() << "FAIL to Connect Camera.";
}
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "[ERROR!] An Exception Occurred in connectCamera()."
<< endl << e.GetDescription() << endl;
}
}
void FrameGrabber::configureCamera()
{
if (pylonCamera->IsGrabbing()) pylonCamera->StopGrabbing();
try
{
// Method 1:
// Load pfs file into the camera's node map with enabled validation.
//const char cameraParaFile[] = "../conf/acA2040-55uc_22095198.pfs";
const char cameraParaFile[] = "../conf/acA2440-35uc_22776933.pfs";
//CFeaturePersistence::Save(cameraParaFile, &pylonCamera->GetNodeMap());
CFeaturePersistence::Load(cameraParaFile, &pylonCamera->GetNodeMap(), true);
// Method 2:
// The parameter MaxNumBuffer can be used to control the count of buffers
// allocated for grabbing. The default value of this parameter is 10.
//pylonCamera->MaxNumBuffer = 10;
/*
pylonCamera->ExposureAuto.SetValue(ExposureAuto_Off);
pylonCamera->ExposureMode.SetValue(ExposureMode_Timed);
pylonCamera->ExposureTime.SetValue(25000); // us
pylonCamera->AcquisitionFrameRateEnable.SetValue(true);
pylonCamera->AcquisitionFrameRate.SetValue(35.0);
pylonCamera->Width.SetValue(2048);
pylonCamera->Height.SetValue(1536);
*/
//pylonCamera->CenterX.SetValue(true);
//pylonCamera->CenterY.SetValue(true);
// Warm up camera for 0.1s
usleep(100000);
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "[ERROR!] An Exception Occurred in configureCamera()."
<< endl << e.GetDescription() << endl;
}
}
void FrameGrabber::receiveDisconnectCamera()
{
if (pylonCamera->IsOpen())
{
if (pylonCamera->IsGrabbing()) pylonCamera->StopGrabbing();
pylonCamera->Close();
cameraConnected = false;
grabMode = 'N';
startGrabbing = false;
}
pylonCamera->DetachDevice();
}
void FrameGrabber::receiveStartCaptureMode()
{
grabMode = 'C';
if (!startGrabbing)
{
try
{
pylonCamera->StartGrabbing(GrabStrategy_LatestImageOnly);
startGrabbing = true;
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "[ERROR!] An Exception Occurred in startCapture()."
<< endl << e.GetDescription() << endl;
}
}
emit sendCaptureFrame(readFrame());
}
void FrameGrabber::receiveStartStreamMode()
{
grabMode = 'S';
if (!startGrabbing)
{
try
{
pylonCamera->StartGrabbing(GrabStrategy_LatestImageOnly);
startGrabbing = true;
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "[ERROR!] An Exception Occurred in startStream()."
<< endl << e.GetDescription() << endl;
}
}
}
void FrameGrabber::receiveSendFrame()
{
if (startGrabbing)
{
emit sendFrame(readFrame());
}
}
cv::Mat FrameGrabber::readFrame()
{
try
{
CPylonImage pylonFrame;
CImageFormatConverter formatConverter;
formatConverter.OutputPixelFormat = PixelType_BGR8packed;
// This smart pointer will receive the grab result data.
GrabResultPtr_t ptrGrabResult;
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
pylonCamera->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded())
{
// Convert the grabbed buffer to pylon image
formatConverter.Convert(pylonFrame, ptrGrabResult);
// Create an OpenCV image out of pylon image
cv::Mat tempFrame = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(),
CV_8UC3, (uint8_t *) pylonFrame.GetBuffer());
cvFrame = tempFrame.clone();
return cvFrame;
}
else
{
qDebug() << "Fail to Grab Frame.";
}
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "[ERROR!] An Exception Occurred in readFrame()."
<< endl << e.GetDescription() << endl;
}
}
void FrameGrabber::receiveStopGrabbing()
{
if (startGrabbing)
{
if (pylonCamera->IsGrabbing()) pylonCamera->StopGrabbing();
startGrabbing = false;
grabMode = 'N';
}
}
QString FrameGrabber::scanDevices()
{
// Get all the connected camera
Pylon::DeviceInfoList_t cameraList;
Pylon::CTlFactory::GetInstance().EnumerateDevices(cameraList, true);
QString cameraNameString = NULL;
if (cameraList.size() != 0)
{
cameraNameString = QString(cameraList[0].GetFriendlyName());
}
return cameraNameString;
}