-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlib_image_from_array.h
388 lines (294 loc) · 15.6 KB
/
dlib_image_from_array.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#pragma once
// This function is a modification of the code of dlib png loader: http://dlib.net/dlib/image_loader/png_loader.h.html
// I've left original code commented out to make comparison easier
// PNG save code http://dlib.net/dlib/external/libpng/pngwutil.c.html supports more formats, line 16-bit 4-channels, you may try to add these types if you can find in dlib souce how they are processed
// This code ( and original dlib load_png() ) does not seem to support 16-bit, so all input 16-bit array data is converted to 8-bit (except non-alpha grayscale one, it seems to work)
// It accepts image is single one-dimensional contiguous integer array, but you can easily modify it for your data type,
// Just replace 'int* original_img' with your type and rewrite function navigate_in_array() to return the pointer to the array[4] which contains integer r,g,b,a pixel data
#include <vector>
#include <dlib/image_loader/png_loader.h>
using namespace dlib;
// ----------------------------------------------------------------------------------------
// i <-> rows, j <-> cols
int* navigate_in_array(unsigned& i, unsigned& j, int* array, unsigned& rows, unsigned& cols, unsigned& channels){ //do not copy anything to save time
//return pointer to the pixel at img[i][j]
//row and column indexing starts from 0 here!
return &array[(cols*i+j)*channels];
}
//Different types of input using template function, just like in http://dlib.net/dlib/image_loader/png_loader.h.html
template <typename image_type>
//original_img is integer array of length RowsxColsxPixel_array_length
// One simplification: if imported image has alpha channel, then it is present for all pixels
// BGR is needed for OpenCV which follows its rules in Python as well, so beware when you will receive arrays with Python cv2 image data
// Or you can play with Dlib "bgr_pixel" type
void load_png_to_dlib_img(int* original_img, unsigned height_, unsigned width_, unsigned bit_depth_, unsigned channels, image_type& image, bool bgr = 0){
// porting the code
bool is_gray = (channels == 1);
bool is_graya = (channels == 2);
bool is_rgb = (channels == 3);
bool is_rgba = (channels == 4);
image_view<image_type> t(image);
//setting dimensions
t.set_size( height_, width_ );
if (is_gray && bit_depth_ == 8)
{
unsigned char p; //image element, there is only one in non-alpha gray case
for ( unsigned n = 0; n < height_;n++ )
{
//const unsigned char* v = get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
//unsigned char p = v[m];
unsigned char p = static_cast<unsigned char> (*navigate_in_array(n,m,original_img,height_, width_, channels)); //casting image element to the specified type
assign_pixel( t[n][m], p );
}
}
}
else if (is_gray && bit_depth_ == 16)
{
dlib::uint16 p;
for ( unsigned n = 0; n < height_;n++ )
{
//const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
//dlib::uint16 p = v[m];
p = static_cast<dlib::uint16> (*navigate_in_array(n,m,original_img,height_, width_, channels)); //casting image element to the specified type
assign_pixel( t[n][m], p );
}
}
}
else if (is_graya && bit_depth_ == 8) //here we have two values to read
{
for ( unsigned n = 0; n < height_;n++ )
{
//const unsigned char* v = get_row( n );
for ( unsigned m = 0; m < width_; m++ )
{
/*
unsigned char p = v[m*2];
if (!pixel_traits<pixel_type>::has_alpha)
{
assign_pixel( t[n][m], p );
}
else
{
unsigned char pa = v[m*2+1];
rgb_alpha_pixel pix;
assign_pixel(pix, p);
assign_pixel(pix.alpha, pa);
assign_pixel(t[n][m], pix);
}
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
rgb_alpha_pixel pix;
assign_pixel(pix, static_cast<unsigned char>(addr[0]));
assign_pixel(pix.alpha, static_cast<unsigned char>(addr[1])); //unsafe thing! But we hope user gave us correct dimensions
assign_pixel(t[n][m], pix);
}
}
}
else if (is_graya && bit_depth_ == 16)
{
for ( unsigned n = 0; n < height_;n++ )
{
//const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_; m++ )
{
// This their code should not work:
// according to http://dlib.net/dlib/pixel.h.html#rgb_alpha_pixel
// rgb_alpha_pixel is
/*
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
*/
//Their code:
/*
dlib::uint16 p = v[m*2];
if (!pixel_traits<pixel_type>::has_alpha)
{
assign_pixel( t[n][m], p );
}
else
{
dlib::uint16 pa = v[m*2+1];
rgb_alpha_pixel pix;
assign_pixel(pix, p);
assign_pixel(pix.alpha, pa);
assign_pixel(t[n][m], pix);
}
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
// converting to 8-bit
rgb_alpha_pixel pix;
assign_pixel(pix,static_cast<unsigned char>(addr[0]/255.0));
assign_pixel(pix.alpha, static_cast<unsigned char>(addr[1]/255.0)); //unsafe thing! But we hope user gave us correct dimensions
assign_pixel(t[n][m], pix);
}
}
}
else if (is_rgb && bit_depth_ == 8)
{
for ( unsigned n = 0; n < height_;n++ )
{
//const unsigned char* v = get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
/*
rgb_pixel p;
p.red = v[m*3];
p.green = v[m*3+1];
p.blue = v[m*3+2];
assign_pixel( t[n][m], p );
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
rgb_pixel p;
if (!bgr){
p.red = static_cast<unsigned char>(addr[0]);
p.green = static_cast<unsigned char>(addr[1]);
p.blue = static_cast<unsigned char>(addr[2]);
} else {
p.blue = static_cast<unsigned char>(addr[0]);
p.green = static_cast<unsigned char>(addr[1]);
p.red = static_cast<unsigned char>(addr[2]);
}
assign_pixel( t[n][m], p );
}
}
}
else if (is_rgb && bit_depth_ == 16)
{
for ( unsigned n = 0; n < height_;n++ )
{
//const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
/*
rgb_pixel p;
p.red = static_cast<uint8>(v[m*3]);
p.green = static_cast<uint8>(v[m*3+1]);
p.blue = static_cast<uint8>(v[m*3+2]);
assign_pixel( t[n][m], p );
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
rgb_pixel p;
// yep, still 8 there. Dividing by 255 to bring to 8-bit
if (!bgr){
p.red = static_cast<uint8>(addr[0]/255.0);
p.green = static_cast<uint8>(addr[1]/255.0);
p.blue = static_cast<uint8>(addr[2]/255.0);
} else {
p.blue = static_cast<uint8>(addr[0]/255.0);
p.green = static_cast<uint8>(addr[1]/255.0);
p.red = static_cast<uint8>(addr[2]/255.0);
}
assign_pixel( t[n][m], p );
}
}
}
else if (is_rgba && bit_depth_ == 8)
{
/*
if (!pixel_traits<pixel_type>::has_alpha)
assign_all_pixels(t,0);
*/
for ( unsigned n = 0; n < height_;n++ )
{
//const unsigned char* v = get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
/*
rgb_alpha_pixel p;
p.red = v[m*4];
p.green = v[m*4+1];
p.blue = v[m*4+2];
p.alpha = v[m*4+3];
assign_pixel( t[n][m], p );
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
rgb_alpha_pixel p;
if (!bgr){
p.red = static_cast<unsigned char>(addr[0]);
p.green = static_cast<unsigned char>(addr[1]);
p.blue = static_cast<unsigned char>(addr[2]);
p.alpha = static_cast<unsigned char>(addr[3]);
} else {
p.blue = static_cast<unsigned char>(addr[0]);
p.green = static_cast<unsigned char>(addr[1]);
p.red = static_cast<unsigned char>(addr[2]);
p.alpha = static_cast<unsigned char>(addr[3]);
}
assign_pixel( t[n][m], p );
}
}
}
else if (is_rgba && bit_depth_ == 16)
{
/*
if (!pixel_traits<pixel_type>::has_alpha)
assign_all_pixels(t,0);
*/
for ( unsigned n = 0; n < height_;n++ )
{
//const uint16* v = (uint16*)get_row( n );
for ( unsigned m = 0; m < width_;m++ )
{
/*
rgb_alpha_pixel p;
p.red = static_cast<uint8>(v[m*4]);
p.green = static_cast<uint8>(v[m*4+1]);
p.blue = static_cast<uint8>(v[m*4+2]);
p.alpha = static_cast<uint8>(v[m*4+3]);
assign_pixel( t[n][m], p );
*/
int* addr = navigate_in_array(n,m,original_img,height_, width_, channels);
rgb_alpha_pixel p;
if (!bgr){
p.red = static_cast<unsigned char>(addr[0]/255.0);
p.green = static_cast<unsigned char>(addr[1]/255.0);
p.blue = static_cast<unsigned char>(addr[2]/255.0);
p.alpha = static_cast<unsigned char>(addr[3]/255.0);
} else {
p.blue = static_cast<unsigned char>(addr[0]/255.0);
p.green = static_cast<unsigned char>(addr[1]/255.0);
p.red = static_cast<unsigned char>(addr[2]/255.0);
p.alpha = static_cast<unsigned char>(addr[3]/255.0);
}
assign_pixel( t[n][m], p );
}
}
}
}
// ----------------------------------------------------------------------------------------
// example usage
//see also http://dlib.net/imaging.html
/*
RGB
There are two RGB pixel types in dlib, rgb_pixel and bgr_pixel. Each defines a 24bit RGB pixel type. The bgr_pixel is identical to rgb_pixel except that it lays the color channels down in memory in BGR order rather than RGB order and is therefore useful for interfacing with other image processing tools which expect this format (e.g. OpenCV).
*/
matrix<rgb_pixel> return_matrix_of_rgb_pixel(int* original_img, unsigned height_, unsigned width_, unsigned bit_depth_, unsigned channels, bool bgr = 0){
matrix<rgb_pixel> img;
load_png_to_dlib_img(original_img, height_, width_, bit_depth_, channels, img, bgr);
return img;
}
/*
RGB Alpha
The rgb_alpha_pixel is an 8bit per channel RGB pixel with an 8bit alpha channel.
*/
matrix<rgb_alpha_pixel> return_matrix_of_rgb_alpha_pixel(int* original_img, unsigned height_, unsigned width_, unsigned bit_depth_, unsigned channels, bool bgr = 0){
matrix<rgb_alpha_pixel> img;
load_png_to_dlib_img(original_img, height_, width_, bit_depth_, channels, img, bgr);
return img;
}
/*
Grayscale
Any built in scalar type may be used as a grayscale pixel type. For example, unsigned char, int, double, etc.
*/
matrix<dlib::uint16> return_matrix_of_gray16_pixel(int* original_img, unsigned height_, unsigned width_, unsigned bit_depth_, unsigned channels, bool bgr = 0){
matrix<dlib::uint16> img;
load_png_to_dlib_img(original_img, height_, width_, bit_depth_, channels, img, bgr);
return img;
}
//