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>
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
9
|
|
7b3948ab
David Mayerich
added support for...
|
10
|
namespace stim{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
11
12
|
/// 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...
|
13
14
15
16
17
18
|
//currently this interface uses CImg
// T = data type (usually unsigned char)
template <class T>
class image{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
19
20
21
22
|
//cimg_library::CImg<T> img;
T* img; //pointer to the image data (assumes RGB for loading/saving)
size_t R[3];
|
a2bf1d08
David Mayerich
general bug fixes...
|
23
24
25
|
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...
|
26
|
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
27
28
|
size_t bytes(){ return size() * sizeof(T); }
|
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
|
735a2a24
David Mayerich
started testing o...
|
36
|
img=NULL;
|
c8c976a9
David Mayerich
replaced CImg wit...
|
37
|
}
|
1a224b6a
David Mayerich
fixed linux compa...
|
38
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
39
40
41
42
43
44
45
|
void clear(){ //clears all image data
unalloc(); //unallocate previous memory
init(); //re-initialize the variables
}
void allocate(){
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
46
47
48
|
unalloc();
img = (T*) malloc( bytes() ); //allocate memory
memset(img, 0, bytes());
|
c8c976a9
David Mayerich
replaced CImg wit...
|
49
50
51
|
}
void allocate(size_t x, size_t y, size_t c){ //allocate memory based on the resolution
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
52
|
unalloc();
|
c8c976a9
David Mayerich
replaced CImg wit...
|
53
54
55
56
|
R[0] = c; R[1] = x; R[2] = y; //set the resolution
allocate(); //allocate memory
}
|
c8c976a9
David Mayerich
replaced CImg wit...
|
57
58
59
60
|
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...
|
61
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
62
63
|
int cv_type(){
|
3ff136b3
David Mayerich
replaced C++11 co...
|
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
// 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...
|
81
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
82
|
std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
|
3ff136b3
David Mayerich
replaced C++11 co...
|
83
|
exit(1);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
84
85
|
}
|
a2bf1d08
David Mayerich
general bug fixes...
|
86
87
|
/// Returns the value for "white" based on the dynamic range (assumes white is 1.0 for floating point images)
T white(){
|
3ff136b3
David Mayerich
replaced C++11 co...
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
// 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...
|
105
106
|
std::cout<<"ERROR in stim::image::white - no white value known for this data type"<<std::endl;
|
3ff136b3
David Mayerich
replaced C++11 co...
|
107
|
exit(1);
|
a2bf1d08
David Mayerich
general bug fixes...
|
108
109
|
}
|
d32a1854
David Mayerich
added framework f...
|
110
111
112
|
public:
|
a2bf1d08
David Mayerich
general bug fixes...
|
113
|
/// Default constructor - creates an empty image object
|
735a2a24
David Mayerich
started testing o...
|
114
115
116
|
image(){
init(); //initialize all variables to zero, don't allocate any memory
}
|
7b3948ab
David Mayerich
added support for...
|
117
|
|
a2bf1d08
David Mayerich
general bug fixes...
|
118
|
/// Constructor with a filename - loads the specified file
|
c8c976a9
David Mayerich
replaced CImg wit...
|
119
|
image(std::string filename){ //constructor initialize the image with an image file
|
fba6e7e3
Pavel Govyadinov
fixed image.h bug...
|
120
|
init();
|
c8c976a9
David Mayerich
replaced CImg wit...
|
121
|
load(filename);
|
7b3948ab
David Mayerich
added support for...
|
122
123
|
}
|
c8c976a9
David Mayerich
replaced CImg wit...
|
124
|
/// Create a new image from scratch given a number of samples and channels
|
1a224b6a
David Mayerich
fixed linux compa...
|
125
|
image(size_t x, size_t y = 1, size_t c = 1){
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
126
|
init();
|
a2bf1d08
David Mayerich
general bug fixes...
|
127
|
allocate(x, y, c);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
128
|
}
|
f186dbda
Tianshu Cheng
header file for b...
|
129
|
|
a2bf1d08
David Mayerich
general bug fixes...
|
130
|
/// Create a new image with the data given in 'data'
|
c8c976a9
David Mayerich
replaced CImg wit...
|
131
|
image(T* data, size_t x, size_t y, size_t c = 1){
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
132
|
init();
|
c8c976a9
David Mayerich
replaced CImg wit...
|
133
134
|
allocate(x, y, c);
memcpy(img, data, bytes());
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
135
136
|
}
|
a2bf1d08
David Mayerich
general bug fixes...
|
137
|
/// Copy constructor - duplicates an image object
|
735a2a24
David Mayerich
started testing o...
|
138
|
image(const stim::image<T> &I){
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
139
|
init();
|
a2bf1d08
David Mayerich
general bug fixes...
|
140
|
allocate(I.X(), I.Y(), I.C());
|
a2bf1d08
David Mayerich
general bug fixes...
|
141
142
143
144
145
146
147
148
|
memcpy(img, I.img, bytes());
}
/// Destructor - clear memory
~image(){
free(img);
}
|
735a2a24
David Mayerich
started testing o...
|
149
|
stim::image<T>& operator=(const stim::image<T>& I){
|
a2bf1d08
David Mayerich
general bug fixes...
|
150
151
152
153
154
155
156
|
if(&I == this) //handle self-assignment
return *this;
allocate(I.X(), I.Y(), I.C());
memcpy(img, I.img, bytes());
return *this;
}
|
c8c976a9
David Mayerich
replaced CImg wit...
|
157
|
/// Load an image from a file
|
d32a1854
David Mayerich
added framework f...
|
158
|
void load(std::string filename){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
159
160
|
cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED); //use OpenCV to open the image file
|
a2bf1d08
David Mayerich
general bug fixes...
|
161
|
if(!cvImage.data){
|
735a2a24
David Mayerich
started testing o...
|
162
|
std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<" ["<<__FILE__<<" (line "<<__LINE__<<")]"<<std::endl;
|
a2bf1d08
David Mayerich
general bug fixes...
|
163
164
|
exit(1);
}
|
c8c976a9
David Mayerich
replaced CImg wit...
|
165
|
allocate(cvImage.cols, cvImage.rows, cvImage.channels()); //allocate space for the image
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
166
167
168
169
|
T* cv_ptr = (T*) cvImage.data;
if(C() == 1)
{
//if this is a single-color image, just copy the data
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
170
|
memcpy(img, cv_ptr, bytes());
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
171
|
}
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
172
173
|
if(C() == 3)
{ //if this is a 3-color image, OpenCV uses BGR interleaving
|
a2bf1d08
David Mayerich
general bug fixes...
|
174
|
set_interleaved_bgr(cv_ptr, X(), Y());
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
175
|
}
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
176
|
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
177
|
cvImage.release();
|
d32a1854
David Mayerich
added framework f...
|
178
179
180
181
|
}
//save a file
void save(std::string filename){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
182
183
|
//OpenCV uses an interleaved format, so convert first and then output
T* buffer = (T*) malloc(bytes());
|
a2bf1d08
David Mayerich
general bug fixes...
|
184
185
186
187
|
if(C() == 1)
memcpy(buffer, img, bytes());
else if(C() == 3)
|
9b766f1f
Pavel Govyadinov
completed merge f...
|
188
|
get_interleaved_bgr(buffer);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
189
190
|
cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
cv::imwrite(filename, cvImage);
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
191
192
|
cvImage.release();
free(buffer);
|
d32a1854
David Mayerich
added framework f...
|
193
194
|
}
|
8b7be670
David Mayerich
implemented savin...
|
195
|
//create an image from an interleaved buffer
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
196
197
|
void set_interleaved_rgb(T* buffer, size_t width, size_t height, size_t channels = 3){
allocate(width, height, channels);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
198
199
|
memcpy(img, buffer, bytes());
}
|
fbf0ab02
David Mayerich
added support for...
|
200
|
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
201
202
|
void set_interleaved_bgr(T* buffer, size_t width, size_t height, size_t channels = 3){
allocate(width, height, channels);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
203
204
205
206
207
|
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++){
img[idx(x, y, c)] = buffer[y * X() * C() + x * C() + (2-c)];
}
|
fbf0ab02
David Mayerich
added support for...
|
208
209
|
}
}
|
8b7be670
David Mayerich
implemented savin...
|
210
211
|
}
|
9b766f1f
Pavel Govyadinov
completed merge f...
|
212
|
void get_interleaved_bgr(T* data){
|
8b7be670
David Mayerich
implemented savin...
|
213
214
|
//for each channel
|
c8c976a9
David Mayerich
replaced CImg wit...
|
215
216
217
218
219
220
221
|
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...
|
222
223
|
}
|
9b766f1f
Pavel Govyadinov
completed merge f...
|
224
225
226
227
|
void get_interleaved_rgb(T* data){
memcpy(data, img, bytes());
}
|
904614c3
David Mayerich
added normalizati...
|
228
229
230
231
232
233
234
235
236
237
238
239
|
//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...
|
240
|
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
241
|
image<T> channel(size_t c){
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
242
243
|
//create a new image
|
a2bf1d08
David Mayerich
general bug fixes...
|
244
|
image<T> r(X(), Y(), 1);
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
245
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
246
247
|
for(size_t x = 0; x < X(); x++){
for(size_t y = 0; y < Y(); y++){
|
8e2fb2b4
David Mayerich
fixed a segmentat...
|
248
|
r.img[r.idx(x, y, 0)] = img[idx(x, y, c)];
|
c8c976a9
David Mayerich
replaced CImg wit...
|
249
250
|
}
}
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
251
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
252
|
return r;
|
41acaf5d
David Mayerich
added a CUDA Gaus...
|
253
254
255
|
}
|
c8c976a9
David Mayerich
replaced CImg wit...
|
256
257
|
T& operator()(size_t x, size_t y, size_t c = 0){
return img[idx(x, y, c)];
|
3d0b6243
David Mayerich
fixed hsiproc bug...
|
258
259
260
261
262
263
264
|
}
/// 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...
|
265
|
size_t N = size();
|
3d0b6243
David Mayerich
fixed hsiproc bug...
|
266
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
267
268
|
for(size_t n = 0; n < N; n++)
img[n] = v;
|
3d0b6243
David Mayerich
fixed hsiproc bug...
|
269
270
271
272
273
|
return *this;
}
|
f186dbda
Tianshu Cheng
header file for b...
|
274
275
276
277
278
|
/// 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...
|
279
|
void set_channel(T* buffer, size_t c){
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
280
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
281
282
283
284
285
286
|
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...
|
287
288
|
}
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
289
|
size_t channels(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
290
|
return C();
|
8b7be670
David Mayerich
implemented savin...
|
291
292
|
}
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
293
|
size_t width(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
294
|
return X();
|
8b7be670
David Mayerich
implemented savin...
|
295
296
|
}
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
297
|
size_t height(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
298
|
return Y();
|
3d0b6243
David Mayerich
fixed hsiproc bug...
|
299
300
|
}
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
301
|
T* data(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
302
|
return img;
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
303
304
|
}
|
7b3948ab
David Mayerich
added support for...
|
305
|
//returns the size (number of values) of the image
|
c8c976a9
David Mayerich
replaced CImg wit...
|
306
|
size_t size(){ return C() * X() * Y(); }
|
d32a1854
David Mayerich
added framework f...
|
307
|
|
faef7718
David Mayerich
updates to stim::...
|
308
|
/// Returns the number of nonzero values
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
309
|
size_t nnz(){
|
faef7718
David Mayerich
updates to stim::...
|
310
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
311
|
size_t N = X() * Y() * C();
|
faef7718
David Mayerich
updates to stim::...
|
312
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
313
314
315
|
size_t nz = 0;
for(size_t n = 0; n < N; n++)
if(img[n] != 0) nz++;
|
faef7718
David Mayerich
updates to stim::...
|
316
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
317
|
return nz; //return the number of nonzero pixels
|
faef7718
David Mayerich
updates to stim::...
|
318
319
320
321
|
}
//this function returns indices of pixels that have nonzero values
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
322
|
std::vector<size_t> sparse_idx(){
|
faef7718
David Mayerich
updates to stim::...
|
323
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
324
|
std::vector<size_t> s; //allocate an array
|
faef7718
David Mayerich
updates to stim::...
|
325
326
|
s.resize(nnz()); //allocate space in the array
|
c8c976a9
David Mayerich
replaced CImg wit...
|
327
328
|
size_t N = size();
//size_t C = channels();
|
faef7718
David Mayerich
updates to stim::...
|
329
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
330
|
//T* ptr = img.data(); //get a pointer to the image data
|
faef7718
David Mayerich
updates to stim::...
|
331
|
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
332
|
size_t i = 0;
|
c8c976a9
David Mayerich
replaced CImg wit...
|
333
334
335
336
|
for(size_t n = 0; n < N; n++){
if(img[n] != 0){
s[i] = n;
i++;
|
faef7718
David Mayerich
updates to stim::...
|
337
338
339
340
341
|
}
}
return s; //return the index list
}
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
342
|
|
1a224b6a
David Mayerich
fixed linux compa...
|
343
|
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
344
|
/// Returns the maximum pixel value in the image
|
96f9b10f
Laila Saadatifard
change the header...
|
345
|
T maxv(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
346
347
|
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...
|
348
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
349
|
for (size_t n=0; n<N; n++){ //for every value
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
350
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
351
352
|
if (img[n] > max_val){ //if the value is higher than the current max
max_val = img[n];
|
1a224b6a
David Mayerich
fixed linux compa...
|
353
|
}
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
354
355
|
}
|
1a224b6a
David Mayerich
fixed linux compa...
|
356
|
return max_val;
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
357
|
}
|
d32a1854
David Mayerich
added framework f...
|
358
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
359
|
/// Returns the maximum pixel value in the image
|
96f9b10f
Laila Saadatifard
change the header...
|
360
|
T minv(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
361
362
|
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...
|
363
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
364
365
366
|
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...
|
367
|
}
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
368
369
|
}
|
1a224b6a
David Mayerich
fixed linux compa...
|
370
|
return min_val;
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
371
372
|
}
|
a2bf1d08
David Mayerich
general bug fixes...
|
373
374
375
376
377
378
|
/// 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...
|
379
|
|
a2bf1d08
David Mayerich
general bug fixes...
|
380
381
|
return r; //return the inverted image
}
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
382
383
384
385
386
387
388
389
390
391
392
|
/// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value
image<T> invert(){
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
T white_val = maxv();
for(size_t n = 0; n < N; n++)
r.img[n] = white_val - img[n]; //perform the inversion
return r; //return the inverted image
}
|
a2bf1d08
David Mayerich
general bug fixes...
|
393
|
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
394
|
///crops the image from x1 to x0 and y1 to y0 and returns a new (smaller) image.
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
395
396
397
398
399
400
|
image<T> crop(int x0, int x1, int y0, int y1)
{
image<T> ret(x1-x0, y1-y0, C());
int newWidth = x1-x0;
int destidx, srcidx;
|
fba6e7e3
Pavel Govyadinov
fixed image.h bug...
|
401
|
///for each row, cut what amount of data from the original and put it into the new copy.
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
402
|
for(int i = 0; i < (y1-y0); i++)
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
403
404
405
|
{
destidx = i*newWidth*C(); ///destination index one per each row
srcidx = ((i+(y0))*X()+x0)*C(); ///source index, one per each row.
|
e9a1b974
Pavel Govyadinov
imlemented Dr. Ma...
|
406
|
memcpy(&ret.img[destidx], &img[srcidx], sizeof(T)*newWidth*C());
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
407
|
}
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
408
409
|
return ret;
}
|
a2bf1d08
David Mayerich
general bug fixes...
|
410
|
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
411
|
image<T> srgb2lab(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
412
413
|
std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl;
exit(1);
|
6d30a707
Tianshu Cheng
add cuda/array_ad...
|
414
415
416
|
}
image<T> convolve2(image<T> mask){
|
1a224b6a
David Mayerich
fixed linux compa...
|
417
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
418
419
|
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...
|
420
|
}
|
d32a1854
David Mayerich
added framework f...
|
421
422
|
|
f186dbda
Tianshu Cheng
header file for b...
|
423
|
image<T> rotate(float angle, float cx, float cy){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
424
425
|
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...
|
426
|
}
|
945ee13c
Laila Saadatifard
the get_list func...
|
427
|
|
ae05c3e4
Pavel Govyadinov
Found an error wi...
|
428
|
|
945ee13c
Laila Saadatifard
the get_list func...
|
429
430
|
// leila's code for non_interleaving data in 3D
//create an data set from an interleaved buffer
|
9d3ba0b1
David Mayerich
added stim::hsi a...
|
431
|
void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
432
433
|
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...
|
434
|
}
|
1a224b6a
David Mayerich
fixed linux compa...
|
435
|
|
d32a1854
David Mayerich
added framework f...
|
436
437
|
};
|
7b3948ab
David Mayerich
added support for...
|
438
439
|
}; //end namespace stim
|
d32a1854
David Mayerich
added framework f...
|
440
441
|
#endif
|