diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /ml/dlib/dlib/opencv | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ml/dlib/dlib/opencv')
-rw-r--r-- | ml/dlib/dlib/opencv/cv_image.h | 225 | ||||
-rw-r--r-- | ml/dlib/dlib/opencv/cv_image_abstract.h | 280 | ||||
-rw-r--r-- | ml/dlib/dlib/opencv/to_open_cv.h | 46 | ||||
-rw-r--r-- | ml/dlib/dlib/opencv/to_open_cv_abstract.h | 34 |
4 files changed, 585 insertions, 0 deletions
diff --git a/ml/dlib/dlib/opencv/cv_image.h b/ml/dlib/dlib/opencv/cv_image.h new file mode 100644 index 000000000..5f224d003 --- /dev/null +++ b/ml/dlib/dlib/opencv/cv_image.h @@ -0,0 +1,225 @@ +// Copyright (C) 2009 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_CvIMAGE_H_ +#define DLIB_CvIMAGE_H_ + +#include <opencv2/core/core.hpp> +#include <opencv2/core/types_c.h> +#include "cv_image_abstract.h" +#include "../algs.h" +#include "../pixel.h" +#include "../matrix/matrix_mat.h" +#include "../image_processing/generic_image.h" + +namespace dlib +{ + + template < + typename pixel_type + > + class cv_image + { + public: + typedef pixel_type type; + typedef default_memory_manager mem_manager_type; + + cv_image (const cv::Mat img) + { + DLIB_CASSERT(img.depth() == cv::DataType<typename pixel_traits<pixel_type>::basic_pixel_type>::depth && + img.channels() == pixel_traits<pixel_type>::num, + "The pixel type you gave doesn't match pixel used by the open cv Mat object." + << "\n\t img.depth(): " << img.depth() + << "\n\t img.cv::DataType<typename pixel_traits<pixel_type>::basic_pixel_type>::depth: " + << cv::DataType<typename pixel_traits<pixel_type>::basic_pixel_type>::depth + << "\n\t img.channels(): " << img.channels() + << "\n\t img.pixel_traits<pixel_type>::num: " << pixel_traits<pixel_type>::num + ); + IplImage temp = img; + init(&temp); + } + + cv_image (const IplImage img) + { + init(&img); + } + + cv_image (const IplImage* img) + { + init(img); + } + + cv_image() : _data(0), _widthStep(0), _nr(0), _nc(0) {} + + size_t size () const { return static_cast<size_t>(_nr*_nc); } + + inline pixel_type* operator[](const long row ) + { + // make sure requires clause is not broken + DLIB_ASSERT(0 <= row && row < nr(), + "\tpixel_type* cv_image::operator[](row)" + << "\n\t you have asked for an out of bounds row " + << "\n\t row: " << row + << "\n\t nr(): " << nr() + << "\n\t this: " << this + ); + + return reinterpret_cast<pixel_type*>( _data + _widthStep*row); + } + + inline const pixel_type* operator[](const long row ) const + { + // make sure requires clause is not broken + DLIB_ASSERT(0 <= row && row < nr(), + "\tconst pixel_type* cv_image::operator[](row)" + << "\n\t you have asked for an out of bounds row " + << "\n\t row: " << row + << "\n\t nr(): " << nr() + << "\n\t this: " << this + ); + + return reinterpret_cast<const pixel_type*>( _data + _widthStep*row); + } + + inline const pixel_type& operator()(const long row, const long column) const + { + DLIB_ASSERT(0<= column && column < nc(), + "\tcont pixel_type& cv_image::operator()(const long rown const long column)" + << "\n\t you have asked for an out of bounds column " + << "\n\t column: " << column + << "\n\t nc(): " << nc() + << "\n\t this: " << this + ); + + return (*this)[row][column]; + } + + inline pixel_type& operator()(const long row, const long column) + { + DLIB_ASSERT(0<= column && column < nc(), + "\tcont pixel_type& cv_image::operator()(const long rown const long column)" + << "\n\t you have asked for an out of bounds column " + << "\n\t column: " << column + << "\n\t nc(): " << nc() + << "\n\t this: " << this + ); + + return (*this)[row][column]; + } + + long nr() const { return _nr; } + long nc() const { return _nc; } + long width_step() const { return _widthStep; } + + cv_image& operator=( const cv_image& item) + { + _data = item._data; + _widthStep = item._widthStep; + _nr = item._nr; + _nc = item._nc; + return *this; + } + + cv_image& operator=( const IplImage* img) + { + init(img); + return *this; + } + + cv_image& operator=( const IplImage img) + { + init(&img); + return *this; + } + + cv_image& operator=( const cv::Mat img) + { + IplImage temp = img; + init(&temp); + return *this; + } + + private: + + void init (const IplImage* img) + { + DLIB_CASSERT( img->dataOrder == 0, "Only interleaved color channels are supported with cv_image"); + DLIB_CASSERT((img->depth&0xFF)/8*img->nChannels == sizeof(pixel_type), + "The pixel type you gave doesn't match the size of pixel used by the open cv image struct"); + + _data = img->imageData; + _widthStep = img->widthStep; + _nr = img->height; + _nc = img->width; + + } + + char* _data; + long _widthStep; + long _nr; + long _nc; + }; + +// ---------------------------------------------------------------------------------------- + + template < + typename T + > + const matrix_op<op_array2d_to_mat<cv_image<T> > > mat ( + const cv_image<T>& m + ) + { + typedef op_array2d_to_mat<cv_image<T> > op; + return matrix_op<op>(op(m)); + } + +// ---------------------------------------------------------------------------------------- + +// Define the global functions that make cv_image a proper "generic image" according to +// ../image_processing/generic_image.h + template <typename T> + struct image_traits<cv_image<T> > + { + typedef T pixel_type; + }; + + template <typename T> + inline long num_rows( const cv_image<T>& img) { return img.nr(); } + template <typename T> + inline long num_columns( const cv_image<T>& img) { return img.nc(); } + + template <typename T> + inline void* image_data( + cv_image<T>& img + ) + { + if (img.size() != 0) + return &img[0][0]; + else + return 0; + } + + template <typename T> + inline const void* image_data( + const cv_image<T>& img + ) + { + if (img.size() != 0) + return &img[0][0]; + else + return 0; + } + + template <typename T> + inline long width_step( + const cv_image<T>& img + ) + { + return img.width_step(); + } + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_CvIMAGE_H_ + diff --git a/ml/dlib/dlib/opencv/cv_image_abstract.h b/ml/dlib/dlib/opencv/cv_image_abstract.h new file mode 100644 index 000000000..6fbc56b43 --- /dev/null +++ b/ml/dlib/dlib/opencv/cv_image_abstract.h @@ -0,0 +1,280 @@ +// Copyright (C) 2009 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#undef DLIB_OPENCV_IMAGE_AbSTRACT_H_ +#ifdef DLIB_OPENCV_IMAGE_AbSTRACT_H_ + +#include <opencv2/core/core.hpp> +#include <opencv2/core/types_c.h> +#include "../algs.h" +#include "../pixel.h" + +namespace dlib +{ + + template < + typename pixel_type + > + class cv_image + { + /*! + REQUIREMENTS ON pixel_type + pixel_type just needs to be something that matches the pixel memory + layout of whatever OpenCV image you are going to use with this object. + For example, you might use unsigned char or bgr_pixel depending + on what you needed. + + WHAT THIS OBJECT REPRESENTS + This object is meant to be used as a simple wrapper around the OpenCV + IplImage struct or Mat object. Using this class template you can turn + an OpenCV image into something that looks like a normal dlib style + image object. + + So you should be able to use cv_image objects with many of the image + processing functions in dlib as well as the GUI tools for displaying + images on the screen. + + Note that this object does NOT take ownership of the image data you + give to it. This means it is up to you to make sure the OpenCV image + is properly freed at some point. This also means that an instance of + this object can only be used as long as the OpenCV image it references + remains valid, since a cv_image just points to the OpenCV image's + memory directly. + !*/ + + public: + typedef pixel_type type; + typedef default_memory_manager mem_manager_type; + + cv_image ( + const IplImage* img + ); + /*! + requires + - img->dataOrder == 0 + (i.e. Only interleaved color channels are supported with cv_image) + - (img->depth&0xFF)/8*img->nChannels == sizeof(pixel_type) + (i.e. The size of the pixel_type needs to match the size of the pixels + inside the OpenCV image) + ensures + - #nr() == img->height + #nc() == img->width + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + !*/ + + cv_image ( + const IplImage img + ); + /*! + requires + - img.dataOrder == 0 + (i.e. Only interleaved color channels are supported with cv_image) + - (img.depth&0xFF)/8*img.nChannels == sizeof(pixel_type) + (i.e. The size of the pixel_type needs to match the size of the pixels + inside the OpenCV image) + ensures + - #nr() == img.height + #nc() == img.width + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + !*/ + + cv_image ( + const cv::Mat img + ); + /*! + requires + - img.depth() == cv::DataType<pixel_traits<pixel_type>::basic_pixel_type>::depth + (i.e. The pixel_type template argument needs to match the type of pixel + used inside the OpenCV image) + - img.channels() == pixel_traits<pixel_type>::num + (i.e. the number of channels in the pixel_type needs to match the number of + channels in the OpenCV image) + ensures + - #nr() == img.rows + - #nc() == img.cols + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + !*/ + + cv_image( + ); + /*! + ensures + - #nr() == 0 + - #nc() == 0 + !*/ + + ~cv_image ( + ); + /*! + ensures + - This function does nothing. e.g. It doesn't delete the OpenCV + image used by this cv_image object + !*/ + + long nr( + ) const; + /*! + ensures + - returns the number of rows in this image + !*/ + + long nc( + ) const; + /*! + ensures + - returns the number of columns in this image + !*/ + + size_t size ( + ) const; + /*! + ensures + - returns nr()*nc() + (i.e. returns the number of pixels in this image) + !*/ + + inline pixel_type* operator[] ( + const long row + ); + /*! + requires + - 0 <= row < nr() + ensures + - returns a pointer to the first pixel in the given row + of this image + !*/ + + inline const pixel_type* operator[] ( + const long row + ) const; + /*! + requires + - 0 <= row < nr() + ensures + - returns a pointer to the first pixel in the given row + of this image + !*/ + + inline const pixel_type& operator()( + const long row, const long column + ) const + /*! + requires + - 0 <= row < nr() + - 0 <= column < nc() + ensures + - returns a const reference to the pixel at coordinates (row, column) + of this image + !*/ + + inline pixel_type& operator()( + const long row, const long column + ) + /*! + requires + - 0 <= row < nr() + - 0 <= column < nc() + ensures + - returns a reference to the pixel at coordinates (row, column) + of this image + !*/ + + cv_image& operator= ( + const cv_image& item + ); + /*! + ensures + - #*this is an identical copy of item + - returns #*this + !*/ + + cv_image& operator=( + const IplImage* img + ); + /*! + requires + - img->dataOrder == 0 + (i.e. Only interleaved color channels are supported with cv_image) + - (img->depth&0xFF)/8*img->nChannels == sizeof(pixel_type) + (i.e. The size of the pixel_type needs to match the size of the pixels + inside the OpenCV image) + ensures + - #nr() == img->height + #nc() == img->width + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + - returns #*this + !*/ + + cv_image& operator=( + const IplImage img + ); + /*! + requires + - img->dataOrder == 0 + (i.e. Only interleaved color channels are supported with cv_image) + - (img->depth&0xFF)/8*img->nChannels == sizeof(pixel_type) + (i.e. The size of the pixel_type needs to match the size of the pixels + inside the OpenCV image) + ensures + - #nr() == img->height + #nc() == img->width + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + - returns #*this + !*/ + + cv_image& operator=( + const cv::Mat img + ); + /*! + requires + - img.depth() == cv::DataType<pixel_traits<pixel_type>::basic_pixel_type>::depth + (i.e. The pixel_type template argument needs to match the type of pixel + used inside the OpenCV image) + - img.channels() == pixel_traits<pixel_type>::num + (i.e. the number of channels in the pixel_type needs to match the number of + channels in the OpenCV image) + ensures + - #nr() == img.rows + - #nc() == img.cols + - using the operator[] on this object you will be able to access the pixels + inside this OpenCV image. + - returns #*this + !*/ + + long width_step ( + ) const; + /*! + ensures + - returns the size of one row of the image, in bytes. + More precisely, return a number N such that: + (char*)&item[0][0] + N == (char*)&item[1][0]. + !*/ + }; + +// ---------------------------------------------------------------------------------------- + + template < + typename T + > + const matrix_exp mat ( + const cv_image<T>& img + ); + /*! + ensures + - returns a matrix R such that: + - R.nr() == img.nr() + - R.nc() == img.nc() + - for all valid r and c: + R(r, c) == img[r][c] + !*/ + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_OPENCV_IMAGE_AbSTRACT_H_ + diff --git a/ml/dlib/dlib/opencv/to_open_cv.h b/ml/dlib/dlib/opencv/to_open_cv.h new file mode 100644 index 000000000..02b7bf6fc --- /dev/null +++ b/ml/dlib/dlib/opencv/to_open_cv.h @@ -0,0 +1,46 @@ +// Copyright (C) 2012 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_TO_OPEN_Cv_Hh_ +#define DLIB_TO_OPEN_Cv_Hh_ + +#include <opencv2/core/core.hpp> +#include "to_open_cv_abstract.h" +#include "../pixel.h" +#include "../matrix/matrix.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + template < + typename image_type + > + cv::Mat toMat ( + image_type& img + ) + { + if (image_size(img) == 0) + return cv::Mat(); + + typedef typename image_traits<image_type>::pixel_type type; + typedef typename pixel_traits<type>::basic_pixel_type basic_pixel_type; + if (pixel_traits<type>::num == 1) + { + return cv::Mat(num_rows(img), num_columns(img), cv::DataType<basic_pixel_type>::type, image_data(img), width_step(img)); + } + else + { + int depth = sizeof(typename pixel_traits<type>::basic_pixel_type)*8; + int channels = pixel_traits<type>::num; + int thetype = CV_MAKETYPE(depth, channels); + return cv::Mat(num_rows(img), num_columns(img), thetype, image_data(img), width_step(img)); + } + } + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_TO_OPEN_Cv_Hh_ + diff --git a/ml/dlib/dlib/opencv/to_open_cv_abstract.h b/ml/dlib/dlib/opencv/to_open_cv_abstract.h new file mode 100644 index 000000000..43307e02f --- /dev/null +++ b/ml/dlib/dlib/opencv/to_open_cv_abstract.h @@ -0,0 +1,34 @@ +// Copyright (C) 2012 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#undef DLIB_TO_OPEN_Cv_ABSTRACTh_ +#ifdef DLIB_TO_OPEN_Cv_ABSTRACTh_ + +#include <opencv2/core/core.hpp> +#include "../pixel.h" + +namespace dlib +{ + template < + typename image_type + > + cv::Mat toMat ( + image_type& img + ); + /*! + requires + - image_type == an image object that implements the interface defined in + dlib/image_processing/generic_image.h or a dlib::matrix object which uses a + row_major_layout. + - pixel_traits is defined for the contents of img. + ensures + - returns an OpenCV Mat object which represents the same image as img. This + is done by setting up the Mat object to point to the same memory as img. + Therefore, the returned Mat object is valid only as long as pointers + to the pixels in img remain valid. + !*/ +} + +#endif // DLIB_TO_OPEN_Cv_ABSTRACTh_ + + + |