image.h
5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef STIM_IMAGE_H
#define STIM_IMAGE_H
#ifdef JPEG_FOUND
#define cimg_use_jpeg //necessary for JPG files
#endif
#include "CImg.h"
#include <vector>
#include <iostream>
namespace stim{
//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{
cimg_library::CImg<T> img;
public:
//default constructor
image(){
}
//constructor (load an image file)
image(std::string filename){
img.load(filename.c_str());
}
/// 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);
}*/
image(size_t x, size_t y = 1, size_t z = 1, size_t c = 1){
img = cimg_library::CImg<T>(x, y, z, c);
}
//Load an image from a file
void load(std::string filename){
img.load(filename.c_str());
}
//save a file
void save(std::string filename){
img.save(filename.c_str());
}
//create an image from an interleaved buffer
void set_interleaved(T* buffer, size_t width, size_t height, size_t channels = 1){
T* non_interleaved = (T*)malloc(width * height * 3 * sizeof(T));
size_t S = width * height;
for(size_t i = 0; i < S; i++){
for(size_t c = 0; c < channels; c++){
non_interleaved[i + c * S] = buffer[i * channels + c];
}
}
img = cimg_library::CImg<T>(non_interleaved, width, height, 1, channels);
}
//fills an allocated region of memory with non-interleaved data
void data_noninterleaved(T* data){
memcpy(data, img.data(), sizeof(T) * size());
}
void data_interleaved(T* data){
size_t C = channels();
size_t X = width() * height();
T* ptr = img.data();
//for each channel
for(size_t c = 0; c < C; c++)
//convert each pixel
for(size_t x = 0; x < X; x++)
data[x * C + c] = ptr[c * X + x];
}
image<T> channel(size_t c){
//create a new image
image<T> single;
single.img = img.get_channel(c);
return single;
}
T& operator()(size_t x, size_t y, size_t z = 0, size_t c = 0){
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){
size_t X = width() * height() * depth() * channels();
for(size_t x = 0; x < X; x++)
img.data()[x] = v;
return *this;
}
/// 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
void set_channel(size_t c, T* buffer){
//calculate the number of pixels in a channel
size_t channel_size = width() * height();
//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);
}
image<T> getslice(size_t c){
//create a new image
image<T> slice;
slice.img = img.get_slice(c);
return slice;
}
size_t channels(){
return (size_t)img.spectrum();
}
size_t width(){
return img.width();
}
size_t height(){
return img.height();
}
size_t depth(){
return img.depth();
}
T* data(){
return img.data();
}
//returns the size (number of values) of the image
size_t size(){
return img.size();
}
/// Returns the number of nonzero values
size_t nnz(){
size_t P = width() * height();
size_t C = channels();
T* ptr = img.data();
size_t n = 0;
for(size_t p = 0; p < P; p++){
for(size_t 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<size_t> sparse_idx(){
std::vector<size_t> s; //allocate an array
s.resize(nnz()); //allocate space in the array
size_t P = width() * height();
size_t C = channels();
T* ptr = img.data(); //get a pointer to the image data
size_t i = 0;
for(size_t p = 0; p < P; p++){
for(size_t c = 0; c < C; c++){
if(ptr[c * P + p] > 0){
s[i] = p;
i++;
break;
}
}
}
return s; //return the index list
}
/// Returns the maximum pixel value in the image
T maxv(){
float max = 0;
size_t N = width() * height(); //get the number of pixels
for (size_t i=0; i<N; i++){
if (img.data()[i] > max)
{
max = img.data()[i];
}
}
return max;
}
/// Returns the minimum pixel value in the image
T minv(){
float min = 0;
size_t N = width() * height(); //get the number of pixels
for (size_t 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;
}
image<T> rotate(float angle, float cx, float cy){
image<T> result;
float zoom = 1;
size_t interpolation = 1;
size_t boundary = 1;
result.img = img.get_rotate (angle, cx, cy, zoom, interpolation, boundary);
//result.save("data_output/test_rotate_neum.bmp");
return result;
}
// leila's code for non_interleaving data in 3D
//create an data set from an interleaved buffer
void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
T* non_interleaved3 = (T*)malloc(width * height * depth * 3 * sizeof(T));
size_t p = width * height * depth;
for(size_t i = 0; i < p; i++){
for(size_t c = 0; c < channels; c++){
non_interleaved3[i + c * p] = buffer[i * channels + c];
}
}
img = cimg_library::CImg<T>(non_interleaved3, width, height, depth, channels);
}
};
}; //end namespace stim
#endif