Record a calibrated datacube (dark frame subtracted and response corrected) with a PikaL or Pika XC2 and save it to disk in a format readable by ENVI or Spectronon.
#include <iostream>
#include <fstream>
#include <exception>
#include <string>
#include <iomanip>
#include <assert.h>
#include <cstdlib>
#include "resonon_imager_basler.h"
int main() {
const int LINE_COUNT = 100;
const int DARK_COUNT = 30;
const int RESPONSE_COUNT = 25;
const std::string filename = "Pika_basler_test.bil";
std::string header_filename;
int framesize;
int cubesize;
unsigned short * frame_buffer;
double * cube_buffer;
double * dark_buffer;
double * response_buffer;
double corrected_pixel;
try {
frame_buffer = new unsigned short[framesize]();
if (frame_buffer == 0) {
throw std::exception("Error: memory could not be allocated for frame");
}
dark_buffer = new double[framesize]();
if (dark_buffer == 0) {
throw std::exception("Error: memory could not be allocated for dark cube");
}
std::cout << "\nPlace lens cap on camera to record Dark Cube\n";
std::cout << "Press Enter to begin recording\n";
std::cin.ignore();
std::cout << "\nRecording Dark Cube" << std::endl;
for (int line = 0; line < DARK_COUNT; line++) {
for (int pixel = 0; pixel < framesize; pixel++) {
dark_buffer[pixel] += double(frame_buffer[pixel]);
}
std::cout << "Line " << line + 1 << " of " << DARK_COUNT << std::endl;
}
for (int k = 0; k < framesize; k++) {
dark_buffer[k] /= DARK_COUNT;
}
std::cout << "Dark Cube recording complete" << std::endl;
response_buffer = new double[framesize]();
if (response_buffer == 0) {
throw std::exception("Error: memory could not be allocated for response cube");
}
std::cout << "\nPlace calibration panel in front of camera to record Response Cube\n";
std::cout << "Press Enter to begin recording\n";
std::cin.ignore();
std::cout << "\nRecording Response Cube" << std::endl;
for (int line = 0; line < RESPONSE_COUNT; line++) {
for (int pixel = 0; pixel < framesize; pixel++) {
response_buffer[pixel] += double(frame_buffer[pixel]);
}
std::cout << "Line " << line + 1 << " of " << RESPONSE_COUNT << std::endl;
}
for (int k = 0; k < framesize; k++) {
response_buffer[k] /= RESPONSE_COUNT;
}
std::cout << "Response Cube recording complete" << std::endl;
cubesize = framesize * LINE_COUNT;
cube_buffer = new double[cubesize]();
if (cube_buffer == 0) {
throw std::exception("Error: memory could not be allocated for datacube");
}
std::cout << "\nAim camera at target to record Datacube\n";
std::cout << "Press Enter to begin recording\n";
std::cin.ignore();
std::cout << "\nRecording Datacube" << std::endl;
for (int line = 0; line < LINE_COUNT; line++) {
for (int pixel = 0; pixel < framesize; pixel++) {
corrected_pixel = (double(frame_buffer[pixel]) - dark_buffer[pixel]) / (response_buffer[pixel] - dark_buffer[pixel] + 1e-9);
if (corrected_pixel < 0) {
corrected_pixel = 0;
}
cube_buffer[line * framesize + pixel] = corrected_pixel;
}
std::cout << "Line " << line + 1 << " of " << LINE_COUNT << std::endl;
}
std::cout << "Recording Complete\nWriting Datacube to Disk" << std::endl;
header_filename = filename + ".hdr";
std::ofstream outfile(header_filename.c_str());
outfile << "ENVI\n";
outfile << "interleave = bil\n";
outfile << "data type = 5\n";
outfile << "lines = " << LINE_COUNT << "\n";
outfile <<
"gain = " << imager.
get_gain() <<
"\n";
outfile << "wavelength = {";
outfile << std::setprecision(5);
for(int band = start; band < end - 1; band++) {
}
outfile.close();
std::ofstream cubefile;
cubefile.open(filename.c_str(), std::ios::out | std::ios::binary);
cubefile.write((const char*) cube_buffer, cubesize * sizeof(double));
cubefile.close();
std::cout << "Done." << std::endl;
delete [] frame_buffer;
delete [] dark_buffer;
delete [] response_buffer;
delete [] cube_buffer;
} catch (std::exception const & e) {
std::cerr << "Error: " << e.what() << std::endl;
if (frame_buffer != 0) {
delete [] frame_buffer;
}
if (dark_buffer != 0) {
delete [] dark_buffer;
}
if (response_buffer != 0) {
delete [] response_buffer;
}
if (cube_buffer != 0) {
delete [] cube_buffer;
}
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}