Blame view

stim/image/image.h 5.86 KB
d32a1854   David Mayerich   added framework f...
1
2
  #ifndef STIM_IMAGE_H
  #define STIM_IMAGE_H
1cb3eb53   David Mayerich   modified image.h ...
3
4
5
  #ifdef JPEG_FOUND
  	#define cimg_use_jpeg         //necessary for JPG files
  #endif
fbf0ab02   David Mayerich   added support for...
6
7
  #include "CImg.h"
  
912d9073   David Mayerich   added vector.h to...
8
  #include <vector>
d32a1854   David Mayerich   added framework f...
9
  #include <iostream>
6d30a707   Tianshu Cheng   add cuda/array_ad...
10
  
7b3948ab   David Mayerich   added support for...
11
  namespace stim{
d32a1854   David Mayerich   added framework f...
12
13
14
15
16
17
18
19
  //This static class provides the STIM interface for loading images
  //	Use this interface for all image management - that way the actual library can be changed without problems
  
  //currently this interface uses CImg
  //	T = data type (usually unsigned char)
  template <class T>
  class image{
  
7b3948ab   David Mayerich   added support for...
20
  	cimg_library::CImg<T> img;
d32a1854   David Mayerich   added framework f...
21
22
23
  
  public:
  
7b3948ab   David Mayerich   added support for...
24
25
26
27
28
29
30
31
32
  	//default constructor
  	image(){
  	}
  
  	//constructor (load an image file)
  	image(std::string filename){
  		img.load(filename.c_str());
  	}
  
41acaf5d   David Mayerich   added a CUDA Gaus...
33
  	/// Constructor initializes an image to a given size
f186dbda   Tianshu Cheng   header file for b...
34
  	/*image(unsigned int x, unsigned int y = 1, unsigned int z = 1){
41acaf5d   David Mayerich   added a CUDA Gaus...
35
  		img = cimg_library::CImg<T>(x, y, z);
f186dbda   Tianshu Cheng   header file for b...
36
37
  	}*/
  
9d3ba0b1   David Mayerich   added stim::hsi a...
38
  	image(size_t x, size_t y = 1, size_t z = 1, size_t c = 1){
f186dbda   Tianshu Cheng   header file for b...
39
  		img = cimg_library::CImg<T>(x, y, z, c);
41acaf5d   David Mayerich   added a CUDA Gaus...
40
41
  	}
  
d32a1854   David Mayerich   added framework f...
42
43
  	//Load an image from a file
  	void load(std::string filename){
7b3948ab   David Mayerich   added support for...
44
  		img.load(filename.c_str());
d32a1854   David Mayerich   added framework f...
45
46
47
48
  	}
  
  	//save a file
  	void save(std::string filename){
7b3948ab   David Mayerich   added support for...
49
  		img.save(filename.c_str());
d32a1854   David Mayerich   added framework f...
50
51
  	}
  
8b7be670   David Mayerich   implemented savin...
52
  	//create an image from an interleaved buffer
9d3ba0b1   David Mayerich   added stim::hsi a...
53
  	void set_interleaved(T* buffer, size_t width, size_t height, size_t channels = 1){
fbf0ab02   David Mayerich   added support for...
54
  
84ca9bba   Laila Saadatifard   fix some bugs in ...
55
  		T* non_interleaved = (T*)malloc(width * height * 3 * sizeof(T));
9d3ba0b1   David Mayerich   added stim::hsi a...
56
  		size_t S = width * height;
fbf0ab02   David Mayerich   added support for...
57
  
9d3ba0b1   David Mayerich   added stim::hsi a...
58
59
  		for(size_t i = 0; i < S; i++){
  			for(size_t c = 0; c < channels; c++){
fbf0ab02   David Mayerich   added support for...
60
61
62
  				non_interleaved[i + c * S] = buffer[i * channels + c];
  			}
  		}
d32a1854   David Mayerich   added framework f...
63
  
84ca9bba   Laila Saadatifard   fix some bugs in ...
64
  		img = cimg_library::CImg<T>(non_interleaved, width, height, 1, channels);
7b3948ab   David Mayerich   added support for...
65
66
  	}
  
32504c2c   David Mayerich   fixed some functi...
67
  	//fills an allocated region of memory with non-interleaved data
8b7be670   David Mayerich   implemented savin...
68
69
70
71
72
73
  	void data_noninterleaved(T* data){
  		memcpy(data, img.data(), sizeof(T) * size());
  	}
  
  	void data_interleaved(T* data){
  
9d3ba0b1   David Mayerich   added stim::hsi a...
74
75
  		size_t C = channels();
  		size_t X = width() * height();
8b7be670   David Mayerich   implemented savin...
76
77
78
79
  
  		T* ptr = img.data();
  
  		//for each channel
9d3ba0b1   David Mayerich   added stim::hsi a...
80
  		for(size_t c = 0; c < C; c++)
8b7be670   David Mayerich   implemented savin...
81
  			//convert each pixel
9d3ba0b1   David Mayerich   added stim::hsi a...
82
  			for(size_t x = 0; x < X; x++)
8b7be670   David Mayerich   implemented savin...
83
84
85
  				data[x * C + c] = ptr[c * X + x];
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
86
  	image<T> channel(size_t c){
41acaf5d   David Mayerich   added a CUDA Gaus...
87
88
89
90
  
  		//create a new image
  		image<T> single;
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
91
  		single.img = img.get_channel(c);
41acaf5d   David Mayerich   added a CUDA Gaus...
92
93
94
95
96
  
  		return single;
  
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
97
  	T& operator()(size_t x, size_t y, size_t z = 0, size_t c = 0){
3d0b6243   David Mayerich   fixed hsiproc bug...
98
99
100
101
102
103
104
105
  		return img(x, y, z, c);
  	}
  
  	/// Set all elements in the image to a given scalar value
  
  	/// @param v is the value used to set all values in the image
  	image<T> operator=(T v){
  
9d3ba0b1   David Mayerich   added stim::hsi a...
106
  		size_t X = width() * height() * depth() * channels();
3d0b6243   David Mayerich   fixed hsiproc bug...
107
  
9d3ba0b1   David Mayerich   added stim::hsi a...
108
  		for(size_t x = 0; x < X; x++)
3d0b6243   David Mayerich   fixed hsiproc bug...
109
110
111
112
113
114
  			img.data()[x] = v;
  
  		return *this;
  
  	}
  
f186dbda   Tianshu Cheng   header file for b...
115
116
117
118
119
  	/// Copy the given data to the specified channel
  
  	/// @param c is the channel number that the data will be copied to
  	/// @param buffer is a pointer to the image to be copied to channel c
  
9d3ba0b1   David Mayerich   added stim::hsi a...
120
  	void set_channel(size_t c, T* buffer){
f186dbda   Tianshu Cheng   header file for b...
121
122
  
  		//calculate the number of pixels in a channel
9d3ba0b1   David Mayerich   added stim::hsi a...
123
  		size_t channel_size = width() * height();        
f186dbda   Tianshu Cheng   header file for b...
124
125
126
127
128
129
130
131
  
  		//retreive a pointer to the raw image data
  		T* ptr = img.data() + channel_size * c;
  
  		//copy the buffer to the specified channel
  		memcpy(ptr, buffer, sizeof(T) * channel_size);
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
132
  	image<T> getslice(size_t c){
6d30a707   Tianshu Cheng   add cuda/array_ad...
133
134
135
136
137
138
139
140
141
142
  
  		//create a new image
  		image<T> slice;
  
  		slice.img = img.get_slice(c);
  
  		return slice;
  
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
143
144
  	size_t channels(){
  		return (size_t)img.spectrum();
8b7be670   David Mayerich   implemented savin...
145
146
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
147
  	size_t width(){
8b7be670   David Mayerich   implemented savin...
148
149
150
  		return img.width();
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
151
  	size_t height(){
8b7be670   David Mayerich   implemented savin...
152
  		return img.height();
7b3948ab   David Mayerich   added support for...
153
  	}
d32a1854   David Mayerich   added framework f...
154
  
9d3ba0b1   David Mayerich   added stim::hsi a...
155
  	size_t depth(){
3d0b6243   David Mayerich   fixed hsiproc bug...
156
157
158
  		return img.depth();
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
159
  	T* data(){
945ee13c   Laila Saadatifard   the get_list func...
160
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
161
162
163
  		return img.data();
  	}
  
7b3948ab   David Mayerich   added support for...
164
  	//returns the size (number of values) of the image
9d3ba0b1   David Mayerich   added stim::hsi a...
165
  	size_t size(){
7b3948ab   David Mayerich   added support for...
166
  		return img.size();
d32a1854   David Mayerich   added framework f...
167
168
  	}
  
faef7718   David Mayerich   updates to stim::...
169
  	/// Returns the number of nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
170
  	size_t nnz(){
faef7718   David Mayerich   updates to stim::...
171
  
9d3ba0b1   David Mayerich   added stim::hsi a...
172
173
  		size_t P = width() * height();
  		size_t C = channels();
faef7718   David Mayerich   updates to stim::...
174
175
176
  
  		T* ptr = img.data();
  
9d3ba0b1   David Mayerich   added stim::hsi a...
177
  		size_t n = 0;
faef7718   David Mayerich   updates to stim::...
178
  
9d3ba0b1   David Mayerich   added stim::hsi a...
179
180
  		for(size_t p = 0; p < P; p++){
  			for(size_t c = 0; c < C; c++){
faef7718   David Mayerich   updates to stim::...
181
182
183
184
185
186
187
188
189
190
191
192
193
  
  				if(ptr[c * P + p] > 0){
  					n++;
  					break;
  				}
  			}
  		}
  
  		return n;	//return the number of nonzero pixels
  
  	}
  
  	//this function returns indices of pixels that have nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
194
  	std::vector<size_t> sparse_idx(){
faef7718   David Mayerich   updates to stim::...
195
  
9d3ba0b1   David Mayerich   added stim::hsi a...
196
  		std::vector<size_t> s;		//allocate an array
faef7718   David Mayerich   updates to stim::...
197
198
  		s.resize(nnz());					//allocate space in the array
  
9d3ba0b1   David Mayerich   added stim::hsi a...
199
200
  		size_t P = width() * height();
  		size_t C = channels();
faef7718   David Mayerich   updates to stim::...
201
202
203
  
  		T* ptr = img.data();				//get a pointer to the image data
  
9d3ba0b1   David Mayerich   added stim::hsi a...
204
205
206
  		size_t i = 0;
  		for(size_t p = 0; p < P; p++){
  			for(size_t c = 0; c < C; c++){
faef7718   David Mayerich   updates to stim::...
207
208
209
210
211
212
213
214
215
216
217
  
  				if(ptr[c * P + p] > 0){
  					s[i] = p;
  					i++;
  					break;
  				}
  			}
  		}
  
  		return s;			//return the index list
  	}
6d30a707   Tianshu Cheng   add cuda/array_ad...
218
  
d32a1854   David Mayerich   added framework f...
219
  	
6d30a707   Tianshu Cheng   add cuda/array_ad...
220
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
221
  	T maxv(){
6d30a707   Tianshu Cheng   add cuda/array_ad...
222
  		float max = 0;
9d3ba0b1   David Mayerich   added stim::hsi a...
223
  		size_t N = width() * height();		//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
224
  
9d3ba0b1   David Mayerich   added stim::hsi a...
225
  		for (size_t i=0; i<N; i++){
6d30a707   Tianshu Cheng   add cuda/array_ad...
226
227
228
229
230
231
232
233
234
  
  			if (img.data()[i] > max)
  			{
  				max = img.data()[i];
  			}	
  		}
  
  		return max;	
  	}
d32a1854   David Mayerich   added framework f...
235
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
236
  	/// Returns the minimum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
237
  	T minv(){
6d30a707   Tianshu Cheng   add cuda/array_ad...
238
  		float min = 0;
9d3ba0b1   David Mayerich   added stim::hsi a...
239
  		size_t N = width() * height();		//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
240
  
9d3ba0b1   David Mayerich   added stim::hsi a...
241
  		for (size_t i=0; i<N; i++){
6d30a707   Tianshu Cheng   add cuda/array_ad...
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
  
  			if (img.data()[i] < min)
  			{
  				min = img.data()[i];
  			}	
  		}
  
  		return min;	
  
  	}
  
  	image<T> srgb2lab(){
  
  		image<T> rgb;
  		rgb.img = img.get_sRGBtoRGB();
  
  		image<T> lab;
  		lab.img = rgb.img.get_RGBtoLab();
  
  		return lab;
  
  	}
  
  	image<T> convolve2(image<T> mask){
  		
  		image<T> result;
  
  		result.img = img.get_convolve(mask.img);
  
  		return result;
  	}
d32a1854   David Mayerich   added framework f...
273
274
  
  
f186dbda   Tianshu Cheng   header file for b...
275
276
277
278
  	image<T> rotate(float angle, float cx, float cy){
  		
  		image<T> result;
  		float zoom = 1;
9d3ba0b1   David Mayerich   added stim::hsi a...
279
280
  		size_t interpolation = 1;
  		size_t boundary = 1;
f186dbda   Tianshu Cheng   header file for b...
281
282
283
284
285
  		result.img = img.get_rotate (angle, cx, cy, zoom, interpolation, boundary);
  		//result.save("data_output/test_rotate_neum.bmp");
  
  		return result;
  	}
945ee13c   Laila Saadatifard   the get_list func...
286
287
288
  
  	// leila's code for non_interleaving data in 3D
  	//create an data set from an interleaved buffer
9d3ba0b1   David Mayerich   added stim::hsi a...
289
  	void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
945ee13c   Laila Saadatifard   the get_list func...
290
291
  
  		T* non_interleaved3 = (T*)malloc(width * height * depth * 3 * sizeof(T));
9d3ba0b1   David Mayerich   added stim::hsi a...
292
  		size_t p = width * height * depth;
945ee13c   Laila Saadatifard   the get_list func...
293
  
9d3ba0b1   David Mayerich   added stim::hsi a...
294
295
  		for(size_t i = 0; i < p; i++){
  			for(size_t c = 0; c < channels; c++){
945ee13c   Laila Saadatifard   the get_list func...
296
297
298
299
300
301
  				non_interleaved3[i + c * p] = buffer[i * channels + c];
  			}
  		}
  
  		img = cimg_library::CImg<T>(non_interleaved3, width, height, depth, channels);
  	}
f186dbda   Tianshu Cheng   header file for b...
302
  	
d32a1854   David Mayerich   added framework f...
303
304
  };
  
7b3948ab   David Mayerich   added support for...
305
306
  };		//end namespace stim
  
d32a1854   David Mayerich   added framework f...
307
308
  
  #endif