Added the toMat() routine for converting from a dlib style image to an

OpenCV cv::Mat image.
This commit is contained in:
Davis King 2012-04-21 22:42:51 -04:00
parent bcea0df438
commit ae73da4438
3 changed files with 69 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#define DLIB_OPEnCV_HEADER
#include "opencv/cv_image.h"
#include "opencv/to_open_cv.h"
#endif // DLIB_OPEnCV_HEADER

37
dlib/opencv/to_open_cv.h Normal file
View File

@ -0,0 +1,37 @@
// 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_H__
#define DLIB_TO_OPEN_Cv_H__
#include "to_open_cv_abstract.h"
#include "../pixel.h"
namespace dlib
{
template <
typename image_type
>
cv::Mat toMat (
image_type& img
)
{
if (img.size() == 0)
return cv::Mat();
typedef typename image_type::type type;
if (pixel_traits<type>::num == 1)
{
return cv::Mat(img.nr(), img.nc(), cv::DataType<type>::type, &img[0][0], img.width_step());
}
else
{
int depth = sizeof(typename pixel_traits<type>::basic_pixel_type)*8;
int channels = pixel_traits<type>::num;
int type CV_MAKETYPE(depth, channels);
return cv::Mat(img.nr(), img.nc(), type, &img[0][0], img.width_step());
}
}
}
#endif // DLIB_TO_OPEN_Cv_H__

View File

@ -0,0 +1,31 @@
// 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_ABSTRACT__
#ifdef DLIB_TO_OPEN_Cv_ABSTRACT__
#include "../pixel.h"
namespace dlib
{
template <
typename image_type
>
cv::Mat toMat (
image_type& img
);
/*!
requires
- image_type == an implementation of dlib/array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> is defined
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_ABSTRACT__