Commit c5da629063f64c2f67ca26e8a87758c2d582997a
Merge branch 'master' of git.stim.ee.uh.edu:codebase/stimlib
Showing
3 changed files
with
92 additions
and
21 deletions
Show diff stats
stim/gl/gl_spider.h
... | ... | @@ -140,7 +140,7 @@ class gl_spider : public virtual gl_texture<T> |
140 | 140 | setMatrix(); //create the transformation matrix. |
141 | 141 | glCallList(dList+1); //move the templates to p, d, m. |
142 | 142 | int best = getCost(ptexbufferID, numSamplesPos); //find min cost. |
143 | - std::cerr << best << std::endl; | |
143 | +// std::cerr << best << std::endl; | |
144 | 144 | stim::vec<float> next( //find next position. |
145 | 145 | pV[best][0], |
146 | 146 | pV[best][1], | ... | ... |
stim/grids/image_stack.h
1 | 1 | #ifndef STIM_IMAGE_STACK_H |
2 | 2 | #define STIM_IMAGE_STACK_H |
3 | 3 | |
4 | -#include "../parser/wildcards.h" | |
5 | -#include "../parser/filename.h" | |
6 | -#include "../grids/grid.h" | |
7 | -#include "../image/image.h" | |
4 | +#include <stim/parser/wildcards.h> | |
5 | +#include <stim/parser/filename.h> | |
6 | +#include <stim/grids/grid.h> | |
7 | +#include <stim/image/image.h> | |
8 | 8 | |
9 | 9 | namespace stim{ |
10 | 10 | |
... | ... | @@ -48,7 +48,7 @@ public: |
48 | 48 | } |
49 | 49 | |
50 | 50 | //load the first image and set all of the image_stack properties |
51 | - std::cout<<"File to Load: "<<file_list[0].str()<<std::endl; | |
51 | +// std::cout<<"File to Load: "<<file_list[0].str()<<std::endl; | |
52 | 52 | stim::image<T> I(file_list[0].str()); |
53 | 53 | |
54 | 54 | //set the image resolution and number of channels |
... | ... | @@ -63,7 +63,7 @@ public: |
63 | 63 | //load and copy each image into the grid |
64 | 64 | for(unsigned int i = 0; i<R[3]; i++){ |
65 | 65 | |
66 | - std::cout<<"File to Load: "<<file_list[i].str()<<std::endl; | |
66 | +// std::cout<<"File to Load: "<<file_list[i].str()<<std::endl; | |
67 | 67 | //load the image |
68 | 68 | stim::image<T> I(file_list[i].str()); |
69 | 69 | |
... | ... | @@ -73,8 +73,15 @@ public: |
73 | 73 | } |
74 | 74 | } |
75 | 75 | |
76 | - ///Saves a single page to an image file | |
76 | + ///Inserts image I into slot i. | |
77 | + /// @param stim::image<T> I; image to insert. | |
78 | + /// @int I, where to place the image. | |
79 | + void insert_image(stim::image<T> I, int i) | |
80 | + { | |
81 | + I.get_interleaved_rgb(&ptr[i *R[0] *R[1] *R[2] ]); | |
82 | + } | |
77 | 83 | |
84 | + ///Saves a single page to an image file | |
78 | 85 | /// @param file_name is the name of the image file to be created |
79 | 86 | /// @param i is the page to be saved |
80 | 87 | void save_image(std::string file_name, unsigned int i){ |
... | ... | @@ -83,10 +90,11 @@ public: |
83 | 90 | stim::image<T> I; |
84 | 91 | |
85 | 92 | //retrieve the interlaced data from the image - store it in the grid |
86 | - I.set_interleaved(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]); | |
93 | + I.set_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]); | |
87 | 94 | |
88 | 95 | I.save(file_name); |
89 | 96 | } |
97 | + | |
90 | 98 | ///Sets the dimensions of the image in each direction |
91 | 99 | ///Voxel-size. |
92 | 100 | /// @param x size in the x direction |
... | ... | @@ -100,8 +108,24 @@ public: |
100 | 108 | S[2] = y; |
101 | 109 | S[3] = z; |
102 | 110 | } |
103 | - ///Saves the entire stack to a set of images | |
104 | 111 | |
112 | + ///set dimensions of the grid. | |
113 | + /// @param channels, number of channels in each image. | |
114 | + /// @param width, number of pixels in width each image. | |
115 | + /// @param height, number of pixels in height. | |
116 | + /// @param depth, number of pixels in depth. | |
117 | + void init(int channels, int width, int height, int depth) | |
118 | + { | |
119 | + R.resize(4); | |
120 | + R[0] = channels; | |
121 | + R[1] = width; | |
122 | + R[2] = height; | |
123 | + R[3] = depth; | |
124 | + | |
125 | + ptr = (T*)malloc(sizeof(T) * samples()); | |
126 | + } | |
127 | + | |
128 | + ///Saves the entire stack to a set of images | |
105 | 129 | /// @param file_mask is the mask describing how the file names will be saved (ex. image????.bmp) |
106 | 130 | void save_images(std::string file_mask){ |
107 | 131 | ... | ... |
stim/image/image.h
... | ... | @@ -24,6 +24,8 @@ class image{ |
24 | 24 | size_t Y() const { return R[2]; } |
25 | 25 | size_t C() const { return R[0]; } |
26 | 26 | |
27 | + size_t bytes(){ return size() * sizeof(T); } | |
28 | + | |
27 | 29 | void init(){ //initializes all variables, assumes no memory is allocated |
28 | 30 | memset(R, 0, sizeof(size_t) * 3); //set the resolution and number of channels to zero |
29 | 31 | img = NULL; |
... | ... | @@ -31,6 +33,8 @@ class image{ |
31 | 33 | |
32 | 34 | void unalloc(){ //frees any resources associated with the image |
33 | 35 | if(img) free(img); //if memory has been allocated, free it |
36 | + img=NULL; | |
37 | + | |
34 | 38 | } |
35 | 39 | |
36 | 40 | |
... | ... | @@ -40,15 +44,17 @@ class image{ |
40 | 44 | } |
41 | 45 | |
42 | 46 | void allocate(){ |
43 | - img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] ); //allocate memory | |
47 | + unalloc(); | |
48 | + img = (T*) malloc( bytes() ); //allocate memory | |
49 | + memset(img, 0, bytes()); | |
44 | 50 | } |
45 | 51 | |
46 | 52 | void allocate(size_t x, size_t y, size_t c){ //allocate memory based on the resolution |
53 | + unalloc(); | |
47 | 54 | R[0] = c; R[1] = x; R[2] = y; //set the resolution |
48 | 55 | allocate(); //allocate memory |
49 | 56 | } |
50 | 57 | |
51 | - size_t bytes(){ return size() * sizeof(T); } | |
52 | 58 | |
53 | 59 | size_t idx(size_t x, size_t y, size_t c = 0){ |
54 | 60 | return y * C() * X() + x * C() + c; |
... | ... | @@ -90,24 +96,27 @@ public: |
90 | 96 | |
91 | 97 | /// Constructor with a filename - loads the specified file |
92 | 98 | image(std::string filename){ //constructor initialize the image with an image file |
99 | + init(); | |
93 | 100 | load(filename); |
94 | 101 | } |
95 | 102 | |
96 | 103 | /// Create a new image from scratch given a number of samples and channels |
97 | 104 | image(size_t x, size_t y = 1, size_t c = 1){ |
105 | + init(); | |
98 | 106 | allocate(x, y, c); |
99 | 107 | } |
100 | 108 | |
101 | 109 | /// Create a new image with the data given in 'data' |
102 | 110 | image(T* data, size_t x, size_t y, size_t c = 1){ |
111 | + init(); | |
103 | 112 | allocate(x, y, c); |
104 | 113 | memcpy(img, data, bytes()); |
105 | 114 | } |
106 | 115 | |
107 | 116 | /// Copy constructor - duplicates an image object |
108 | 117 | image(const stim::image<T>& I){ |
118 | + init(); | |
109 | 119 | allocate(I.X(), I.Y(), I.C()); |
110 | - //allocate(I.R[1], I.R[2], I.R[0]); | |
111 | 120 | memcpy(img, I.img, bytes()); |
112 | 121 | } |
113 | 122 | |
... | ... | @@ -133,11 +142,18 @@ public: |
133 | 142 | exit(1); |
134 | 143 | } |
135 | 144 | allocate(cvImage.cols, cvImage.rows, cvImage.channels()); //allocate space for the image |
136 | - T* cv_ptr = (T*)cvImage.data; | |
137 | - if(C() == 1) //if this is a single-color image, just copy the data | |
138 | - memcpy(img, cv_ptr, bytes()); | |
139 | - if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving | |
145 | + T* cv_ptr = (T*) cvImage.data; | |
146 | + if(C() == 1) | |
147 | + { | |
148 | + //if this is a single-color image, just copy the data | |
149 | + memcpy(img, cv_ptr, bytes()); | |
150 | + } | |
151 | + if(C() == 3) | |
152 | + { //if this is a 3-color image, OpenCV uses BGR interleaving | |
140 | 153 | set_interleaved_bgr(cv_ptr, X(), Y()); |
154 | + } | |
155 | + | |
156 | + cvImage.release(); | |
141 | 157 | } |
142 | 158 | |
143 | 159 | //save a file |
... | ... | @@ -151,16 +167,18 @@ public: |
151 | 167 | get_interleaved_bgr(buffer); |
152 | 168 | cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer); |
153 | 169 | cv::imwrite(filename, cvImage); |
170 | + cvImage.release(); | |
171 | + free(buffer); | |
154 | 172 | } |
155 | 173 | |
156 | 174 | //create an image from an interleaved buffer |
157 | - void set_interleaved_rgb(T* buffer, size_t width, size_t height){ | |
158 | - allocate(width, height, 3); | |
175 | + void set_interleaved_rgb(T* buffer, size_t width, size_t height, size_t channels = 3){ | |
176 | + allocate(width, height, channels); | |
159 | 177 | memcpy(img, buffer, bytes()); |
160 | 178 | } |
161 | 179 | |
162 | - void set_interleaved_bgr(T* buffer, size_t width, size_t height){ | |
163 | - allocate(width, height, 3); | |
180 | + void set_interleaved_bgr(T* buffer, size_t width, size_t height, size_t channels = 3){ | |
181 | + allocate(width, height, channels); | |
164 | 182 | for(size_t c = 0; c < C(); c++){ //copy directly |
165 | 183 | for(size_t y = 0; y < Y(); y++){ |
166 | 184 | for(size_t x = 0; x < X(); x++){ |
... | ... | @@ -340,6 +358,34 @@ public: |
340 | 358 | |
341 | 359 | return r; //return the inverted image |
342 | 360 | } |
361 | + | |
362 | + /// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value | |
363 | + image<T> invert(){ | |
364 | + size_t N = size(); //calculate the total number of values in the image | |
365 | + image<T> r(X(), Y(), C()); //allocate space for the resulting image | |
366 | + T white_val = maxv(); | |
367 | + for(size_t n = 0; n < N; n++) | |
368 | + r.img[n] = white_val - img[n]; //perform the inversion | |
369 | + | |
370 | + return r; //return the inverted image | |
371 | + } | |
372 | + | |
373 | + ///crops the image from x1 to x0 and y1 to y0 and returns a new (smaller) image. | |
374 | + image<T> crop(int x0, int x1, int y0, int y1) | |
375 | + { | |
376 | + | |
377 | + image<T> ret(x1-x0, y1-y0, C()); | |
378 | + int newWidth = x1-x0; | |
379 | + int destidx, srcidx; | |
380 | + ///for each row, cut what amount of data from the original and put it into the new copy. | |
381 | + for(int i = 0; i < (y1-y0); i++) | |
382 | + { | |
383 | + destidx = i*newWidth*C(); ///destination index one per each row | |
384 | + srcidx = ((i+(y0))*X()+x0)*C(); ///source index, one per each row. | |
385 | + memcpy(&ret.img[destidx], &img[srcidx], sizeof(T)*newWidth*C()); | |
386 | + } | |
387 | + return ret; | |
388 | + } | |
343 | 389 | |
344 | 390 | image<T> srgb2lab(){ |
345 | 391 | std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl; |
... | ... | @@ -358,6 +404,7 @@ public: |
358 | 404 | exit(1); |
359 | 405 | } |
360 | 406 | |
407 | + | |
361 | 408 | // leila's code for non_interleaving data in 3D |
362 | 409 | //create an data set from an interleaved buffer |
363 | 410 | void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){ | ... | ... |