From 78aeaca9371d1c6c5df1a7a13f3fc2d7d18b34d3 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 9 Feb 2014 12:56:13 -0500 Subject: [PATCH] Added tools for interfacing with python images. --- dlib/python.h | 2 + dlib/python/numpy_image.h | 140 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 dlib/python/numpy_image.h diff --git a/dlib/python.h b/dlib/python.h index 1f0c0bf8e..4b2580c4f 100644 --- a/dlib/python.h +++ b/dlib/python.h @@ -6,6 +6,8 @@ #include "python/boost_python_utils.h" #include "python/pyassert.h" #include "python/serialize_pickle.h" +#include "python/numpy.h" +#include "python/numpy_image.h" #endif // DLIB_PYTHoN_TOP_ diff --git a/dlib/python/numpy_image.h b/dlib/python/numpy_image.h new file mode 100644 index 000000000..0b92e1e3a --- /dev/null +++ b/dlib/python/numpy_image.h @@ -0,0 +1,140 @@ +// 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_H__ +#define DLIB_PYTHON_NuMPY_IMAGE_H__ + +#include "numpy.h" +#include +#include + + +// ---------------------------------------------------------------------------------------- + +class numpy_gray_image +{ +public: + typedef unsigned char type; + typedef dlib::default_memory_manager mem_manager_type; + + numpy_gray_image() : _data(0), _nr(0), _nc(0) {} + numpy_gray_image (boost::python::object& img) + { + long shape[2]; + get_numpy_ndarray_parts(img, _data, shape); + _nr = shape[0]; + _nc = shape[1]; + } + + unsigned long size () const { return static_cast(_nr*_nc); } + + inline type* operator[](const long row ) + { return _data + _nc*row; } + + inline const type* operator[](const long row ) const + { return _data + _nc*row; } + + long nr() const { return _nr; } + long nc() const { return _nc; } + long width_step() const { return nc()*sizeof(type); } + +private: + + type* _data; + long _nr; + long _nc; +}; + +// ---------------------------------------------------------------------------------------- + +inline const dlib::matrix_op > mat ( + const numpy_gray_image& m +) +{ + using namespace dlib; + typedef op_array2d_to_mat op; + return matrix_op(op(m)); +} + +// ---------------------------------------------------------------------------------------- + +inline bool is_gray_python_image (boost::python::object& img) +{ + try + { + numpy_gray_image temp(img); + return true; + } + catch (dlib::error&) + { + return false; + } +} + +// ---------------------------------------------------------------------------------------- + +class numpy_rgb_image +{ +public: + typedef dlib::rgb_pixel type; + typedef dlib::default_memory_manager mem_manager_type; + + numpy_rgb_image() : _data(0), _nr(0), _nc(0) {} + numpy_rgb_image (boost::python::object& img) + { + long shape[3]; + get_numpy_ndarray_parts(img, _data, 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."); + } + + unsigned long size () const { return static_cast(_nr*_nc); } + + inline type* operator[](const long row ) + { return _data + _nc*row; } + + inline const type* operator[](const long row ) const + { return _data + _nc*row; } + + long nr() const { return _nr; } + long nc() const { return _nc; } + long width_step() const { return nc()*sizeof(type); } + +private: + + type* _data; + long _nr; + long _nc; +}; + +// ---------------------------------------------------------------------------------------- + +inline const dlib::matrix_op > mat ( + const numpy_rgb_image& m +) +{ + using namespace dlib; + typedef op_array2d_to_mat op; + return matrix_op(op(m)); +} + +// ---------------------------------------------------------------------------------------- + +inline bool is_rgb_python_image (boost::python::object& img) +{ + try + { + numpy_rgb_image temp(img); + return true; + } + catch (dlib::error&) + { + return false; + } +} + +// ---------------------------------------------------------------------------------------- + +#endif // DLIB_PYTHON_NuMPY_IMAGE_H__ +