Commit bb29dc49fb1bc39e0ef2ef2721143c76a9665852

Authored by David Mayerich
2 parents 96e8ab1f 618dce30

Merge branch 'master' of git.stim.ee.uh.edu:codebase/stimlib

matlab/images2ENVI.m 0 → 100644
  1 +function images2ENVI( image_struct, filename )
  2 +%HSIWRITE : This function generates a hyperspectral binary image and header
  3 +% based on multi-dimention image
  4 +%
  5 +%
  6 +% This function accepts a srutcture which contains several channels.
  7 +% It creates a 'bsq' interleaved image and a ENVI header filer correspoing
  8 +% to the information of the image
  9 +
  10 +fields = fieldnames(image_struct);
  11 +
  12 +% check all images to have the same size
  13 +for i = 1 : size (fields, 1)
  14 + for j = 1 : size (fields, 1)
  15 + if size(image_struct.(fields{i}))~= size(image_struct.(fields{i}))
  16 + fprintf ('Dimension of input images do not Match !');
  17 + exit;
  18 + end
  19 + end
  20 +end
  21 +
  22 +
  23 +
  24 +
  25 +
  26 +% Extracting the needed information
  27 +base_image = image_struct.(fields{1});
  28 +
  29 +rows = size(base_image, 1);
  30 +columns = size(base_image, 2);
  31 +bands = size(fields,1);
  32 +multiDimImage = base_image'; % Matlab stores column major, transposing make it row major to be saved on memory
  33 +
  34 +% Create multi-dimentional image
  35 +for j = 1 : size(fields,1)
  36 + multiDimImage(:,:,j) = (image_struct.(fields{j})'); % Matlab stores column major, transposing make it row major to be saved on memory
  37 +end
  38 +
  39 +% create the binary image
  40 +HSImage = reshape (multiDimImage, [rows*columns*bands, 1]);
  41 +if (~isfloat(HSImage))
  42 + HSImage_float = single(HSImage); % casting to float
  43 +end
  44 +multibandwrite(HSImage_float,filename,'bsq');
  45 +
  46 +
  47 +% create the header file
  48 +info.samples = columns;
  49 +info.lines = rows;
  50 +info.bands = bands;
  51 +info.header_offset = 0;
  52 +info.file_type = 'ENVI Standard';
  53 +info.data_type = 4;
  54 +info.interleave = 'bsq';
  55 +
  56 +
  57 +
  58 +
  59 +fid = fopen(strcat(filename, '.hdr'),'w'); % open a file to write the header file
  60 +fprintf(fid,'%s\n','ENVI'); % first line of the header file showing it is an ENVI file
  61 +
  62 +info_fields=fieldnames(info);
  63 +for i = 1:length(info_fields)
  64 + parameter = info_fields{i}; % get the parameter of each line
  65 + value = info.(info_fields{i}); % get the corresponding value of the parameter
  66 + parameter(strfind(parameter,'_')) = ' '; % change '_' to ' '
  67 + line=[parameter,' = ',num2str(value)]; % create the line of the hdr file
  68 + fprintf(fid,'%s\n',line); % write it to the hdr file
  69 +end
  70 +
  71 +fclose(fid); % clode the header file
  72 +
  73 +end
  74 +
stim/envi/binary.h
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 #include <unistd.h> 15 #include <unistd.h>
16 #endif 16 #endif
17 17
18 -#ifdef CUDA_FOUND 18 +#ifdef USE_CUDA
19 //CUDA externs 19 //CUDA externs
20 void gpu_permute(char* dest, char* src, size_t sx, size_t sy, size_t sz, size_t d0, size_t d1, size_t d2, size_t typesize); 20 void gpu_permute(char* dest, char* src, size_t sx, size_t sy, size_t sz, size_t d0, size_t d1, size_t d2, size_t typesize);
21 #include <stim/cuda/cudatools/error.h> 21 #include <stim/cuda/cudatools/error.h>
@@ -647,7 +647,7 @@ public: @@ -647,7 +647,7 @@ public:
647 std::chrono::high_resolution_clock::time_point t0, t1; 647 std::chrono::high_resolution_clock::time_point t0, t1;
648 t0 = std::chrono::high_resolution_clock::now(); 648 t0 = std::chrono::high_resolution_clock::now();
649 649
650 -#ifdef CUDA_FOUND 650 +#ifdef USE_CUDA
651 T* gpu_src; 651 T* gpu_src;
652 HANDLE_ERROR( cudaMalloc(&gpu_src, sx*sy*sz*sizeof(T)) ); 652 HANDLE_ERROR( cudaMalloc(&gpu_src, sx*sy*sz*sizeof(T)) );
653 HANDLE_ERROR( cudaMemcpy(gpu_src, src, sx*sy*sz*sizeof(T), cudaMemcpyHostToDevice) ); 653 HANDLE_ERROR( cudaMemcpy(gpu_src, src, sx*sy*sz*sizeof(T), cudaMemcpyHostToDevice) );
@@ -359,11 +359,23 @@ public: @@ -359,11 +359,23 @@ public:
359 359
360 fseek(f, 9, SEEK_SET); //seek to the number of bands 360 fseek(f, 9, SEEK_SET); //seek to the number of bands
361 short b; //allocate space for the number of bands 361 short b; //allocate space for the number of bands
362 - fread(&b, sizeof(short), 1, f); //read the number of bands 362 + size_t nread = fread(&b, sizeof(short), 1, f); //read the number of bands
  363 + if(nread != 1){
  364 + std::cout<<"Error reading band number from Agilent file."<<std::endl;
  365 + exit(1);
  366 + }
363 fseek(f, 13, SEEK_CUR); //skip the the x and y dimensions 367 fseek(f, 13, SEEK_CUR); //skip the the x and y dimensions
364 short x, y; 368 short x, y;
365 - fread(&x, sizeof(short), 1, f); //read the image x and y size  
366 - fread(&y, sizeof(short), 1, f); 369 + nread = fread(&x, sizeof(short), 1, f); //read the image x and y size
  370 + if(nread != 1){
  371 + std::cout<<"Error reading X dimension from Agilent file."<<std::endl;
  372 + exit(1);
  373 + }
  374 + nread = fread(&y, sizeof(short), 1, f);
  375 + if(nread != 1){
  376 + std::cout<<"Error reading Y dimension from Agilent file."<<std::endl;
  377 + exit(1);
  378 + }
367 fclose(f); //close the file 379 fclose(f); //close the file
368 380
369 //store the information from the Agilent header in the ENVI header 381 //store the information from the Agilent header in the ENVI header
stim/image/image.h
@@ -141,7 +141,7 @@ public: @@ -141,7 +141,7 @@ public:
141 141
142 //save a Netpbm file 142 //save a Netpbm file
143 void load_netpbm(std::string filename) { 143 void load_netpbm(std::string filename) {
144 - std::ifstream infile(filename, std::ios::in | std::ios::binary); //open an output file 144 + std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary); //open an output file
145 if (!infile) { 145 if (!infile) {
146 std::cout << "Error opening input file in image::load_netpbm()" << std::endl; 146 std::cout << "Error opening input file in image::load_netpbm()" << std::endl;
147 exit(1); 147 exit(1);
@@ -244,7 +244,7 @@ public: @@ -244,7 +244,7 @@ public:
244 244
245 //save a Netpbm file 245 //save a Netpbm file
246 void save_netpbm(std::string filename) { 246 void save_netpbm(std::string filename) {
247 - std::ofstream outfile(filename, std::ios::out | std::ios::binary); //open an output file 247 + std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary); //open an output file
248 if(!outfile) { 248 if(!outfile) {
249 std::cout << "Error generating output file in image::save_netpbm()" << std::endl; 249 std::cout << "Error generating output file in image::save_netpbm()" << std::endl;
250 exit(1); 250 exit(1);
@@ -356,8 +356,8 @@ public: @@ -356,8 +356,8 @@ public:
356 } 356 }
357 357
358 /// Returns an std::vector containing each channel as a separate image 358 /// Returns an std::vector containing each channel as a separate image
359 - std::vector<image<T>> split() const {  
360 - std::vector<image<T>> r; //create an image array 359 + std::vector< image<T> > split() const {
  360 + std::vector< image<T> > r; //create an image array
361 r.resize(C()); //create images for each channel 361 r.resize(C()); //create images for each channel
362 362
363 for (size_t c = 0; c < C(); c++) { //for each channel 363 for (size_t c = 0; c < C(); c++) { //for each channel
@@ -367,7 +367,7 @@ public: @@ -367,7 +367,7 @@ public:
367 } 367 }
368 368
369 /// Merge a series of single-channel images into a multi-channel image 369 /// Merge a series of single-channel images into a multi-channel image
370 - void merge(std::vector<image<T>>& list) { 370 + void merge(std::vector< image<T> >& list) {
371 size_t x = list[0].width(); //calculate the size of the image 371 size_t x = list[0].width(); //calculate the size of the image
372 size_t y = list[0].height(); 372 size_t y = list[0].height();
373 allocate(x, y, list.size()); //re-allocate the image 373 allocate(x, y, list.size()); //re-allocate the image
stim/math/filters/conv2.h
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 namespace stim { 10 namespace stim {
11 #ifdef __CUDACC__ 11 #ifdef __CUDACC__
12 //Kernel function that performs the 2D convolution. 12 //Kernel function that performs the 2D convolution.
13 - template<typename T, typename K = T> 13 + template<typename T, typename K>
14 __global__ void kernel_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) { 14 __global__ void kernel_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) {
15 extern __shared__ T s[]; //declare a shared memory array 15 extern __shared__ T s[]; //declare a shared memory array
16 size_t xi = blockIdx.x * blockDim.x + threadIdx.x; //threads correspond to indices into the output image 16 size_t xi = blockIdx.x * blockDim.x + threadIdx.x; //threads correspond to indices into the output image
@@ -52,7 +52,7 @@ namespace stim { @@ -52,7 +52,7 @@ namespace stim {
52 //@param sy is the size of the input image along Y 52 //@param sy is the size of the input image along Y
53 //@param kx is the size of the kernel along X 53 //@param kx is the size of the kernel along X
54 //@param ky is the size of the kernel along Y 54 //@param ky is the size of the kernel along Y
55 - template<typename T, typename K = T> 55 + template<typename T, typename K>
56 void gpu_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) { 56 void gpu_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) {
57 cudaDeviceProp p; 57 cudaDeviceProp p;
58 HANDLE_ERROR(cudaGetDeviceProperties(&p, 0)); 58 HANDLE_ERROR(cudaGetDeviceProperties(&p, 0));
@@ -77,7 +77,7 @@ namespace stim { @@ -77,7 +77,7 @@ namespace stim {
77 //@param sy is the size of the input image along Y 77 //@param sy is the size of the input image along Y
78 //@param kx is the size of the kernel along X 78 //@param kx is the size of the kernel along X
79 //@param ky is the size of the kernel along Y 79 //@param ky is the size of the kernel along Y
80 - template<typename T, typename K = T> 80 + template<typename T, typename K>
81 void cpu_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) { 81 void cpu_conv2(T* out, T* in, K* kernel, size_t sx, size_t sy, size_t kx, size_t ky) {
82 size_t X = sx - kx + 1; //x size of the output image 82 size_t X = sx - kx + 1; //x size of the output image
83 size_t Y = sy - ky + 1; //y size of the output image 83 size_t Y = sy - ky + 1; //y size of the output image
stim/math/filters/gauss2.h
@@ -16,7 +16,7 @@ namespace stim { @@ -16,7 +16,7 @@ namespace stim {
16 ///@param stdx is the standard deviation (in pixels) along the x axis 16 ///@param stdx is the standard deviation (in pixels) along the x axis
17 ///@param stdy is the standard deviation (in pixels) along the y axis 17 ///@param stdy is the standard deviation (in pixels) along the y axis
18 ///@param nstds specifies the number of standard deviations of the Gaussian that will be kept in the kernel 18 ///@param nstds specifies the number of standard deviations of the Gaussian that will be kept in the kernel
19 - template<typename T, typename K = T> 19 + template<typename T, typename K>
20 stim::image<T> cpu_gauss2(const stim::image<T>& in, K stdx, K stdy, size_t nstds = 3) { 20 stim::image<T> cpu_gauss2(const stim::image<T>& in, K stdx, K stdy, size_t nstds = 3) {
21 size_t kx = stdx * nstds * 2; //calculate the kernel sizes 21 size_t kx = stdx * nstds * 2; //calculate the kernel sizes
22 size_t ky = stdy * nstds * 2; 22 size_t ky = stdy * nstds * 2;
@@ -33,8 +33,8 @@ namespace stim { @@ -33,8 +33,8 @@ namespace stim {
33 for (size_t yi = 0; yi < ky; yi++) 33 for (size_t yi = 0; yi < ky; yi++)
34 gy[yi] = gauss1d((K)yi, muy, stdy); 34 gy[yi] = gauss1d((K)yi, muy, stdy);
35 35
36 - std::vector<stim::image<T>> IN = in.split(); //split the input image into channels  
37 - std::vector<stim::image<T>> R = r.split(); //split the output image into channels 36 + std::vector< stim::image<T> > IN = in.split(); //split the input image into channels
  37 + std::vector< stim::image<T> > R = r.split(); //split the output image into channels
38 for (size_t c = 0; c < in.channels(); c++) //for each channel 38 for (size_t c = 0; c < in.channels(); c++) //for each channel
39 cpu_sepconv2(R[c].data(), IN[c].data(), gx, gy, IN[c].width(), IN[c].height(), kx, ky); 39 cpu_sepconv2(R[c].data(), IN[c].data(), gx, gy, IN[c].width(), IN[c].height(), kx, ky);
40 40
stim/math/filters/sepconv2.h
@@ -15,7 +15,7 @@ namespace stim { @@ -15,7 +15,7 @@ namespace stim {
15 //@param sy is the size of the input image along Y 15 //@param sy is the size of the input image along Y
16 //@param kx is the size of the kernel along X 16 //@param kx is the size of the kernel along X
17 //@param ky is the size of the kernel along Y 17 //@param ky is the size of the kernel along Y
18 - template<typename T, typename K = T> 18 + template<typename T, typename K>
19 void gpu_sepconv2(T* out, T* in, K* k0, K* k1, size_t sx, size_t sy, size_t kx, size_t ky) { 19 void gpu_sepconv2(T* out, T* in, K* k0, K* k1, size_t sx, size_t sy, size_t kx, size_t ky) {
20 cudaDeviceProp p; 20 cudaDeviceProp p;
21 HANDLE_ERROR(cudaGetDeviceProperties(&p, 0)); 21 HANDLE_ERROR(cudaGetDeviceProperties(&p, 0));
@@ -54,7 +54,7 @@ namespace stim { @@ -54,7 +54,7 @@ namespace stim {
54 //@param sy is the size of the input image along Y 54 //@param sy is the size of the input image along Y
55 //@param kx is the size of the kernel along X 55 //@param kx is the size of the kernel along X
56 //@param ky is the size of the kernel along Y 56 //@param ky is the size of the kernel along Y
57 - template<typename T, typename K = T> 57 + template<typename T, typename K>
58 void cpu_sepconv2(T* out, T* in, K* k0, K* k1, size_t sx, size_t sy, size_t kx, size_t ky) { 58 void cpu_sepconv2(T* out, T* in, K* k0, K* k1, size_t sx, size_t sy, size_t kx, size_t ky) {
59 size_t X = sx - kx + 1; //x size of the output image 59 size_t X = sx - kx + 1; //x size of the output image
60 size_t Y = sy - ky + 1; 60 size_t Y = sy - ky + 1;