Commit 0d428108 authored by Davis King's avatar Davis King

Added the bgr_pixel and cv_image objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403166
parent 6db66130
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_OPEnCV_HEADER
#define DLIB_OPEnCV_HEADER
#include "opencv/cv_image.h"
#endif // DLIB_OPEnCV_HEADER
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_CvIMAGE_H_
#define DLIB_CvIMAGE_H_
#include "cv_image_abstract.h"
#include "../algs.h"
namespace dlib
{
template <
typename pixel_type
>
class cv_image
{
public:
typedef pixel_type type;
cv_image (const IplImage* img)
{
check_image_type(img);
_data = img->imageData;
_widthStep = img->widthStep;
_nr = img->height;
_nc = img->width;
}
cv_image() : _data(0), _widthStep(0), _nr(0), _nc(0) {}
unsigned long size () const { return static_cast<unsigned long>(_nr*_nc); }
inline pixel_type* operator[](const long row )
{
// make sure requires clause is not broken
DLIB_ASSERT(0 <= row && row < nr(),
"\tpixel_type* 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* 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);
}
long nr() const { return _nr; }
long nc() const { return _nc; }
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)
{
check_image_type(img);
_data = img->imageData;
_widthStep = img->widthStep;
_nr = img->height;
_nc = img->width;
return *this;
}
private:
inline void check_image_type (const IplImage* img) const
{
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),
"The pixel type you gave doesn't match the size of pixel used by the open cv image struct");
}
char* _data;
long _widthStep;
long _nr;
long _nc;
};
}
#endif // DLIB_CvIMAGE_H_
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.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 "../algs.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 open cv image you are going to use this object
with. 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.
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.
!*/
public:
typedef pixel_type 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 open cv 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.
!*/
cv_image(
);
/*!
ensures
- #nr() == 0
- #nc() == 0
!*/
~cv_image (
);
/*!
ensures
- This function does nothing. e.g. It doesn't delete the IplImage open cv
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
!*/
unsigned long 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
!*/
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 open cv 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.
- returns #*this
!*/
};
}
#endif // DLIB_OPENCV_IMAGE_AbSTRACT_H_
......@@ -99,6 +99,32 @@ namespace dlib
unsigned char blue;
};
// ----------------------------------------------------------------------------------------
struct bgr_pixel
{
/*!
WHAT THIS OBJECT REPRESENTS
This is a simple struct that represents an BGR colored graphical pixel.
(the reason it exists in addition to the rgb_pixel is so you can lay
it down on top of a memory region that organizes its color data in the
BGR format and still be able to read it)
!*/
bgr_pixel (
) {}
bgr_pixel (
unsigned char blue_,
unsigned char green_,
unsigned char red_
) : blue(blue_), green(green_), red(red_) {}
unsigned char blue;
unsigned char green;
unsigned char red;
};
// ----------------------------------------------------------------------------------------
struct rgb_alpha_pixel
......@@ -264,6 +290,26 @@ namespace dlib
provides deserialization support for the rgb_pixel struct
!*/
// ----------------------------------------------------------------------------------------
inline void serialize (
const bgr_pixel& item,
std::ostream& out
);
/*!
provides serialization support for the bgr_pixel struct
!*/
// ----------------------------------------------------------------------------------------
inline void deserialize (
bgr_pixel& item,
std::istream& in
);
/*!
provides deserialization support for the bgr_pixel struct
!*/
// ----------------------------------------------------------------------------------------
inline void serialize (
......@@ -318,6 +364,20 @@ namespace dlib
const static bool has_alpha = false;
};
// ----------------------------------------------------------------------------------------
template <>
struct pixel_traits<bgr_pixel>
{
const static bool rgb = true;
const static bool rgb_alpha = false;
const static bool grayscale = false;
const static bool hsi = false;
const static long num = 3;
static unsigned long max() { return 255;}
const static bool has_alpha = false;
};
// ----------------------------------------------------------------------------------------
template <>
......@@ -1061,6 +1121,44 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
inline void serialize (
const bgr_pixel& item,
std::ostream& out
)
{
try
{
serialize(item.red,out);
serialize(item.green,out);
serialize(item.blue,out);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while serializing object of type bgr_pixel");
}
}
// ----------------------------------------------------------------------------------------
inline void deserialize (
bgr_pixel& item,
std::istream& in
)
{
try
{
deserialize(item.red,in);
deserialize(item.green,in);
deserialize(item.blue,in);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while deserializing object of type bgr_pixel");
}
}
// ----------------------------------------------------------------------------------------
inline void serialize (
......
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