Commit bcea0df4 authored by Davis King's avatar Davis King

Updated the cv_image object so it works with cv::Mat as well as IplImage.

parent a5047222
......@@ -5,6 +5,7 @@
#include "cv_image_abstract.h"
#include "../algs.h"
#include "../pixel.h"
namespace dlib
{
......@@ -18,15 +19,25 @@ namespace dlib
typedef pixel_type type;
typedef default_memory_manager mem_manager_type;
cv_image (const IplImage* img)
cv_image (const cv::Mat img)
{
check_image_type(img);
_data = img->imageData;
_widthStep = img->widthStep;
_nr = img->height;
_nc = img->width;
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.");
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) {}
unsigned long size () const { return static_cast<unsigned long>(_nr*_nc); }
......@@ -35,7 +46,7 @@ namespace dlib
{
// make sure requires clause is not broken
DLIB_ASSERT(0 <= row && row < nr(),
"\tpixel_type* operator[](row)"
"\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()
......@@ -49,7 +60,7 @@ namespace dlib
{
// make sure requires clause is not broken
DLIB_ASSERT(0 <= row && row < nr(),
"\tconst pixel_type* operator[](row)"
"\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()
......@@ -74,26 +85,36 @@ namespace dlib
cv_image& operator=( const IplImage* img)
{
check_image_type(img);
_data = img->imageData;
_widthStep = img->widthStep;
_nr = img->height;
_nc = img->width;
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:
inline void check_image_type (const IplImage*
#ifdef ENABLE_ASSERTS
img // the #ifdef is here to avoid an unused warning argument from the compiler
#endif
) const
void init (const IplImage* img)
{
DLIB_ASSERT( img->dataOrder == 0, "Only interleaved color channels are supported with cv_image");
DLIB_ASSERT((img->depth&0xFF)/8*img->nChannels == sizeof(pixel_type),
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;
......
......@@ -4,6 +4,7 @@
#ifdef DLIB_OPENCV_IMAGE_AbSTRACT_H_
#include "../algs.h"
#include "../pixel.h"
namespace dlib
{
......@@ -16,22 +17,26 @@ namespace dlib
/*!
REQUIREMENTS ON pixel_type
pixel_type just needs to be something that matches the pixel memory
layout of whatever open cv image you are going to use this object
with. For example, you might use unsigned char or bgr_pixel depending
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. Using this class template you can turn an IplImage
object into something that looks like a normal dlib style image object.
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 IplImage pointer
you give to it. This means you must still remember to free this pointer
yourself.
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:
......@@ -47,12 +52,47 @@ namespace dlib
(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 open cv image)
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 open cv image.
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(
......@@ -67,7 +107,7 @@ namespace dlib
);
/*!
ensures
- This function does nothing. e.g. It doesn't delete the IplImage open cv
- This function does nothing. e.g. It doesn't delete the OpenCV
image used by this cv_image object
!*/
......@@ -133,12 +173,49 @@ namespace dlib
(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 open cv image)
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 open cv image.
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
!*/
......
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