Made toMat() work with matrix object in addition to array2d style images.

This commit is contained in:
Davis King 2012-12-22 18:24:59 -05:00
parent ea55f97ef4
commit 30097efba1
2 changed files with 40 additions and 3 deletions

View File

@ -5,9 +5,13 @@
#include "to_open_cv_abstract.h"
#include "../pixel.h"
#include "../matrix/matrix.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename image_type
>
@ -27,10 +31,42 @@ namespace dlib
{
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, (void*)&img[0][0], img.width_step());
int thetype = CV_MAKETYPE(depth, channels);
return cv::Mat(img.nr(), img.nc(), thetype, (void*)&img[0][0], img.width_step());
}
}
// ----------------------------------------------------------------------------------------
template <
typename T,
long NR,
long NC,
typename MM
>
cv::Mat toMat (
matrix<T,NR,NC,MM>& img
)
{
if (img.size() == 0)
return cv::Mat();
typedef T type;
if (pixel_traits<type>::num == 1)
{
return cv::Mat(img.nr(), img.nc(), cv::DataType<type>::type, (void*)&img(0,0), img.nc()*sizeof(type));
}
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(img.nr(), img.nc(), thetype, (void*)&img(0,0), img.nc()*sizeof(type));
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_TO_OPEN_Cv_H__

View File

@ -15,7 +15,8 @@ namespace dlib
);
/*!
requires
- image_type == an implementation of dlib/array2d/array2d_kernel_abstract.h
- image_type == an implementation of dlib/array2d/array2d_kernel_abstract.h or
a dlib::matrix object which uses a row_major_layout.
- pixel_traits<typename image_type::type> is defined
ensures
- returns an OpenCV Mat object which represents the same image as img. This