Blame view

stim/image/image.h 4.24 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
34
35
36
37
  	/// Constructor initializes an image to a given size
  	image(unsigned int x, unsigned int y = 1, unsigned int z = 1){
  		img = cimg_library::CImg<T>(x, y, z);
  	}
  
d32a1854   David Mayerich   added framework f...
38
39
  	//Load an image from a file
  	void load(std::string filename){
7b3948ab   David Mayerich   added support for...
40
  		img.load(filename.c_str());
d32a1854   David Mayerich   added framework f...
41
42
43
44
  	}
  
  	//save a file
  	void save(std::string filename){
7b3948ab   David Mayerich   added support for...
45
  		img.save(filename.c_str());
d32a1854   David Mayerich   added framework f...
46
47
  	}
  
8b7be670   David Mayerich   implemented savin...
48
  	//create an image from an interleaved buffer
fbf0ab02   David Mayerich   added support for...
49
50
  	void set_interleaved(T* buffer, unsigned int width, unsigned int height, unsigned int channels = 1){
  
84ca9bba   Laila Saadatifard   fix some bugs in ...
51
  		T* non_interleaved = (T*)malloc(width * height * 3 * sizeof(T));
fbf0ab02   David Mayerich   added support for...
52
53
54
55
56
57
58
  		unsigned int S = width * height;
  
  		for(unsigned int i = 0; i < S; i++){
  			for(unsigned int c = 0; c < channels; c++){
  				non_interleaved[i + c * S] = buffer[i * channels + c];
  			}
  		}
d32a1854   David Mayerich   added framework f...
59
  
84ca9bba   Laila Saadatifard   fix some bugs in ...
60
  		img = cimg_library::CImg<T>(non_interleaved, width, height, 1, channels);
7b3948ab   David Mayerich   added support for...
61
62
  	}
  
32504c2c   David Mayerich   fixed some functi...
63
  	//fills an allocated region of memory with non-interleaved data
8b7be670   David Mayerich   implemented savin...
64
65
66
67
68
69
70
  	void data_noninterleaved(T* data){
  		memcpy(data, img.data(), sizeof(T) * size());
  	}
  
  	void data_interleaved(T* data){
  
  		unsigned int C = channels();
faef7718   David Mayerich   updates to stim::...
71
  		unsigned int X = width() * height();
8b7be670   David Mayerich   implemented savin...
72
73
74
75
76
77
78
79
80
81
  
  		T* ptr = img.data();
  
  		//for each channel
  		for(unsigned int c = 0; c < C; c++)
  			//convert each pixel
  			for(unsigned int x = 0; x < X; x++)
  				data[x * C + c] = ptr[c * X + x];
  	}
  
41acaf5d   David Mayerich   added a CUDA Gaus...
82
83
84
85
86
  	image<T> channel(unsigned int c){
  
  		//create a new image
  		image<T> single;
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
87
  		single.img = img.get_channel(c);
41acaf5d   David Mayerich   added a CUDA Gaus...
88
89
90
91
92
  
  		return single;
  
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
93
94
95
96
97
98
99
100
101
102
103
  	image<T> getslice(unsigned int c){
  
  		//create a new image
  		image<T> slice;
  
  		slice.img = img.get_slice(c);
  
  		return slice;
  
  	}
  
8b7be670   David Mayerich   implemented savin...
104
105
106
107
108
109
110
111
112
113
  	unsigned int channels(){
  		return (unsigned int)img.spectrum();
  	}
  
  	unsigned int width(){
  		return img.width();
  	}
  
  	unsigned int height(){
  		return img.height();
7b3948ab   David Mayerich   added support for...
114
  	}
d32a1854   David Mayerich   added framework f...
115
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
116
117
118
119
  	T* data(){
  		return img.data();
  	}
  
7b3948ab   David Mayerich   added support for...
120
121
122
  	//returns the size (number of values) of the image
  	unsigned long size(){
  		return img.size();
d32a1854   David Mayerich   added framework f...
123
124
  	}
  
faef7718   David Mayerich   updates to stim::...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  	/// Returns the number of nonzero values
  	unsigned int nnz(){
  
  		unsigned long P = width() * height();
  		unsigned long C = channels();
  
  		T* ptr = img.data();
  
  		unsigned long n = 0;
  
  		for(unsigned long p = 0; p < P; p++){
  			for(unsigned long c = 0; c < C; c++){
  
  				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
  	std::vector<unsigned long> sparse_idx(){
  
  		std::vector<unsigned long> s;		//allocate an array
  		s.resize(nnz());					//allocate space in the array
  
  		unsigned long P = width() * height();
  		unsigned long C = channels();
  
  		T* ptr = img.data();				//get a pointer to the image data
  
  		unsigned long i = 0;
  		for(unsigned long p = 0; p < P; p++){
  			for(unsigned long c = 0; c < C; c++){
  
  				if(ptr[c * P + p] > 0){
  					s[i] = p;
  					i++;
  					break;
  				}
  			}
  		}
  
  		return s;			//return the index list
  	}
6d30a707   Tianshu Cheng   add cuda/array_ad...
174
  
d32a1854   David Mayerich   added framework f...
175
  	
6d30a707   Tianshu Cheng   add cuda/array_ad...
176
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
177
  	T maxv(){
6d30a707   Tianshu Cheng   add cuda/array_ad...
178
179
180
181
182
183
184
185
186
187
188
189
190
  		float max = 0;
  		unsigned long N = width() * height();		//get the number of pixels
  
  		for (unsigned long i=0; i<N; i++){
  
  			if (img.data()[i] > max)
  			{
  				max = img.data()[i];
  			}	
  		}
  
  		return max;	
  	}
d32a1854   David Mayerich   added framework f...
191
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
192
  	/// Returns the minimum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
193
  	T minv(){
6d30a707   Tianshu Cheng   add cuda/array_ad...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  		float min = 0;
  		unsigned long N = width() * height();		//get the number of pixels
  
  		for (unsigned long i=0; i<N; i++){
  
  			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...
229
230
231
232
  
  
  };
  
7b3948ab   David Mayerich   added support for...
233
234
  };		//end namespace stim
  
d32a1854   David Mayerich   added framework f...
235
236
  
  #endif