Blame view

stim/image/image.h 14.5 KB
d32a1854   David Mayerich   added framework f...
1
2
  #ifndef STIM_IMAGE_H
  #define STIM_IMAGE_H
fbf0ab02   David Mayerich   added support for...
3
  
c8c976a9   David Mayerich   replaced CImg wit...
4
5
  #include <opencv2/core/core.hpp>
  #include <opencv2/highgui/highgui.hpp>
912d9073   David Mayerich   added vector.h to...
6
  #include <vector>
d32a1854   David Mayerich   added framework f...
7
  #include <iostream>
a2bf1d08   David Mayerich   general bug fixes...
8
  #include <limits>
e252a6d6   David Mayerich   somehow the C++11...
9
  #include <typeinfo>
cfcf8619   David Mayerich   changes to filena...
10
  #include <fstream>
6d30a707   Tianshu Cheng   add cuda/array_ad...
11
  
7b3948ab   David Mayerich   added support for...
12
  namespace stim{
c8c976a9   David Mayerich   replaced CImg wit...
13
14
  /// This static class provides the STIM interface for loading, saving, and storing 2D images.
  /// Data is stored in an interleaved (BIP) format (default for saving and loading is RGB).
d32a1854   David Mayerich   added framework f...
15
16
17
18
19
20
  
  //currently this interface uses CImg
  //	T = data type (usually unsigned char)
  template <class T>
  class image{
  
c8c976a9   David Mayerich   replaced CImg wit...
21
  	//cimg_library::CImg<T> img;
cfcf8619   David Mayerich   changes to filena...
22
  	T* img;										//pointer to the image data (interleaved RGB for color)
c8c976a9   David Mayerich   replaced CImg wit...
23
24
  	size_t R[3];
  
a2bf1d08   David Mayerich   general bug fixes...
25
26
27
  	size_t X() const { return R[1]; }
  	size_t Y() const { return R[2]; }
  	size_t C() const { return R[0]; }
c8c976a9   David Mayerich   replaced CImg wit...
28
  
c8c976a9   David Mayerich   replaced CImg wit...
29
30
31
32
33
34
35
  	void init(){								//initializes all variables, assumes no memory is allocated
  		memset(R, 0, sizeof(size_t) * 3);		//set the resolution and number of channels to zero
  		img = NULL;
  	}
  
  	void unalloc(){								//frees any resources associated with the image
  		if(img)	free(img);						//if memory has been allocated, free it
c8c976a9   David Mayerich   replaced CImg wit...
36
  	}
1a224b6a   David Mayerich   fixed linux compa...
37
  
c8c976a9   David Mayerich   replaced CImg wit...
38
39
40
41
42
43
44
  
  	void clear(){								//clears all image data
  		unalloc();								//unallocate previous memory
  		init();									//re-initialize the variables
  	}
  
  	void allocate(){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
45
  		unalloc();
814eb271   David Mayerich   fixed problems wi...
46
  		img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] );	//allocate memory
c8c976a9   David Mayerich   replaced CImg wit...
47
48
49
  	}
  
  	void allocate(size_t x, size_t y, size_t c){	//allocate memory based on the resolution
c8c976a9   David Mayerich   replaced CImg wit...
50
51
52
53
  		R[0] = c; R[1] = x; R[2] = y;				//set the resolution
  		allocate();									//allocate memory
  	}
  
814eb271   David Mayerich   fixed problems wi...
54
  	size_t bytes(){ return size() * sizeof(T); }
c8c976a9   David Mayerich   replaced CImg wit...
55
56
57
58
  
  	size_t idx(size_t x, size_t y, size_t c = 0){
  		return y * C() * X() + x * C() + c;
  	}
1a224b6a   David Mayerich   fixed linux compa...
59
  
c8c976a9   David Mayerich   replaced CImg wit...
60
61
  
  	int cv_type(){
e252a6d6   David Mayerich   somehow the C++11...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  		// The following is C++ 11 code, but causes problems on some compilers (ex. nvcc). Below is my best approximation to a solution
  
  		//if(std::is_same<T, unsigned char>::value)	return CV_MAKETYPE(CV_8U, (int)C());
  		//if(std::is_same<T, char>::value)			return CV_MAKETYPE(CV_8S, (int)C());
  		//if(std::is_same<T, unsigned short>::value)	return CV_MAKETYPE(CV_16U, (int)C());
  		//if(std::is_same<T, short>::value)			return CV_MAKETYPE(CV_16S, (int)C());
  		//if(std::is_same<T, int>::value)				return CV_MAKETYPE(CV_32S, (int)C());
  		//if(std::is_same<T, float>::value)			return CV_MAKETYPE(CV_32F, (int)C());
  		//if(std::is_same<T, double>::value)			return CV_MAKETYPE(CV_64F, (int)C());
  
  		if(typeid(T) == typeid(unsigned char))		return CV_MAKETYPE(CV_8U, (int)C());
  		if(typeid(T) == typeid(char))				return CV_MAKETYPE(CV_8S, (int)C());
  		if(typeid(T) == typeid(unsigned short))		return CV_MAKETYPE(CV_16U, (int)C());
  		if(typeid(T) == typeid(short))				return CV_MAKETYPE(CV_16S, (int)C());
  		if(typeid(T) == typeid(int))				return CV_MAKETYPE(CV_32S, (int)C());
  		if(typeid(T) == typeid(float))				return CV_MAKETYPE(CV_32F, (int)C());
  		if(typeid(T) == typeid(double))				return CV_MAKETYPE(CV_64F, (int)C());
1a224b6a   David Mayerich   fixed linux compa...
79
  
c8c976a9   David Mayerich   replaced CImg wit...
80
  		std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
3ff136b3   David Mayerich   replaced C++11 co...
81
  		exit(1);
c8c976a9   David Mayerich   replaced CImg wit...
82
83
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
84
85
  	/// Returns the value for "white" based on the dynamic range (assumes white is 1.0 for floating point images)
  	T white(){
e252a6d6   David Mayerich   somehow the C++11...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  		// The following is C++ 11 code, but causes problems on some compilers (ex. nvcc). Below is my best approximation to a solution
  
  		//if(std::is_same<T, unsigned char>::value)		return UCHAR_MAX;
  		//if(std::is_same<T, unsigned short>::value)		return SHRT_MAX;
  		//if(std::is_same<T, unsigned>::value)			return UINT_MAX;
  		//if(std::is_same<T, unsigned long>::value)		return ULONG_MAX;
  		//if(std::is_same<T, unsigned long long>::value)	return ULLONG_MAX;
  		//if(std::is_same<T, float>::value)				return 1.0f;
  		//if(std::is_same<T, double>::value)				return 1.0;
  
  		if(typeid(T) == typeid(unsigned char))		return UCHAR_MAX;
  		if(typeid(T) == typeid(unsigned short))		return SHRT_MAX;
  		if(typeid(T) == typeid(unsigned))			return UINT_MAX;
  		if(typeid(T) == typeid(unsigned long))		return ULONG_MAX;
  		if(typeid(T) == typeid(unsigned long long))	return ULLONG_MAX;
  		if(typeid(T) == typeid(float))				return 1.0f;
  		if(typeid(T) == typeid(double))				return 1.0;
a2bf1d08   David Mayerich   general bug fixes...
103
104
  
  		std::cout<<"ERROR in stim::image::white - no white value known for this data type"<<std::endl;
e252a6d6   David Mayerich   somehow the C++11...
105
  		exit(1);
814eb271   David Mayerich   fixed problems wi...
106
  
a2bf1d08   David Mayerich   general bug fixes...
107
108
  	}
  
d32a1854   David Mayerich   added framework f...
109
110
111
  
  public:
  
a2bf1d08   David Mayerich   general bug fixes...
112
  	/// Default constructor - creates an empty image object
814eb271   David Mayerich   fixed problems wi...
113
  	image(){ init(); }							//initialize all variables to zero, don't allocate any memory
7b3948ab   David Mayerich   added support for...
114
  
a2bf1d08   David Mayerich   general bug fixes...
115
  	/// Constructor with a filename - loads the specified file
c8c976a9   David Mayerich   replaced CImg wit...
116
  	image(std::string filename){				//constructor initialize the image with an image file
fba6e7e3   Pavel Govyadinov   fixed image.h bug...
117
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
118
  		load(filename);
7b3948ab   David Mayerich   added support for...
119
120
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
121
  	/// Create a new image from scratch given a number of samples and channels
1a224b6a   David Mayerich   fixed linux compa...
122
  	image(size_t x, size_t y = 1, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
123
  		init();
a2bf1d08   David Mayerich   general bug fixes...
124
  		allocate(x, y, c);
c8c976a9   David Mayerich   replaced CImg wit...
125
  	}
f186dbda   Tianshu Cheng   header file for b...
126
  
a2bf1d08   David Mayerich   general bug fixes...
127
  	/// Create a new image with the data given in 'data'
c8c976a9   David Mayerich   replaced CImg wit...
128
  	image(T* data, size_t x, size_t y, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
129
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
130
131
  		allocate(x, y, c);
  		memcpy(img, data, bytes());
41acaf5d   David Mayerich   added a CUDA Gaus...
132
133
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
134
  	/// Copy constructor - duplicates an image object
814eb271   David Mayerich   fixed problems wi...
135
  	image(const stim::image<T>& I){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
136
  		init();
a2bf1d08   David Mayerich   general bug fixes...
137
  		allocate(I.X(), I.Y(), I.C());
a2bf1d08   David Mayerich   general bug fixes...
138
139
140
141
142
143
144
145
  		memcpy(img, I.img, bytes());
  	}
  
  	/// Destructor - clear memory
  	~image(){
  		free(img);
  	}
  
735a2a24   David Mayerich   started testing o...
146
  	stim::image<T>& operator=(const stim::image<T>& I){
814eb271   David Mayerich   fixed problems wi...
147
  		init();
a2bf1d08   David Mayerich   general bug fixes...
148
149
150
151
152
153
154
  		if(&I == this)									//handle self-assignment
  			return *this;
  		allocate(I.X(), I.Y(), I.C());
  		memcpy(img, I.img, bytes());
  		return *this;
  	}
  
cfcf8619   David Mayerich   changes to filena...
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
  	//save a Netpbm file
  	void load_netpbm(std::string filename) {
  		std::ifstream infile(filename, std::ios::in | std::ios::binary);		//open an output file
  		if (!infile) {
  			std::cout << "Error opening input file in image::load_netpbm()" << std::endl;
  			exit(1);
  		}
  		if (sizeof(T) != 1) {
  			std::cout << "Error in image::load_netpbm() - data type must be 8-bit integer." << std::endl;
  			exit(1);
  		}
  
  		size_t c;													//allocate space for the number of channels
  		unsigned char format[2];									//allocate space to hold the image format tag
  		infile.read(format, 2);										//read the image format tag
  		infile.seekg(1);											//skip the newline character
  
  		if (format[0] != 'P') {
  			std::cout << "Error in image::load_netpbm() - file format tag is invalid: " << format[0] << format[1] << std::endl;
  			exit(1);
  		}
  		if (format[1] == '5') c = 1;								//get the number of channels from the format flag
  		else if (format[1] == '6') c = 3;
  		else {
  			std::cout << "Error in image::load_netpbm() - file format tag is invalid: " << format[0] << format[1] << std::endl;
  			exit(1);
  		}
  		std::string sw;									//create a string to store the width of the image
  		unsigned char c;
  		while(true){
  			c = infile.get();							//get a single character
  			if (c == ' ') break;						//exit if we've encountered a space
  			sw.push_back(c);							//push the character on to the string
  		}
  		size_t w = atoi(sw.c_str());					//convert the string into an integer
  
  		std::string sh;
  		while (true) {
  			c = infile.get();
  			if (c == 0x0A) break;
  			sh.push_back(c);
  		}
  		size_t h = atoi(ah.c_str());					//convert the string into an integer
  
  		allocate(w, h, c);								//allocate space for the image
  		infile.read(img, size());						//copy the binary data from the file to the image
  		infile.close();
  	}
  	
  
  
c8c976a9   David Mayerich   replaced CImg wit...
206
  	/// Load an image from a file
d32a1854   David Mayerich   added framework f...
207
  	void load(std::string filename){
c8c976a9   David Mayerich   replaced CImg wit...
208
209
  
  		cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED);	//use OpenCV to open the image file
a2bf1d08   David Mayerich   general bug fixes...
210
  		if(!cvImage.data){
814eb271   David Mayerich   fixed problems wi...
211
  			std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<std::endl;
a2bf1d08   David Mayerich   general bug fixes...
212
213
  			exit(1);
  		}
26da82c8   David Mayerich   finalized stim::g...
214
215
216
217
  		int cols = cvImage.cols;
  		int rows = cvImage.rows;
  		int channels = cvImage.channels();
  		allocate(cols, rows, channels);			//allocate space for the image
ca99f951   David Mayerich   faster implementa...
218
  		unsigned char* cv_ptr = (unsigned char*)cvImage.data;
814eb271   David Mayerich   fixed problems wi...
219
220
221
  		if(C() == 1)														//if this is a single-color image, just copy the data
  			memcpy(img, cv_ptr, bytes());
  		if(C() == 3)														//if this is a 3-color image, OpenCV uses BGR interleaving
ca99f951   David Mayerich   faster implementa...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  			from_opencv(cv_ptr, X(), Y());
  	}
  
  	void from_opencv(unsigned char* buffer, size_t width, size_t height){
  		allocate(width, height, 3);
  		T value;
  		size_t i;
  		for(size_t c = 0; c < C(); c++){								//copy directly
  			for(size_t y = 0; y < Y(); y++){
  				for(size_t x = 0; x < X(); x++){
  					i = y * X() * C() + x * C() + (2-c);
  					value = buffer[i];
  					img[idx(x, y, c)] = value;
  				}
  			}
  		}
d32a1854   David Mayerich   added framework f...
238
239
  	}
  
cfcf8619   David Mayerich   changes to filena...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  	//save a Netpbm file
  	void save_netpbm(std::string filename) {
  		std::ofstream outfile(filename, std::ios::out | std::ios::binary);		//open an output file
  		if(!outfile) {
  			std::cout << "Error generating output file in image::save_netpbm()" << std::endl;
  			exit(1);
  		}
  		if (sizeof(T) != 1) {
  			std::cout << "Error in image::save_netpbm() - data type must be 8-bit integer." << std::endl;
  			exit(1);
  		}
  		std::string format;
  		if (channels() == 1) outfile << "P5" << (char)0x0A;			//output P5 if the file is grayscale
  		else if (channels() == 3) outfile << "P6" << (char)0x0A;		//output P6 if the file is color
  		else {
  			std::cout << "Error in image::save_netpbm() - data must be grayscale or RGB." << std::endl;
  			exit(1);
  		}
  		outfile << width() << " " << height() << (char)0x0A;			//save the width and height
  		outfile << "255" << (char)0x0A;								//output the maximum value
  		outfile.write((const char*)img, size());			//write the binary data
  		outfile.close();
  	}
  
d32a1854   David Mayerich   added framework f...
264
265
  	//save a file
  	void save(std::string filename){
c8c976a9   David Mayerich   replaced CImg wit...
266
267
  		//OpenCV uses an interleaved format, so convert first and then output
  		T* buffer = (T*) malloc(bytes());
a2bf1d08   David Mayerich   general bug fixes...
268
269
270
271
  
  		if(C() == 1)
  			memcpy(buffer, img, bytes());
  		else if(C() == 3)
9b766f1f   Pavel Govyadinov   completed merge f...
272
  			get_interleaved_bgr(buffer);
c8c976a9   David Mayerich   replaced CImg wit...
273
274
  		cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
  		cv::imwrite(filename, cvImage);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
275
  		free(buffer);
d32a1854   David Mayerich   added framework f...
276
277
  	}
  
ca99f951   David Mayerich   faster implementa...
278
279
280
281
282
  	void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){
  		allocate(width, height, channels);
  		memcpy(img, buffer, bytes());
  	}
  
8b7be670   David Mayerich   implemented savin...
283
  	//create an image from an interleaved buffer
814eb271   David Mayerich   fixed problems wi...
284
  	void set_interleaved_rgb(T* buffer, size_t width, size_t height){
ca99f951   David Mayerich   faster implementa...
285
  		set_interleaved(buffer, width, height, 3);
c8c976a9   David Mayerich   replaced CImg wit...
286
  	}
fbf0ab02   David Mayerich   added support for...
287
  
814eb271   David Mayerich   fixed problems wi...
288
289
  	void set_interleaved_bgr(T* buffer, size_t width, size_t height){
  		allocate(width, height, 3);
ca99f951   David Mayerich   faster implementa...
290
291
  		T value;
  		size_t i;
c8c976a9   David Mayerich   replaced CImg wit...
292
293
294
  		for(size_t c = 0; c < C(); c++){								//copy directly
  			for(size_t y = 0; y < Y(); y++){
  				for(size_t x = 0; x < X(); x++){
ca99f951   David Mayerich   faster implementa...
295
296
297
  					i = y * X() * C() + x * C() + (2-c);
  					value = buffer[i];
  					img[idx(x, y, c)] = value;
c8c976a9   David Mayerich   replaced CImg wit...
298
  				}
fbf0ab02   David Mayerich   added support for...
299
300
  			}
  		}
8b7be670   David Mayerich   implemented savin...
301
302
  	}
  
ca99f951   David Mayerich   faster implementa...
303
304
305
306
  	void set_interleaved(T* buffer, size_t width, size_t height){
  		set_interleaved_rgb(buffer, width, height);
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
307
  	void get_interleaved_bgr(T* data){
8b7be670   David Mayerich   implemented savin...
308
309
  
  		//for each channel
c8c976a9   David Mayerich   replaced CImg wit...
310
311
312
313
314
315
316
  		for(size_t y = 0; y < Y(); y++){
  			for(size_t x = 0; x < X(); x++){
  				for(size_t c = 0; c < C(); c++){
  					data[y * X() * C() + x * C() + (2-c)] = img[idx(x, y, c)];
  				}
  			}
  		}
8b7be670   David Mayerich   implemented savin...
317
318
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
319
320
321
322
  	void get_interleaved_rgb(T* data){
  		memcpy(data, img, bytes());
  	}
  
904614c3   David Mayerich   added normalizati...
323
324
325
326
327
328
329
330
331
332
333
334
  	//copies data in the given channel order as a non-interleaved image
  	void get_noninterleaved(T* data){
  		//for each channel
  		for(size_t y = 0; y < Y(); y++){
  			for(size_t x = 0; x < X(); x++){
  				for(size_t c = 0; c < C(); c++){
  					data[c * Y() * X() + y * X() + x] = img[idx(x, y, c)];
  				}
  			}
  		}
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
335
  
9d3ba0b1   David Mayerich   added stim::hsi a...
336
  	image<T> channel(size_t c){
41acaf5d   David Mayerich   added a CUDA Gaus...
337
338
  
  		//create a new image
a2bf1d08   David Mayerich   general bug fixes...
339
  		image<T> r(X(), Y(), 1);
41acaf5d   David Mayerich   added a CUDA Gaus...
340
  
c8c976a9   David Mayerich   replaced CImg wit...
341
342
  		for(size_t x = 0; x < X(); x++){
  			for(size_t y = 0; y < Y(); y++){
8e2fb2b4   David Mayerich   fixed a segmentat...
343
  				r.img[r.idx(x, y, 0)] = img[idx(x, y, c)];
c8c976a9   David Mayerich   replaced CImg wit...
344
345
  			}
  		}
41acaf5d   David Mayerich   added a CUDA Gaus...
346
  
c8c976a9   David Mayerich   replaced CImg wit...
347
  		return r;
41acaf5d   David Mayerich   added a CUDA Gaus...
348
349
350
  
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
351
352
  	T& operator()(size_t x, size_t y, size_t c = 0){
  		return img[idx(x, y, c)];
3d0b6243   David Mayerich   fixed hsiproc bug...
353
354
355
356
357
358
359
  	}
  
  	/// 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){
  
c8c976a9   David Mayerich   replaced CImg wit...
360
  		size_t N = size();
3d0b6243   David Mayerich   fixed hsiproc bug...
361
  
c8c976a9   David Mayerich   replaced CImg wit...
362
363
  		for(size_t n = 0; n < N; n++)
  			img[n] = v;
3d0b6243   David Mayerich   fixed hsiproc bug...
364
365
366
367
368
  
  		return *this;
  
  	}
  
f186dbda   Tianshu Cheng   header file for b...
369
370
371
372
373
  	/// 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
  
c8c976a9   David Mayerich   replaced CImg wit...
374
  	void set_channel(T* buffer, size_t c){
6d30a707   Tianshu Cheng   add cuda/array_ad...
375
  
c8c976a9   David Mayerich   replaced CImg wit...
376
377
378
379
380
381
  		size_t x, y;
  		for(y = 0; y < Y(); y++){
  			for(x = 0; x < X(); x++){
  				img[idx(x, y, c)] = buffer[c];
  			}
  		}
6d30a707   Tianshu Cheng   add cuda/array_ad...
382
383
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
384
  	size_t channels(){
c8c976a9   David Mayerich   replaced CImg wit...
385
  		return C();
8b7be670   David Mayerich   implemented savin...
386
387
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
388
  	size_t width(){
c8c976a9   David Mayerich   replaced CImg wit...
389
  		return X();
8b7be670   David Mayerich   implemented savin...
390
391
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
392
  	size_t height(){
c8c976a9   David Mayerich   replaced CImg wit...
393
  		return Y();
3d0b6243   David Mayerich   fixed hsiproc bug...
394
395
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
396
  	T* data(){
c8c976a9   David Mayerich   replaced CImg wit...
397
  		return img;
6d30a707   Tianshu Cheng   add cuda/array_ad...
398
399
  	}
  
7b3948ab   David Mayerich   added support for...
400
  	//returns the size (number of values) of the image
c8c976a9   David Mayerich   replaced CImg wit...
401
  	size_t size(){ return C() * X() * Y(); }
d32a1854   David Mayerich   added framework f...
402
  
faef7718   David Mayerich   updates to stim::...
403
  	/// Returns the number of nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
404
  	size_t nnz(){
faef7718   David Mayerich   updates to stim::...
405
  
c8c976a9   David Mayerich   replaced CImg wit...
406
  		size_t N = X() * Y() * C();
faef7718   David Mayerich   updates to stim::...
407
  
c8c976a9   David Mayerich   replaced CImg wit...
408
409
410
  		size_t nz = 0;
  		for(size_t n = 0; n < N; n++)
  			if(img[n] != 0) nz++;
faef7718   David Mayerich   updates to stim::...
411
  
c8c976a9   David Mayerich   replaced CImg wit...
412
  		return nz;	//return the number of nonzero pixels
faef7718   David Mayerich   updates to stim::...
413
414
415
416
  
  	}
  
  	//this function returns indices of pixels that have nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
417
  	std::vector<size_t> sparse_idx(){
faef7718   David Mayerich   updates to stim::...
418
  
c8c976a9   David Mayerich   replaced CImg wit...
419
  		std::vector<size_t> s;				//allocate an array
faef7718   David Mayerich   updates to stim::...
420
421
  		s.resize(nnz());					//allocate space in the array
  
c8c976a9   David Mayerich   replaced CImg wit...
422
423
  		size_t N = size();
  		//size_t C = channels();
faef7718   David Mayerich   updates to stim::...
424
  
c8c976a9   David Mayerich   replaced CImg wit...
425
  		//T* ptr = img.data();				//get a pointer to the image data
faef7718   David Mayerich   updates to stim::...
426
  
9d3ba0b1   David Mayerich   added stim::hsi a...
427
  		size_t i = 0;
c8c976a9   David Mayerich   replaced CImg wit...
428
429
430
431
  		for(size_t n = 0; n < N; n++){
  			if(img[n] != 0){
  				s[i] = n;
  				i++;
faef7718   David Mayerich   updates to stim::...
432
433
434
435
436
  			}
  		}
  
  		return s;			//return the index list
  	}
6d30a707   Tianshu Cheng   add cuda/array_ad...
437
  
1a224b6a   David Mayerich   fixed linux compa...
438
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
439
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
440
  	T maxv(){
c8c976a9   David Mayerich   replaced CImg wit...
441
442
  		T max_val = img[0];				//initialize the maximum value to the first one
  		size_t N = size();	//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
443
  
c8c976a9   David Mayerich   replaced CImg wit...
444
  		for (size_t n=0; n<N; n++){		//for every value
6d30a707   Tianshu Cheng   add cuda/array_ad...
445
  
c8c976a9   David Mayerich   replaced CImg wit...
446
447
  			if (img[n] > max_val){			//if the value is higher than the current max
  				max_val = img[n];
1a224b6a   David Mayerich   fixed linux compa...
448
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
449
450
  		}
  
1a224b6a   David Mayerich   fixed linux compa...
451
  		return max_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
452
  	}
d32a1854   David Mayerich   added framework f...
453
  
c8c976a9   David Mayerich   replaced CImg wit...
454
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
455
  	T minv(){
c8c976a9   David Mayerich   replaced CImg wit...
456
457
  		T min_val = img[0];				//initialize the maximum value to the first one
  		size_t N = size();	//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
458
  
c8c976a9   David Mayerich   replaced CImg wit...
459
460
461
  		for (size_t n=0; n<N; n++){		//for every value
  			if (img[n] < min_val){			//if the value is higher than the current max
  				min_val = img[n];
1a224b6a   David Mayerich   fixed linux compa...
462
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
463
464
  		}
  
1a224b6a   David Mayerich   fixed linux compa...
465
  		return min_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
466
467
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
468
469
470
471
472
473
  	/// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value
  	image<T> invert(T white_val){
  		size_t N = size();						//calculate the total number of values in the image
  		image<T> r(X(), Y(), C());				//allocate space for the resulting image
  		for(size_t n = 0; n < N; n++)
  			r.img[n] = white_val - img[n];		//perform the inversion
1a224b6a   David Mayerich   fixed linux compa...
474
  
a2bf1d08   David Mayerich   general bug fixes...
475
476
  		return r;								//return the inverted image
  	}
a2bf1d08   David Mayerich   general bug fixes...
477
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
478
  	image<T> srgb2lab(){
c8c976a9   David Mayerich   replaced CImg wit...
479
480
  		std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl;
  		exit(1);
6d30a707   Tianshu Cheng   add cuda/array_ad...
481
482
483
  	}
  
  	image<T> convolve2(image<T> mask){
1a224b6a   David Mayerich   fixed linux compa...
484
  
c8c976a9   David Mayerich   replaced CImg wit...
485
486
  		std::cout<<"ERROR stim::image::convolve2 - function has been broken, and shouldn't really be in here."<<std::endl;
  		exit(1);
6d30a707   Tianshu Cheng   add cuda/array_ad...
487
  	}
d32a1854   David Mayerich   added framework f...
488
489
  
  
f186dbda   Tianshu Cheng   header file for b...
490
  	image<T> rotate(float angle, float cx, float cy){
c8c976a9   David Mayerich   replaced CImg wit...
491
492
  		std::cout<<"ERROR stim::image::rotate - function has been broken, and shouldn't really be in here."<<std::endl;
  		exit(1);
f186dbda   Tianshu Cheng   header file for b...
493
  	}
945ee13c   Laila Saadatifard   the get_list func...
494
  
945ee13c   Laila Saadatifard   the get_list func...
495
496
  	// leila's code for non_interleaving data in 3D
  	//create an data set from an interleaved buffer
9d3ba0b1   David Mayerich   added stim::hsi a...
497
  	void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
c8c976a9   David Mayerich   replaced CImg wit...
498
499
  		std::cout<<"ERROR stim::image::set_interleaved3 - stim::image no longer supports 3D images."<<std::endl;
  		exit(1);
945ee13c   Laila Saadatifard   the get_list func...
500
  	}
1a224b6a   David Mayerich   fixed linux compa...
501
  
d32a1854   David Mayerich   added framework f...
502
503
  };
  
7b3948ab   David Mayerich   added support for...
504
505
  };		//end namespace stim
  
d32a1854   David Mayerich   added framework f...
506
507
  
  #endif