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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "simpleocv.h"
#if NCNN_SIMPLEOCV
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#if __ARM_NEON
#define STBI_NEON
#endif
#define STBI_NO_THREAD_LOCALS
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_ONLY_BMP
#define STBI_ONLY_PNM
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
namespace cv {
Mat imread(const std::string& path, int flags)
{
int desired_channels = 0;
if (flags == IMREAD_UNCHANGED)
{
desired_channels = 0;
}
else if (flags == IMREAD_GRAYSCALE)
{
desired_channels = 1;
}
else if (flags == IMREAD_COLOR)
{
desired_channels = 3;
}
else
{
// unknown flags
return Mat();
}
int w;
int h;
int c;
unsigned char* pixeldata = stbi_load(path.c_str(), &w, &h, &c, desired_channels);
if (!pixeldata)
{
// load failed
return Mat();
}
if (desired_channels)
{
c = desired_channels;
}
// copy pixeldata to Mat
Mat img;
if (c == 1)
{
img.create(h, w, CV_8UC1);
}
else if (c == 3)
{
img.create(h, w, CV_8UC3);
}
else if (c == 4)
{
img.create(h, w, CV_8UC4);
}
else
{
// unexpected channels
stbi_image_free(pixeldata);
return Mat();
}
memcpy(img.data, pixeldata, w * h * c);
stbi_image_free(pixeldata);
// // resolve exif orientation
// {
// std::ifstream ifs;
// ifs.open(filename.c_str(), std::ifstream::in);
//
// if (ifs.good())
// {
// ExifReader exif_reader(ifs);
// if (exif_reader.parse())
// {
// ExifEntry_t e = exif_reader.getTag(ORIENTATION);
// int orientation = e.field_u16;
// if (orientation >= 1 && orientation <= 8)
// rotate_by_orientation(img, img, orientation);
// }
// }
//
// ifs.close();
// }
// rgb to bgr
if (c == 3)
{
uchar* p = img.data;
for (int i = 0; i < w * h; i++)
{
std::swap(p[0], p[2]);
p += 3;
}
}
if (c == 4)
{
uchar* p = img.data;
for (int i = 0; i < w * h; i++)
{
std::swap(p[0], p[2]);
p += 4;
}
}
return img;
}
Mat imdecode(const std::vector<uchar>& buf, int flags)
{
int desired_channels = 0;
if (flags == IMREAD_UNCHANGED)
{
desired_channels = 0;
}
else if (flags == IMREAD_GRAYSCALE)
{
desired_channels = 1;
}
else if (flags == IMREAD_COLOR)
{
desired_channels = 3;
}
else
{
// unknown flags
return Mat();
}
int w;
int h;
int c;
unsigned char* pixeldata = stbi_load_from_memory(&buf.front(), buf.size(), &w, &h, &c, desired_channels);
if (!pixeldata)
{
// load failed
return Mat();
}
if (desired_channels)
{
c = desired_channels;
}
// copy pixeldata to Mat
Mat img;
if (c == 1)
{
img.create(h, w, CV_8UC1);
}
else if (c == 3)
{
img.create(h, w, CV_8UC3);
}
else if (c == 4)
{
img.create(h, w, CV_8UC4);
}
else
{
// unexpected channels
stbi_image_free(pixeldata);
return Mat();
}
memcpy(img.data, pixeldata, w * h * c);
stbi_image_free(pixeldata);
// rgb to bgr
if (c == 3)
{
uchar* p = img.data;
for (int i = 0; i < w * h; i++)
{
std::swap(p[0], p[2]);
p += 3;
}
}
if (c == 4)
{
uchar* p = img.data;
for (int i = 0; i < w * h; i++)
{
std::swap(p[0], p[2]);
p += 4;
}
}
return img;
}
bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params)
{
const char* _ext = strrchr(path.c_str(), '.');
if (!_ext)
{
// missing extension
return false;
}
std::string ext = _ext;
Mat img = m.clone();
// bgr to rgb
int c = 0;
if (img.type() == CV_8UC1)
{
c = 1;
}
else if (img.type() == CV_8UC3)
{
c = 3;
uchar* p = img.data;
for (int i = 0; i < img.cols * img.rows; i++)
{
std::swap(p[0], p[2]);
p += 3;
}
}
else if (img.type() == CV_8UC4)
{
c = 4;
uchar* p = img.data;
for (int i = 0; i < img.cols * img.rows; i++)
{
std::swap(p[0], p[2]);
p += 4;
}
}
else
{
// unexpected image channels
return false;
}
bool success = false;
if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
{
int quality = 95;
for (size_t i = 0; i < params.size(); i += 2)
{
if (params[i] == IMWRITE_JPEG_QUALITY)
{
quality = params[i + 1];
break;
}
}
success = stbi_write_jpg(path.c_str(), img.cols, img.rows, c, img.data, quality);
}
else if (ext == ".png" || ext == ".PNG")
{
success = stbi_write_png(path.c_str(), img.cols, img.rows, c, img.data, 0);
}
else if (ext == ".bmp" || ext == ".BMP")
{
success = stbi_write_bmp(path.c_str(), img.cols, img.rows, c, img.data);
}
else
{
// unknown extension type
return false;
}
return success;
}
void imshow(const std::string& name, const Mat& m)
{
NCNN_LOGE("imshow save image to %s.png", name.c_str());
imwrite(name + ".png", m);
}
int waitKey(int delay)
{
NCNN_LOGE("waitKey stub");
return -1;
}
#if NCNN_PIXEL
void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags)
{
(void)flags;
int srcw = src.cols;
int srch = src.rows;
int w = size.width;
int h = size.height;
if (w == 0 || h == 0)
{
w = srcw * sw;
h = srch * sh;
}
if (w == 0 || h == 0)
return;
if (w == srcw && h == srch)
{
dst = src.clone();
return;
}
cv::Mat tmp(h, w, src.c);
if (tmp.empty())
return;
if (src.c == 1)
ncnn::resize_bilinear_c1(src.data, srcw, srch, tmp.data, w, h);
else if (src.c == 3)
ncnn::resize_bilinear_c3(src.data, srcw, srch, tmp.data, w, h);
else if (src.c == 4)
ncnn::resize_bilinear_c4(src.data, srcw, srch, tmp.data, w, h);
dst = tmp;
}
#endif // NCNN_PIXEL
#if NCNN_PIXEL_DRAWING
void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness)
{
Rect rec;
rec.x = std::min(pt1.x, pt2.x);
rec.y = std::min(pt1.y, pt2.y);
rec.width = std::max(pt1.x, pt2.x) - rec.x;
rec.height = std::max(pt1.y, pt2.y) - rec.y;
rectangle(img, rec, color, thickness);
}
void rectangle(Mat& img, Rect rec, const Scalar& _color, int thickness)
{
unsigned int color = 0;
unsigned char* border_color = (unsigned char*)&color;
if (img.c == 1)
{
border_color[0] = _color[0];
ncnn::draw_rectangle_c1(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
}
else if (img.c == 3)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
ncnn::draw_rectangle_c3(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
}
else if (img.c == 4)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
border_color[3] = _color[3];
ncnn::draw_rectangle_c4(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
}
}
void circle(Mat& img, Point center, int radius, const Scalar& _color, int thickness)
{
unsigned int color = 0;
unsigned char* border_color = (unsigned char*)&color;
if (img.c == 1)
{
border_color[0] = _color[0];
ncnn::draw_circle_c1(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
}
else if (img.c == 3)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
ncnn::draw_circle_c3(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
}
else if (img.c == 4)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
border_color[3] = _color[3];
ncnn::draw_circle_c4(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
}
}
void line(Mat& img, Point p0, Point p1, const Scalar& _color, int thickness)
{
unsigned int color = 0;
unsigned char* border_color = (unsigned char*)&color;
if (img.c == 1)
{
border_color[0] = _color[0];
ncnn::draw_line_c1(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
}
else if (img.c == 3)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
ncnn::draw_line_c3(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
}
else if (img.c == 4)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
border_color[3] = _color[3];
ncnn::draw_line_c4(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
}
}
void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar _color, int thickness)
{
const int fontpixelsize = 20 * fontScale;
unsigned int color = 0;
unsigned char* border_color = (unsigned char*)&color;
if (img.c == 1)
{
border_color[0] = _color[0];
ncnn::draw_text_c1(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
}
else if (img.c == 3)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
ncnn::draw_text_c3(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
}
else if (img.c == 4)
{
border_color[0] = _color[0];
border_color[1] = _color[1];
border_color[2] = _color[2];
border_color[3] = _color[3];
ncnn::draw_text_c4(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
}
}
Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine)
{
const int fontpixelsize = 20 * fontScale;
int w;
int h;
ncnn::get_text_drawing_size(text.c_str(), fontpixelsize, &w, &h);
*baseLine = 0;
return Size(w, h);
}
#endif // NCNN_PIXEL_DRAWING
} // namespace cv
#endif // NCNN_SIMPLEOCV
|