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
|
// Copyright (C) 2014 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_PYTHON_NuMPY_IMAGE_Hh_
#define DLIB_PYTHON_NuMPY_IMAGE_Hh_
#include "numpy.h"
#include <dlib/pixel.h>
#include <dlib/matrix.h>
#include <dlib/array.h>
// ----------------------------------------------------------------------------------------
class numpy_gray_image
{
public:
numpy_gray_image() : _data(0), _nr(0), _nc(0) {}
numpy_gray_image (py::object& img)
{
long shape[2];
get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
_nr = shape[0];
_nc = shape[1];
}
friend inline long num_rows(const numpy_gray_image& img) { return img._nr; }
friend inline long num_columns(const numpy_gray_image& img) { return img._nc; }
friend inline void* image_data(numpy_gray_image& img) { return img._data; }
friend inline const void* image_data(const numpy_gray_image& img) { return img._data; }
friend inline long width_step(const numpy_gray_image& img) { return img._nc*sizeof(unsigned char); }
private:
unsigned char* _data;
dlib::array<unsigned char> _contig_buf;
long _nr;
long _nc;
};
namespace dlib
{
template <>
struct image_traits<numpy_gray_image >
{
typedef unsigned char pixel_type;
};
}
// ----------------------------------------------------------------------------------------
inline bool is_gray_python_image (py::object& img)
{
try
{
long shape[2];
get_numpy_ndarray_shape(img, shape);
return true;
}
catch (dlib::error&)
{
return false;
}
}
// ----------------------------------------------------------------------------------------
class numpy_rgb_image
{
public:
numpy_rgb_image() : _data(0), _nr(0), _nc(0) {}
numpy_rgb_image (py::object& img)
{
long shape[3];
get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
_nr = shape[0];
_nc = shape[1];
if (shape[2] != 3)
throw dlib::error("Error, python object is not a three band image and therefore can't be a RGB image.");
}
friend inline long num_rows(const numpy_rgb_image& img) { return img._nr; }
friend inline long num_columns(const numpy_rgb_image& img) { return img._nc; }
friend inline void* image_data(numpy_rgb_image& img) { return img._data; }
friend inline const void* image_data(const numpy_rgb_image& img) { return img._data; }
friend inline long width_step(const numpy_rgb_image& img) { return img._nc*sizeof(dlib::rgb_pixel); }
private:
dlib::rgb_pixel* _data;
dlib::array<dlib::rgb_pixel> _contig_buf;
long _nr;
long _nc;
};
namespace dlib
{
template <>
struct image_traits<numpy_rgb_image >
{
typedef rgb_pixel pixel_type;
};
}
// ----------------------------------------------------------------------------------------
inline bool is_rgb_python_image (py::object& img)
{
try
{
long shape[3];
get_numpy_ndarray_shape(img, shape);
if (shape[2] == 3)
return true;
return false;
}
catch (dlib::error&)
{
return false;
}
}
// ----------------------------------------------------------------------------------------
#endif // DLIB_PYTHON_NuMPY_IMAGE_Hh_
|