-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathimage.h
325 lines (274 loc) · 7.28 KB
/
image.h
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
/*
图像基本处理辅助函数 库文件
Reference:https://github.com/robertwgh/ezSIFT
*/
#ifndef SIFT_IMAGE_H
#define SIFT_IMAGE_H
#include "image_utility.h"
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <typeinfo>
namespace sift {
template <typename T>
class Image {
public:
int w;
int h;
T *data;
Image();
Image(int _w, int _h);
// Copy construction function
Image(const Image<T> &img);
~Image();
Image<T> &operator=(const Image<T> &img);
void init(int _w, int _h);
void reinit(int _w, int _h);
void release();
int read_pgm(const char *filename);
int write_pgm(const char *filename);
Image<unsigned char> to_uchar() const;
Image<float> to_float() const;
// Upsample the image by 2x, linear interpolation.
Image<T> upsample_2x() const;
// Downsample the image by 2x, nearest neighbor interpolation.
Image<T> downsample_2x() const;
};
// Member function definition
// 图片对象初始化
template <typename T>
Image<T>::Image()
{
w = 0;
h = 0;
data = NULL;
}
// 图片对象初始化(指定w, h)
template <typename T>
Image<T>::Image(int _w, int _h)
{
w = _w;
h = _h;
data = new T[w * h];
}
// Copy construction function
// 图片对象赋值
template <typename T>
Image<T>::Image(const Image<T> &img)
{
w = img.w;
h = img.h;
data = new T[w * h];
memcpy(data, img.data, w * h * sizeof(T));
}
// 图片对象析构
template <typename T>
Image<T>::~Image()
{
if (data) {
delete[] data;
data = NULL;
}
}
// 重定义图片对象的'='操作
template <typename T>
Image<T> &Image<T>::operator=(const Image<T> &img)
{
init(img.w, img.h);
memcpy(data, img.data, img.w * img.h * sizeof(T));
return *this;
}
// Init成员函数
template <typename T>
void Image<T>::init(int _w, int _h)
{
w = _w;
h = _h;
data = new T[w * h];
}
// Reinit成员函数
template <typename T>
void Image<T>::reinit(int _w, int _h)
{
w = _w;
h = _h;
if (data) {
delete[] data;
}
data = new T[w * h];
}
// Release成员函数
template <typename T>
void Image<T>::release()
{
w = 0;
h = 0;
if (data) {
delete[] data;
data = nullptr;
}
}
// pgm格式图片读取成员函数
template <typename T>
int Image<T>::read_pgm(const char *filename)
{
FILE *in_file;
char ch, type;
int dummy;
unsigned char *_data;
in_file = fopen(filename, "rb");
if (!in_file) {
fprintf(stderr, "ERROR(0): Fail to open file %s\n", filename);
return -1;
}
// Determine pgm image type (only type three can be used)
ch = getc(in_file);
if (ch != 'P') {
printf("ERROR(1): Not valid pgm/ppm file type\n");
return -1;
}
ch = getc(in_file);
// Convert the one digit integer currently represented as a character to
// an integer(48 == '0')
type = ch - 48;
if (type != 5) {
printf("ERROR(2): this file type (P%d) is not supported!\n", type);
return -1;
}
while (getc(in_file) != '\n')
; // Skip to end of line
while (getc(in_file) == '#') { // Skip comment lines
while (getc(in_file) != '\n')
;
}
fseek(in_file, -1, SEEK_CUR); // Backup one character
fscanf(in_file, "%d", &w);
fscanf(in_file, "%d", &h);
fscanf(in_file, "%d", &dummy); // Skipped here
while (getc(in_file) != '\n')
;
init(w, h);
if (typeid(T) == typeid(unsigned char)) {
fread(data, sizeof(unsigned char), (w) * (h), in_file);
}
else {
_data = new unsigned char[w * h];
fread(_data, sizeof(unsigned char), (w) * (h), in_file);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
data[i * w + j] = (T)(_data[i * w + j]);
}
}
delete[] _data;
}
return 0;
}
// pgm格式图片写入成员函数
template <typename T>
int Image<T>::write_pgm(const char *filename)
{
FILE *out_file;
if (w <= 0 || h <= 0) {
fprintf(stderr, "write_pgm(%s):Invalid image width or height\n",
filename);
return -1;
}
out_file = fopen(filename, "wb");
if (!out_file) {
fprintf(stderr, "Fail to open file: %s\n", filename);
return -1;
}
fprintf(out_file, "P5\n");
fprintf(out_file, "%d %d\n255\n", w, h);
Image<unsigned char> tmpImage;
tmpImage = this->to_uchar();
fwrite(tmpImage.data, sizeof(unsigned char), w * h, out_file);
fclose(out_file);
return 0;
}
// 图像格式变换 to_unchar
template <typename T>
Image<unsigned char> Image<T>::to_uchar() const
{
Image<unsigned char> dstImage(w, h);
for (int r = 0; r < h; r++) {
for (int c = 0; c < w; c++) {
// Negative number, truncate to zero.
float temp = data[r * w + c];
dstImage.data[r * w + c] = temp >= 0 ? (unsigned char)temp : 0;
}
}
return dstImage;
}
// 图片格式变换 to_float
template <typename T>
Image<float> Image<T>::to_float() const
{
Image<float> dstImage(w, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dstImage.data[i * w + j] = (float)this->data[i * w + j];
}
}
return dstImage;
}
// 2x 上采样
// Upsample the image by 2x, linear interpolation.
template <typename T>
Image<T> Image<T>::upsample_2x() const
{
float scale = 2.0f;
int srcW = w, srcH = h;
int dstW = srcW << 1, dstH = srcH << 1;
Image<T> out_image(dstW, dstH);
T *srcData = data;
T *dstData = out_image.data;
for (int r = 0; r < dstH; r++) {
for (int c = 0; c < dstW; c++) {
float ori_r = r / scale;
float ori_c = c / scale;
int r1 = (int)ori_r;
int c1 = (int)ori_c;
float dr = ori_r - r1;
float dc = ori_c - c1;
int idx = r1 * srcW + c1;
dstData[r * dstW + c] =
(unsigned char)((1 - dr) * (1 - dc) * srcData[idx] +
dr * (1 - dc) *
(r1 < srcH - 1 ? srcData[idx + srcW]
: srcData[idx]) +
(1 - dr) * dc *
(c1 < srcW - 1 ? srcData[idx + 1]
: srcData[idx]) +
dr * dc *
((c1 < srcW - 1 && r1 < srcH - 1)
? srcData[idx + srcW + 1]
: srcData[idx]));
}
}
return out_image;
}
// 2x 下采样
// Downsample the image by 2x, nearest neighbor interpolation.
template <typename T>
Image<T> Image<T>::downsample_2x() const
{
int srcW = w, srcH = h;
int dstW = srcW >> 1, dstH = srcH >> 1;
Image<T> out_image(dstW, dstH);
T *srcData = data;
T *dstData = out_image.data;
for (int r = 0; r < dstH; r++) {
for (int c = 0; c < dstW; c++) {
int ori_r = r << 1;
int ori_c = c << 1;
dstData[r * dstW + c] = srcData[ori_r * srcW + ori_c];
}
}
return out_image;
}
} // end namespace sift
#endif