-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforReportCode.cpp
More file actions
389 lines (325 loc) · 12.7 KB
/
forReportCode.cpp
File metadata and controls
389 lines (325 loc) · 12.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include<ueye.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<time.h>
#include <sys/time.h>
#include <arm_neon.h>
// #include <opencv2/highgui.hpp>
class HSICamera
{
public:
HSICamera();
void initialize(int pixelClockMHz, int resolution, double exposureMs, int rows, int columns, int pixelDepth);
void runCubeCapture();
void captureSingleImage();
private:
HIDS hCam = 1;
char* memSingleImage = NULL;
int memIDSingle = 0;
int sensorRows;
int sensorColumns;
int bitDepth;
int cubeColumns;
int cubeRows = 25;
const int nSingleFrames = 1735;
const int nRawImagesInMemory = 100;
double frameRate = 32.0;
int bands;
int nBandsBinned;
int factorLastBands = 0;
int nFullBinnsPerRow = 0;
int binningFactor = 20;
bool meanBinning = true;
char cubeType = 'c'; //a=bil b=bip c=bsq
char** memSingleImageSequence = new char*[nRawImagesInMemory];
char **hsiCube;
unsigned char** binnedImages;
void writeCubeToFile();
void writeSingleToFile();
void writeBandsToSeparateFiles();
void writeRawDataToFile(char** rawImages, int nRows, int nColumns);
int random_partition(unsigned char* arr, int start, int end);
int random_selection(unsigned char* arr, int start, int end, int k);
void insertionSort(unsigned char arr[], int n);
void swap(unsigned char* a, unsigned char* b);
int partition (unsigned char arr[], int low, int high);
void quickSort(unsigned char arr[], int low, int high);
};
HSICamera::HSICamera(){
is_SetErrorReport (hCam, IS_ENABLE_ERR_REP);
INT success = is_InitCamera(&hCam, NULL);
if(success!=IS_SUCCESS){
printf("Failed to initialize camera!\n");
}
}
void HSICamera::initialize(int pixelClockMHz, int resolution, double exposureMs, int rows, int columns, int pixelDepth){
printf("Initializing camera parameters\n");
sensorRows = rows;
sensorColumns = columns;
bands = columns;
nBandsBinned = bands;
bitDepth = pixelDepth;
UINT nPixelClock = pixelClockMHz;
////////////////////Binning////////////////////
factorLastBands = bands%binningFactor;
nFullBinnsPerRow = bands/binningFactor;
nBandsBinned = (bands + binningFactor - 1) / binningFactor;
if(cubeType=='a' || cubeType=='b'){
cubeColumns = sensorRows*nBandsBinned;
cubeRows = nSingleFrames;
}
else{//BSQ
cubeColumns = sensorRows;
cubeRows = nSingleFrames*nBandsBinned;
}
////////////////////Initialize///////////////////
int errorCode = is_PixelClock(hCam, IS_PIXELCLOCK_CMD_SET,
(void*)&nPixelClock,
sizeof(nPixelClock));
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the pixel clock, error code: %d\n", errorCode);
};
errorCode = is_SetDisplayMode(hCam, IS_SET_DM_DIB);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the display mode, error code: %d\n", errorCode);
};
UINT formatID = resolution;
errorCode = is_ImageFormat (hCam, IMGFRMT_CMD_SET_FORMAT, &formatID, 4);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the resolution setup, error code: %d\n", errorCode);
};
double expTime = exposureMs;
errorCode = is_Exposure(hCam, IS_EXPOSURE_CMD_SET_EXPOSURE, (void*)&expTime, sizeof(expTime));
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the exposure time, error code: %d\n", errorCode);
};
errorCode = is_SetColorMode(hCam, IS_CM_MONO8);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the color mode, error code: %d\n", errorCode);
};
errorCode = is_SetGainBoost(hCam, IS_SET_GAINBOOST_ON);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the gainboost, error code: %d\n", errorCode);
};
errorCode = is_SetHardwareGain (hCam, 50, IS_IGNORE_PARAMETER, IS_IGNORE_PARAMETER, IS_IGNORE_PARAMETER);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with the hardware gain, error code: %d\n", errorCode);
};
/////////////Set imagememory for triggermode//////////////////////
/*
is_AllocImageMem(hCam, sensorColumns, sensorRows, bitDepth, &memSingleImage, &memIDSingle);
is_SetImageMem(hCam, memSingleImage, memIDSingle);
printf("Allocated memory for single image\n");
is_SetExternalTrigger(hCam, IS_SET_TRIGGER_SOFTWARE);
*/
/////////////Set imagememory for freerun mode//////////////////////
for(int imageMemory=1; imageMemory<=nRawImagesInMemory; imageMemory++){
// printf("Adding imagememory %i\n", imageMemory);
is_AllocImageMem(hCam, sensorColumns, sensorRows, bitDepth, &memSingleImageSequence[imageMemory], &imageMemory);
is_AddToSequence (hCam, memSingleImageSequence[imageMemory], imageMemory);
}
is_InitImageQueue (hCam, 0);
binnedImages = new unsigned char*[nSingleFrames];
for(int image=0; image<nSingleFrames; image++){
binnedImages[image] = new unsigned char[nBandsBinned*sensorRows];//TODO pixeldepth
}
errorCode = is_SetFrameRate(hCam, frameRate, &frameRate);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with setting the framerate, error code: %d\n", errorCode);
};
errorCode = is_CaptureVideo (hCam, IS_WAIT);
if(errorCode!=IS_SUCCESS){
printf("Something went wrong with putting camera in freerun mode, error code: %d\n", errorCode);
};
}
void HSICamera::runCubeCapture(){
/////////////////Freerun mode //////////////
char* rawImageP;
int imageSequenceID = 1;
for(int imageNumber=0; imageNumber<nSingleFrames; imageNumber++){
is_WaitForNextImage(hCam, 1000, &(rawImageP), &imageSequenceID);
/////Binning
#pragma omp parallel for num_threads(2)
for(int row=0; row<sensorRows; row++){
int rowOffset = row*sensorColumns;
int binnedIdxOffset = row*nBandsBinned;
int binOffset = 0;
for(int binnIterator=0; binnIterator<nFullBinnsPerRow; binnIterator++){
int rowAndBinOffset = rowOffset+binOffset;
uint8x8_t vec1_16x8, vec2_16x8, vec3_16x8;
vec1_16x8 = vld1_u8((uint8_t*)(rawImageP+rowAndBinOffset));
vec2_16x8 = vld1_u8((uint8_t*)(rawImageP+rowAndBinOffset+8));
vec3_16x8 = vadd_u8(vec1_16x8, vec2_16x8);
vec1_16x8 = vld1_u8((uint8_t*)(rawImageP+16));
vec2_16x8 = vadd_u8(vec1_16x8, vec3_16x8);
uint8_t shiftTmp[8] = {vget_lane_u8(vec3_16x8, 4), vget_lane_u8(vec3_16x8, 5), vget_lane_u8(vec3_16x8, 6), vget_lane_u8(vec3_16x8, 7), 0, 0, 0, 0};
vec3_16x8 = vld1_u8(shiftTmp);
vec1_16x8 = vadd_u8(vec3_16x8, vec2_16x8);
uint16_t totPixVal = vget_lane_u8(vec1_16x8, 0) + vget_lane_u8(vec1_16x8, 1) + vget_lane_u8(vec1_16x8, 2) + vget_lane_u8(vec1_16x8, 3);
binOffset += binningFactor;
binnedImages[imageNumber][binnedIdxOffset+binnIterator] = (unsigned char)(totPixVal/binningFactor);
}
if(factorLastBands>0){
char totPixVal = 0;
for(int pixelIterator=0; pixelIterator<factorLastBands; pixelIterator++){
totPixVal = rawImageP[nFullBinnsPerRow*binningFactor+pixelIterator];
}
binnedImages[imageNumber][binnedIdxOffset+nFullBinnsPerRow] = rawImageP[nFullBinnsPerRow*binningFactor];
}
}
is_UnlockSeqBuf (hCam, 1, rawImageP);
}
for(int imageMemory=1; imageMemory<=nRawImagesInMemory; imageMemory++){
is_FreeImageMem (hCam, memSingleImageSequence[imageMemory], imageMemory);
}
hsiCube = new char*[cubeRows];
for(int cubeRow=0; cubeRow<cubeRows; cubeRow++){
hsiCube[cubeRow] = new char[cubeColumns];//TODO pixeldepth
}
///Make cube
if(cubeType=='a'){//BIL
for(int cubeRow=0; cubeRow<cubeRows; cubeRow++){
for(int band=0; band<nBandsBinned; band++){
for(int pixelInCubeRow=0; pixelInCubeRow<sensorRows; pixelInCubeRow++){
hsiCube[cubeRow][band*sensorRows+pixelInCubeRow] = binnedImages[cubeRow][nBandsBinned*(sensorRows-1)-nBandsBinned*pixelInCubeRow+band];
}
}
}
}
else if(cubeType=='b'){//BIP
for(int cubeRow=0; cubeRow<cubeRows; cubeRow++){
for(int pixel=0; pixel<sensorRows; pixel++){
for(int band=0; band<nBandsBinned; band++){
hsiCube[cubeRow][pixel*nBandsBinned+band] = binnedImages[cubeRow][nBandsBinned*(sensorRows-1)-nBandsBinned*pixel+band];
}
}
}
}
else{//BSQ
for(int band=0; band<nBandsBinned; band++){
for(int rowSpatial=0; rowSpatial<nSingleFrames; rowSpatial++){
for(int pixelInCubeRow=0; pixelInCubeRow<cubeColumns; pixelInCubeRow++){
hsiCube[band*nSingleFrames+rowSpatial][pixelInCubeRow] = binnedImages[rowSpatial][nBandsBinned*(sensorRows-1)-nBandsBinned*pixelInCubeRow+band];
}
}
}
}
/////////////////////////////////Trigger mode /////////////////////
/*
if(1){//if this is bsq
for(int cubeRow=0; cubeRow<cubeRows; cubeRow++){
is_FreezeVideo(hCam, IS_WAIT);
//TODO Binning
for(int band=0; band<bands; band++){
for(int pixelInCubeRow=0; pixelInCubeRow<sensorRows; pixelInCubeRow++){
hsiCube[cubeRow][band*sensorRows+pixelInCubeRow] = memSingleImage[sensorColumns*(sensorRows-1)-sensorColumns*pixelInCubeRow+band];
}
}
// usleep(captureInterval);
}
}
*/
writeCubeToFile();
}
void HSICamera::writeRawDataToFile(char** rawImages, int nRows, int nColumns){
struct timespec timeSystem;
clock_gettime(CLOCK_REALTIME, &timeSystem);
char timeSystemString[32];
sprintf(timeSystemString, "%lli", (long long)timeSystem.tv_nsec);
char filePath[64];
strcpy(filePath, "/home/capture/");
// strcpy(filePath, "/home/andreas/HSIProject/capture/");
strcat(filePath, timeSystemString);
strcat(filePath, "SensorData.raw");
FILE * fp;
fp = fopen (filePath,"wb");
for(int i=0; i<nRows; i++){
fwrite (rawImages[i], sizeof(char), nColumns, fp);//TODO bitDepth
}
fclose (fp);
}
void HSICamera::writeCubeToFile(){
struct timespec timeSystem;
clock_gettime(CLOCK_REALTIME, &timeSystem);
char timeSystemString[32];
sprintf(timeSystemString, "%lli", (long long)timeSystem.tv_nsec);
char filePath[64];
strcpy(filePath, "/home/root/capture/");
// strcpy(filePath, "/home/andreas/HSIProject/capture/");
strcat(filePath, timeSystemString);
strcat(filePath, "Cube.raw");
FILE * fp;
fp = fopen (filePath,"wb");
if (fp==NULL){
printf("\nFile cant be opened");
return;
}
for(int i=0; i<cubeRows; i++){
fwrite (hsiCube[i], sizeof(char), cubeColumns, fp);//TODO bitDepth
}
fclose (fp);
}
void HSICamera::captureSingleImage(){
is_FreezeVideo(hCam, IS_WAIT);
writeSingleToFile();
}
void HSICamera::writeSingleToFile(){
struct timespec timeSystem;
clock_gettime(CLOCK_REALTIME, &timeSystem);
char timeSystemString[32];
sprintf(timeSystemString, "%lli", (long long)timeSystem.tv_nsec);
char filePath[64];
strcpy(filePath, "/home/capture/");
// strcpy(filePath, "/home/andreas/HSIProject/capture/");
strcat(filePath, timeSystemString);
strcat(filePath, "Single.raw");
FILE * fp;
fp = fopen (filePath,"wb");
fwrite (memSingleImage, sizeof(char), sensorColumns*sensorRows, fp);//TODO bitDepth
fclose (fp);
}
void HSICamera::writeBandsToSeparateFiles(){
/*
auto time_now = std::time(0);
std::stringstream ss;
ss << time_now;
std::string timeString = ss.str();
std::string newDirectory = "mkdir -p ./capture/"+timeString;
system(newDirectory.c_str());
std::vector<cv::Mat> rgbChannels;
char grayScaleImage[nSingleFrames*sensorRows];
char rgbImage[3][nSingleFrames*sensorRows];
int bandIterator = 0;
if(1){ //This is bsq
for(int band=0; band<nBandsBinned; band++){
for(int cubeRow=0; cubeRow<cubeRows; cubeRow++){
for(int sensorRow=0; sensorRow<sensorRows; sensorRow++){
grayScaleImage[cubeRow*sensorRows+sensorRow] = hsiCube[cubeRow][band*sensorRows+sensorRow];
}
}
std::string filename = "./capture/" + timeString + "/" + std::to_string(band) + ".png";
cv::Mat grayScaleMat = cv::Mat(nSingleFrames, sensorRows, CV_8UC1, &grayScaleImage);
imwrite(filename,grayScaleMat);
*/
//Make RGB image
/*
if(band==292||band==734||band==1147){
for(int i=0; i<nSingleFrames*sensorRows; i++){
rgbImage[bandIterator][i] = grayScaleImage[i];
}
bandIterator++;
if(band==1147){
rgbChannels.push_back(cv::Mat(nSingleFrames, sensorRows, CV_8UC1, &rgbImage[0]));
rgbChannels.push_back(cv::Mat(nSingleFrames, sensorRows, CV_8UC1, &rgbImage[1]));
rgbChannels.push_back(cv::Mat(nSingleFrames, sensorRows, CV_8UC1, &rgbImage[2]));
cv::Mat rgbImage;
cv::merge(rgbChannels, rgbImage);
filename = "./capture/" + timeString + "/RedGreenBlue" + ".png";
imwrite(filename,rgbImage);
}
}
*/
// }
// }
}