Commit 30097efb authored by Davis King's avatar Davis King

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

parent ea55f97e
......@@ -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__
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment