-
Notifications
You must be signed in to change notification settings - Fork 19
/
Image.hpp
365 lines (289 loc) · 7.98 KB
/
Image.hpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/****************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
/****************************************************************************
*
* Image.hpp
*
* Purpose: C++ wrapper for OpenCV IplImage which supports simple and
* efficient access to the image data
*
* Author: Donovan Parks, September 2007
*
* Based on code from:
* http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
******************************************************************************/
#ifndef _IMAGE_H_
#define _IMAGE_H_
#include <cv.h>
#include <cxcore.h>
// --- Image Iterator ---------------------------------------------------------
template <class T>
class ImageIterator
{
public:
ImageIterator(IplImage* image, int x=0, int y=0, int dx= 0, int dy=0) :
i(x), j(y), i0(0)
{
data = reinterpret_cast<T*>(image->imageData);
step = image->widthStep / sizeof(T);
nl= image->height;
if ((y+dy)>0 && (y+dy) < nl)
nl= y+dy;
if (y<0)
j=0;
data += step*j;
nc = image->width;
if ((x+dx) > 0 && (x+dx) < nc)
nc = x+dx;
nc *= image->nChannels;
if (x>0)
i0 = x*image->nChannels;
i = i0;
nch = image->nChannels;
}
/* has next ? */
bool operator!() const { return j < nl; }
/* next pixel */
ImageIterator& operator++()
{
i++;
if (i >= nc)
{
i=i0;
j++;
data += step;
}
return *this;
}
ImageIterator& operator+=(int s)
{
i+=s;
if (i >= nc)
{
i=i0;
j++;
data += step;
}
return *this;
}
/* pixel access */
T& operator*() { return data[i]; }
const T operator*() const { return data[i]; }
const T neighbor(int dx, int dy) const
{
return *(data+dy*step+i+dx);
}
T* operator&() const { return data+i; }
/* current pixel coordinates */
int column() const { return i/nch; }
int line() const { return j; }
private:
int i, i0,j;
T* data;
int step;
int nl, nc;
int nch;
};
// --- Constants --------------------------------------------------------------
const unsigned char NUM_CHANNELS = 3;
// --- Pixel Types ------------------------------------------------------------
class RgbPixel
{
public:
RgbPixel() {;}
RgbPixel(unsigned char _r, unsigned char _g, unsigned char _b)
{
ch[0] = _r; ch[1] = _g; ch[2] = _b;
}
RgbPixel& operator=(const RgbPixel& rhs)
{
ch[0] = rhs.ch[0]; ch[1] = rhs.ch[1]; ch[2] = rhs.ch[2];
return *this;
}
inline unsigned char& operator()(const int _ch)
{
return ch[_ch];
}
inline unsigned char operator()(const int _ch) const
{
return ch[_ch];
}
unsigned char ch[3];
};
class RgbPixelFloat
{
public:
RgbPixelFloat() {;}
RgbPixelFloat(float _r, float _g, float _b)
{
ch[0] = _r; ch[1] = _g; ch[2] = _b;
}
RgbPixelFloat& operator=(const RgbPixelFloat& rhs)
{
ch[0] = rhs.ch[0]; ch[1] = rhs.ch[1]; ch[2] = rhs.ch[2];
return *this;
}
inline float& operator()(const int _ch)
{
return ch[_ch];
}
inline float operator()(const int _ch) const
{
return ch[_ch];
}
float ch[3];
};
// --- Image Types ------------------------------------------------------------
class ImageBase
{
public:
ImageBase(IplImage* img = NULL) { imgp = img; m_bReleaseMemory = true; }
~ImageBase();
void ReleaseMemory(bool b) { m_bReleaseMemory = b; }
IplImage* Ptr() { return imgp; }
const IplImage* Ptr() const { return imgp; }
void operator=(IplImage* img)
{
imgp = img;
}
// copy-constructor
ImageBase(const ImageBase& rhs)
{
// it is very inefficent if this copy-constructor is called
assert(false);
}
// assignment operator
ImageBase& operator=(const ImageBase& rhs)
{
// it is very inefficent if operator= is called
assert(false);
return *this;
}
virtual void Clear() = 0;
protected:
IplImage* imgp;
bool m_bReleaseMemory;
};
class RgbImage : public ImageBase
{
public:
RgbImage(IplImage* img = NULL) : ImageBase(img) { ; }
virtual void Clear()
{
cvZero(imgp);
}
void operator=(IplImage* img)
{
imgp = img;
}
// channel-level access using image(row, col, channel)
inline unsigned char& operator()(const int r, const int c, const int ch)
{
return (unsigned char &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels+ch];
}
inline const unsigned char& operator()(const int r, const int c, const int ch) const
{
return (unsigned char &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels+ch];
}
// RGB pixel-level access using image(row, col)
inline RgbPixel& operator()(const int r, const int c)
{
return (RgbPixel &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels];
}
inline const RgbPixel& operator()(const int r, const int c) const
{
return (RgbPixel &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels];
}
};
class RgbImageFloat : public ImageBase
{
public:
RgbImageFloat(IplImage* img = NULL) : ImageBase(img) { ; }
virtual void Clear()
{
cvZero(imgp);
}
void operator=(IplImage* img)
{
imgp = img;
}
// channel-level access using image(row, col, channel)
inline float& operator()(const int r, const int c, const int ch)
{
return (float &)imgp->imageData[r*imgp->widthStep+(c*imgp->nChannels+ch)*sizeof(float)];
}
inline float operator()(const int r, const int c, const int ch) const
{
return (float)imgp->imageData[r*imgp->widthStep+(c*imgp->nChannels+ch)*sizeof(float)];
}
// RGB pixel-level access using image(row, col)
inline RgbPixelFloat& operator()(const int r, const int c)
{
return (RgbPixelFloat &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels*sizeof(float)];
}
inline const RgbPixelFloat& operator()(const int r, const int c) const
{
return (RgbPixelFloat &)imgp->imageData[r*imgp->widthStep+c*imgp->nChannels*sizeof(float)];
}
};
class BwImage : public ImageBase
{
public:
BwImage(IplImage* img = NULL) : ImageBase(img) { ; }
virtual void Clear()
{
cvZero(imgp);
}
void operator=(IplImage* img)
{
imgp = img;
}
// pixel-level access using image(row, col)
inline unsigned char& operator()(const int r, const int c)
{
return (unsigned char &)imgp->imageData[r*imgp->widthStep+c];
}
inline unsigned char operator()(const int r, const int c) const
{
return (unsigned char)imgp->imageData[r*imgp->widthStep+c];
}
};
class BwImageFloat : public ImageBase
{
public:
BwImageFloat(IplImage* img = NULL) : ImageBase(img) { ; }
virtual void Clear()
{
cvZero(imgp);
}
void operator=(IplImage* img)
{
imgp = img;
}
// pixel-level access using image(row, col)
inline float& operator()(const int r, const int c)
{
return (float &)imgp->imageData[r*imgp->widthStep+c*sizeof(float)];
}
inline float operator()(const int r, const int c) const
{
return (float)imgp->imageData[r*imgp->widthStep+c*sizeof(float)];
}
};
// --- Image Functions --------------------------------------------------------
void DensityFilter(BwImage& image, BwImage& filtered, int minDensity, unsigned char fgValue);
#endif