-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageAcquisition.cpp
More file actions
316 lines (200 loc) · 8.24 KB
/
Copy pathImageAcquisition.cpp
File metadata and controls
316 lines (200 loc) · 8.24 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "ImageAcquisition.h"
void saveBuffer(PvBuffer *lBuffer) {
int imageCounter = loadCounter();
PvResult lResult = PvResult::Code::INVALID_PARAMETER;
PvBufferWriter *lBufferWriter = new PvBufferWriter();
char fileName[256];
snprintf( fileName, 255, "output/img%04d.tiff", imageCounter );
lResult = lBufferWriter->Store(lBuffer, fileName, PvBufferFormatTIFF, NULL);
if ( lResult.IsOK() ) {
imageCounter++;
writeCounter(imageCounter);
}
delete lBufferWriter;
}
PvDevice *ConnectToDevice( const PvString &aConnectionID )
{
PvDevice *lDevice;
PvResult lResult;
// Connect to the GigE Vision or USB3 Vision device
lDevice = PvDevice::CreateAndConnect( aConnectionID, &lResult );
return lDevice;
}
PvStream *OpenStream( const PvString &aConnectionID )
{
PvStream *lStream;
PvResult lResult;
// Open stream to the GigE Vision or USB3 Vision device
lStream = PvStream::CreateAndOpen( aConnectionID, &lResult );
return lStream;
}
void ConfigureStream( PvDevice *aDevice, PvStream *aStream )
{
// If this is a GigE Vision device, configure GigE Vision specific streaming parameters
PvDeviceGEV* lDeviceGEV = dynamic_cast<PvDeviceGEV *>( aDevice );
if ( lDeviceGEV != NULL )
{
PvStreamGEV *lStreamGEV = static_cast<PvStreamGEV *>( aStream );
// Negotiate packet size
lDeviceGEV->NegotiatePacketSize();
// Configure device streaming destination
lDeviceGEV->SetStreamDestination( lStreamGEV->GetLocalIPAddress(), lStreamGEV->GetLocalPort() );
}
}
PvPipeline *CreatePipeline( PvDevice *aDevice, PvStream *aStream )
{
// Create the PvPipeline object
PvPipeline* lPipeline = new PvPipeline( aStream );
if ( lPipeline != NULL )
{
// Reading payload size from device
uint32_t lSize = aDevice->GetPayloadSize();
// Set the Buffer count and the Buffer size
lPipeline->SetBufferCount( BUFFER_COUNT );
lPipeline->SetBufferSize( lSize );
}
return lPipeline;
}
void waitForBufferReady(MyPipelineEventSink *lMyPipelineEventSink)
{
while (!lMyPipelineEventSink->isReady) {
usleep(1);
}
}
int AcquireImages( PvDevice *aDevice, PvStream *aStream, PvPipeline *aPipeline, MyPipelineEventSink *lMyPipelineEventSink, uint8_t *imageBuffer, int &imageSizeW, int &imageSizeH)
{
int errorState = 0;
// Get device parameters need to control streaming
PvGenParameterArray *lDeviceParams = aDevice->GetParameters();
/*
for (int i=0; i<lDeviceParams->GetCount(); i++) {
PvGenParameter *par = lDeviceParams->Get(i);
printf("%s\n",par->GetName().GetAscii());
}
*/
PvResult res = lDeviceParams->SetEnumValue("AcquisitionMode", "SingleFrame");
res = lDeviceParams->SetIntegerValue("AcquisitionFrameCount", 1);
//printf("%s\n",res.GetDescription().GetAscii());
PvGenParameter *par = lDeviceParams->Get("AcquisitionFrameToSkip");
//printf("%s\n",par->ToString().GetAscii());
// Map the GenICam AcquisitionStart and AcquisitionStop commands
PvGenCommand *aStart = dynamic_cast<PvGenCommand *>( lDeviceParams->Get( "AcquisitionStart" ) );
PvGenCommand *aStop = dynamic_cast<PvGenCommand *>( lDeviceParams->Get( "AcquisitionStop" ) );
// Note: the pipeline must be initialized before we start acquisition
aPipeline->Start();
// Enable streaming and send the AcquisitionStart command
aDevice->StreamEnable();
// start aqcuisition
aStart->Execute();
waitForBufferReady(lMyPipelineEventSink);
PvBuffer *lBuffer = NULL;
PvResult lOperationResult;
// Retrieve next buffer
PvResult lResult = aPipeline->RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult );
if ( lResult.IsOK() )
{
if ( lOperationResult.IsOK() )
{
PvPayloadType lType;
//
// We now have a valid buffer. This is where you would typically process the buffer.
// -----------------------------------------------------------------------------------------
// ...
// If the buffer contains an image, display width and height.
uint32_t lWidth = 0, lHeight = 0;
lType = lBuffer->GetPayloadType();
if ( lType == PvPayloadTypeImage )
{
// Get image specific buffer interface.
PvImage *lImage = lBuffer->GetImage();
// Read width, height.
lWidth = lImage->GetWidth();
lHeight = lImage->GetHeight();
imageSizeW = lWidth;
imageSizeH = lHeight;
memcpy(imageBuffer, lBuffer->GetDataPointer(), imageSizeW*imageSizeH);
//saveBuffer(lBuffer);
} else {
errorState = 1;
//logError("IMG_ACQ","Buffer does not contain image");
}
}
else
{
errorState = 1;
//logError("IMG_ACQ","Buffer Retrieved ERROR! (Operation)");
}
// Release the buffer back to the pipeline
aPipeline->ReleaseBuffer( lBuffer );
}
else
{
errorState = 1;
//logError("IMG_ACQ","Buffer Retrieved ERROR! (result)");
}
// Tell the device to stop sending images.
aStop->Execute();
// Disable streaming on the device
aDevice->StreamDisable();
// Stop the pipeline
aPipeline->Stop();
return errorState;
}
int setNbreadworeset(PvDeviceSerialPort *aPort, int N) {
int errorState = 0;
char mainCmdBuffer[RX_BUFFER_SIZE];
sprintf(mainCmdBuffer, "set nbreadworeset %d\n",N);
errorState += SendCameraCommand(aPort, mainCmdBuffer);
errorState += ReceiveCameraResult(aPort, mainCmdBuffer);
printf("%s",mainCmdBuffer);
return errorState;
}
void incrementPointsConsidered(uint8_t *pointsConsidered, float *aquiredImage, int maxADU, int fluxImageW, int fluxImageH) {
int i;
for (i=0;i<fluxImageW*fluxImageH;i++) {
if (aquiredImage[i]<maxADU) {
pointsConsidered[i]++;
}
}
}
// TODO: put saturation parameter
// TODO: assemble "used points whitout saturation" image
// OBS2: points considered is a uint8 points, so max of 256 points possible
int AcquireFluxImage(PvDevice *aDevice, PvStream *aStream, PvPipeline *aPipeline, MyPipelineEventSink *lMyPipelineEventSink, int fluxImageW, int fluxImageH, int N, int maxADU, float *fluxImage, float *timeStamps, float *averages, uint8_t *pointsConsidered) {
int i;
int errorState = 0;
uint8_t *mainImageBuffer;
int bufferImageW, bufferImageH;
float *sumY, *sumXY, *sumY2, *Y;
float *sumX, *sumX2, X;
long timeNow, nextTime;
mainImageBuffer = new uint8_t[fluxImageW*fluxImageH*2];
Y = new float[fluxImageW*fluxImageH];
sumY = new float[fluxImageW*fluxImageH];
sumXY = new float[fluxImageW*fluxImageH];
sumX = new float[fluxImageW*fluxImageH];
sumX2 = new float[fluxImageW*fluxImageH];
zeros(sumY, fluxImageW, fluxImageH);
zeros(sumXY, fluxImageW, fluxImageH);
zeros(sumX, fluxImageW, fluxImageH);
zeros(sumX2, fluxImageW, fluxImageH);
timeNow = getMicrotime();
timeStamps[0] = timeNow;
for (i=0;i<N;i++) {
errorState += AcquireImages( aDevice, aStream, aPipeline, lMyPipelineEventSink, mainImageBuffer, bufferImageW, bufferImageH);
nextTime = getMicrotime();
X = (nextTime - timeNow)/1.0e6;
timeStamps[i] = X;
buffer2float(mainImageBuffer, Y, fluxImageW, fluxImageH);
incrementPointsConsidered(pointsConsidered, Y, maxADU, fluxImageW, fluxImageH);
sumProducts(sumY, Y, 1.0, maxADU, fluxImageW, fluxImageH);
sumProducts(sumXY, Y , X, maxADU, fluxImageW, fluxImageH);
sumProductsX(sumX, (float)X, 1.0, Y, maxADU, fluxImageW, fluxImageH);
sumProductsX(sumX2, (float)X, (float)X, Y, maxADU, fluxImageW, fluxImageH);
float average = computeMeanOfFrame(Y, fluxImageW, fluxImageH);
averages[i] = average;
printf("%f, %f; \n",average, X);
}
computeFlux(sumY, sumXY, sumX, sumX2, pointsConsidered, fluxImageW, fluxImageH, fluxImage);
return errorState;
}