50 #include <opencv2/core/core.hpp> 51 #include <opencv2/imgproc/imgproc.hpp> 64 #if CV_MAJOR_VERSION >= 4 65 # define MIRA_USE_IPL_IMAGE 0 67 #ifndef MIRA_USE_IPL_IMAGE 70 # define MIRA_USE_IPL_IMAGE 0 84 template<
typename TImg>
97 r.
member(
"Depth",
depth,
"The data type's depth (bits/channel)");
99 r.
member(
"Type",
type,
"The data type (signed/unsigned/float)");
135 #if MIRA_USE_IPL_IMAGE > 0 142 ImgBase(
const IplImage* img,
bool copyData =
false) {
143 mData = cv::cvarrToMat(img, copyData);
169 #if ((CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >= 3) || CV_MAJOR_VERSION >= 3) 170 operator const cv::_OutputArray()
const {
176 operator cv::_OutputArray() {
181 operator cv::_InputArray() {
186 operator const cv::_InputArray()
const {
191 #if MIRA_USE_IPL_IMAGE > 0 192 operator IplImage() {
194 return (IplImage)
mData;
198 operator const IplImage()
const {
199 return (IplImage)
mData;
222 if(roiMat.cols!=other.
width()||roiMat.rows!=other.
height())
223 MIRA_THROW(XLogical,
"Target roi and source image width and height must match");
226 assert(CV_MAT_TYPE(other.
mData.flags) == CV_MAT_TYPE(roiMat.flags)&&
227 CV_MAT_CN(other.
mData.flags) == CV_MAT_CN(roiMat.flags) &&
228 CV_MAT_DEPTH(other.
mData.flags) == CV_MAT_DEPTH(roiMat.flags));
232 for(
int y=0; y<roi.height; ++y)
233 memcpy(roiMat.data+y*(
size_t)roiMat.step, other.
data(y),bpl);
245 if(roiMat.cols!=other.
width()||roiMat.rows!=other.
height())
246 MIRA_THROW(XLogical,
"Target roi and source image width and height must match");
248 if(mask.cols!=other.
width()||mask.rows!=other.
height())
249 MIRA_THROW(XLogical,
"Mask and image width and height must match");
252 assert(CV_MAT_TYPE(other.
mData.flags) == CV_MAT_TYPE(roiMat.flags)&&
253 CV_MAT_CN(other.
mData.flags) == CV_MAT_CN(roiMat.flags) &&
254 CV_MAT_DEPTH(other.
mData.flags) == CV_MAT_DEPTH(roiMat.flags));
256 other.
mData.copyTo(roiMat, mask);
270 MIRA_THROW(XLogical,
"Need at least 1 polygon point");
272 std::vector<cv::Point> v;
273 foreach (
const Point2i& p, poly)
274 v.emplace_back(p.x(), p.y());
276 cv::Mat mask(roi.height, roi.width, CV_8U);
277 mask = cv::Scalar(0);
278 const cv::Point* vv[1] = { v.data() };
279 int n[1] = { int(v.size()) };
280 cv::fillPoly(mask, vv, n, 1, cv::Scalar(1));
295 if (other.cols !=
mData.cols || other.rows !=
mData.rows)
298 if (CV_MAT_TYPE(other.flags) != CV_MAT_TYPE(
mData.flags) ||
299 CV_MAT_CN(other.flags) != CV_MAT_CN(
mData.flags) ||
300 CV_MAT_DEPTH(other.flags) != CV_MAT_DEPTH(
mData.flags))
304 for (
int y = 0; y < other.rows; ++y) {
305 const uint8* a = other.ptr(y);
306 const uint8* b =
mData.ptr(y);
307 for (
int x = 0; x < other.cols; ++x) {
332 return mData.empty();
345 return (
size_t)
mData.step;
363 return mData.depth();
371 return mData.channels();
376 return mData.empty();
394 static const char types[8] = {
'U',
'S',
'U',
'S',
'S',
'F',
'F',
'?'};
395 return ImgFormat(8*
mData.elemSize1(),
396 std::string(types+CV_MAT_DEPTH(
mData.flags), 1),
428 for(
int y=0; y<
height(); ++y)
429 memset(
data(y), 0, bpl);
436 return TImg(
mData.clone());
453 template<
typename Derived>
461 r.
member(
"Width",
mData.cols,
"The width of the image");
462 r.
member(
"Height",
mData.rows,
"The height of the image");
464 r.
member(
"Type",
type,
"The data type including number of channels");
479 template<
typename Derived>
481 int cols, rows,
type;
489 r.
member(
"Width", cols,
"The width of the image");
490 r.
member(
"Height", rows,
"The height of the image");
491 r.
member(
"Type",
type,
"The data type including number of channels");
529 template<
typename TImg,
typename TPixel>
572 if (!alwaysCopy && (CV_MAKETYPE(other.type(), other.channels()) == targetType))
576 cv::Mat data(otherData.rows, otherData.cols, targetType);
578 if (other.channels() !=
data.channels()) {
583 cv::Mat tmp(otherData.rows, otherData.cols,
584 CV_MAKETYPE(otherData.type(),
data.channels()));
587 std::vector<int> fromTo(
data.channels() * 2);
590 for (
int k = 0; k <
data.channels(); ++k, ++srcChannel) {
591 fromTo[2 * k] = (srcChannel < otherData.channels() ? srcChannel : 0);
592 fromTo[2 * k + 1] = k;
595 cv::mixChannels(&otherData, 1, &tmp, 1, fromTo.data(),
601 #if ((CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >= 3) || CV_MAJOR_VERSION >= 3) 602 otherData.convertTo(
data, targetType);
608 CvMat src = otherData;
610 cvConvertScale(&src, &dest);
644 return TImg(this->
mData(roi));
653 return TImg(this->
mData(roi));
735 template<
typename Derived>
745 template<
typename Derived>
751 MIRA_THROW(XIO,
"The deserialized image type " 752 "(depth: " << this->
mData.depth() <<
", channels: " 753 << this->
mData.channels() <<
") " 754 "does not match the type of the Img class " 770 return static_cast<TImg*
> (
this);
774 return static_cast<const TImg*
> (
this);
779 return static_cast<cv::Mat_<TPixel>&
> (this->
mData);
784 return static_cast<const cv::Mat_<TPixel>&
> (this->
mData);
796 template<
typename T =
void,
int TChannels = 1>
965 template<
typename TImg>
970 #if MIRA_USE_IPL_IMAGE > 0 978 Img(
const IplImage* img,
bool copyData =
false) :
979 Base(img, copyData) {
991 template<
typename TImg>
1020 template <
typename TPixel,
int TChannels>
1023 return cv::DataType<TPixel>::depth == this->
depth() &&
1065 template <
typename TPixel,
int TChannels>
1069 if(other.hasType<TPixel,TChannels>())
1073 MIRA_THROW(XBadCast,
"Cannot cast this untyped image with of type " <<
1075 " to channel of type " << cv::DataType<TPixel>::depth <<
1081 template <
typename TPixel,
int TChannels>
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
This object can use object tracking internally, but the object tracking system's state remains unchan...
Definition: ReflectControlFlags.h:82
specialized typed image with 1 channel This template class provides convenient way to create an image...
Definition: Img.h:865
bool operator!=(const cv::Mat &other) const
Returns true if two images are different.
Definition: Img.h:320
Img(const cv::Mat &data)
Definition: Img.h:880
Img< uint16, 3 > Img16U3
Image: unsigned 16-bit integers, 3 channel.
Definition: Img.h:1125
std::size_t total() const
Returns the total size of the matrix (rows*cols)
Definition: Img.h:441
ImgPixel< T, TChannels > Pixel
Definition: Img.h:799
Img< double, 3 > Img64F3
Image: double precision floating-point numbers, 3 channel.
Definition: Img.h:1197
bool hasType() const
Returns true, if the untyped image internally has the specified type, i.e.
Definition: Img.h:1021
TImg operator()(const cv::Rect &roi)
returns a ROI out of the image Returns a new image with set ROI on the image data.
Definition: Img.h:643
bool isEmpty() const
returns true if internal Mat is empty
Definition: Img.h:375
Untyped image iterator, that allows to iterate over images or image regions pixel by pixel similar to...
Definition: ImgIterator.h:415
Typedefs for OS independent basic data types.
TPixel & operator()(const cv::Point &p)
access to pixel given by point p
Definition: Img.h:674
std::size_t bytesPerLine() const
Returns the number of bytes per image line.
Definition: Img.h:349
UntypedImgIterator iterator
Definition: Img.h:1029
void assignROI(const cv::Rect &roi, const ImgBase &other)
Copies the content of other into the specified region of interest (ROI) of THIS image.
Definition: Img.h:218
Classes to access pixels of images with variable type and channels.
const TImg * This() const
Definition: Img.h:773
ImgIterator< TPixel > iterator
Definition: Img.h:687
Img< uint8, 4 > Img8U4
Image: unsigned 8-bit integers, 4 channel.
Definition: Img.h:1107
Size2i size() const
Returns size of image data.
Definition: Img.h:354
ImgTypedBase()
standard constructor
Definition: Img.h:543
Img(const Size2i &size)
constructs a new image of given size note: the image data is allocated but not initialized ...
Definition: Img.h:907
ImgBase(const ImgBase &other)
Copy constructor, that creates a shallow copy.
Definition: Img.h:154
bool empty() const
returns true if internal Mat is empty
Definition: Img.h:331
Img(const cv::Mat &data)
constructor from cv::Mat constructor is public, since we do not have a type safe image ...
Definition: Img.h:986
VersionType version(VersionType version, const T *caller=NULL)
Definition: BinarySerializer.h:267
General point class template.
Definition: Point.h:133
TPixel * operator[](int y)
access to image row
Definition: Img.h:626
ImgBase(const cv::Mat &data)
constructor to initialize internal data from cv::Mat
Definition: Img.h:122
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
bool operator==(const cv::Mat &other) const
Returns true if two images are equal.
Definition: Img.h:293
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:862
void assignMask(const cv::Rect &roi, const ImgBase &other, const cv::Mat &mask)
Copies the content of other into the specified region of interest (ROI) of THIS image, where mask has non-zero values.
Definition: Img.h:241
Self & operator=(const Pixel &p)
assigns image with given Pixel assigns each pixel of the image to the given Pixel ...
Definition: Img.h:852
ImgBase & operator=(const ImgBase &other)
Assignment operator, that usually makes a shallow copy of "other".
Definition: Img.h:208
Size< int, 2 > Size2i
The size type for 2D objects in integer precision.
Definition: Size.h:275
Img< int16, 1 > Img16S1
Image: 16-bit integers, 1 channel.
Definition: Img.h:1137
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
Img< float, 1 > Img32F1
Image: floating-point numbers, 1 channel.
Definition: Img.h:1161
int channels() const
Returns the number of channels of this image.
Definition: Img.h:370
TImg operator()(const cv::Rect &roi) const
returns a ROI out of the image Returns a new image with set ROI on the image data.
Definition: Img.h:652
Img operator()(const cv::Rect &roi) const
returns a ROI out of the image Returns a new image with set ROI on the image data.
Definition: Img.h:1012
Img< double, 2 > Img64F2
Image: double precision floating-point numbers, 2 channel.
Definition: Img.h:1191
Img()
Definition: Img.h:891
Img operator()(const cv::Rect &roi)
returns a ROI out of the image Returns a new image with set ROI on the image data.
Definition: Img.h:1003
iterator begin()
Definition: Img.h:1032
Img(int width, int height)
constructs a new image of given size note: the image data is allocated but not initialized ...
Definition: Img.h:899
Self & operator=(const ImgBase< TImg > &other)
shallow copy
Definition: Img.h:992
Self & operator=(const Pixel &p)
assigns image with given Pixel assigns each pixel of the image to the given Pixel ...
Definition: Img.h:919
const_iterator begin() const
Returns an const iterator to the beginning of this image (image region)
Definition: Img.h:713
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
void reflect(BinarySerializer< Derived > &r)
reflect method for binaryStream serialization This method uses the reflect method of the ImageBase cl...
Definition: Img.h:736
Img(const ImgBase< TImg > &other)
Definition: Img.h:966
bool codec(const T &obj)
Encodes the specified object containing the data using a matching codec.
Definition: BinarySerializer.h:311
Img< float, 2 > Img32F2
Image: floating-point numbers, 2 channel.
Definition: Img.h:1167
Img()
Definition: Img.h:823
int depth() const
Returns the bit-depth of the image E.g.
Definition: Img.h:362
Img< double, 1 > Img64F1
Image: double precision floating-point numbers, 1 channel.
Definition: Img.h:1185
Base class for typed images.
Definition: Img.h:530
ImgTypedBase(const cv::Mat &data)
Definition: Img.h:536
const TPixel & operator()(const cv::Point &p) const
const access to pixel given by point p
Definition: Img.h:681
Img< uint16, 1 > Img16U1
Image: unsigned 16-bit integers, 1 channel.
Definition: Img.h:1113
Img< uint16, 4 > Img16U4
Image: unsigned 16-bit integers, 4 channel.
Definition: Img.h:1131
Img< uint8, 3 > Img8U3
Image: unsigned 8-bit integers, 3 channel.
Definition: Img.h:1101
Img< float, 3 > Img32F3
Image: floating-point numbers, 3 channel.
Definition: Img.h:1173
const_iterator end() const
Returns an const iterator to the end of this image (image region) Note that the end iterator is NOT p...
Definition: Img.h:722
void assignPolygon(const cv::Rect &roi, const ImgBase &other, const Polygon2i &poly)
Copies the content of other into the specified region of interest (ROI) of THIS image, in area covered by polygon.
Definition: Img.h:267
iterator begin()
Returns an iterator to the beginning of this image (image region)
Definition: Img.h:694
Img(int width, int height, int type, int channels)
constructs a new image of given size and type note: the image data is allocated but not initialized ...
Definition: Img.h:951
ImgTypedBase(int width, int height)
constructs a new image using width and height and type from Template type.
Definition: Img.h:552
void reflect(BinarySerializer< Derived > &r)
reflect method for binaryStream serialization This method implements the reflection of width...
Definition: Img.h:454
Binary serializer and deserializer.
ImgFormat format() const
Return the storage format description.
Definition: Img.h:393
ImgConstIterator< TPixel > const_iterator
Definition: Img.h:688
static TImg convertFrom(const cv::Mat &other, bool alwaysCopy=false)
Converts a typed image into another typed image If the channels and/or type of the new image and the ...
Definition: Img.h:568
const TPixel * operator[](int y) const
const access to image row
Definition: Img.h:634
Deserializer that uses BinaryIstream to deserialize the objects from binary format.
Definition: BinarySerializer.h:928
TPixel & operator()(int x, int y)
access pixel(x,y)
Definition: Img.h:660
void resize(int width, int height)
Same as above method.
Definition: Img.h:416
boost::geometry::model::ring< Point2i > Polygon2i
A 2D polygon with integer precision.
Definition: Polygon.h:128
ImgBase()
base constructor internal data is empty.
Definition: Img.h:112
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Img< int16, 3 > Img16S3
Image: 16-bit integers, 3 channel.
Definition: Img.h:1149
void reflect(BinaryDeserializer< Derived > &r)
reflect method for binaryStream deserialization This method uses the reflect method of the ImageBase ...
Definition: Img.h:746
const uint8 * data() const
Returns const pointer to underlying image data.
Definition: Img.h:383
void reflect(JSONSerializer &r)
reflect method for json serialization This method implements the reflection of general data like widt...
Definition: Img.h:509
cv::Mat_< TPixel > & castToMat()
operator for convenience access to image data as Mat
Definition: Img.h:778
TImg & operator=(const TPixel &p)
assigns a specific type to the image data (used to set pixel values)
Definition: Img.h:617
class for typed images.
Definition: Img.h:797
Const image iterator that allows to iterate over images or image regions pixel by pixel similar to it...
Definition: ImgIterator.h:287
int height() const
Definition: Img.h:339
Img< uint8, 1 > Img8U1
Image: unsigned 8-bit integers, 1 channel.
Definition: Img.h:1089
void write(const T *data, std::size_t count)
Definition: BinarySerializer.h:284
int width() const
Definition: Img.h:335
const cv::Mat_< TPixel > & castToMat() const
operator for convenience const access to image data as Mat
Definition: Img.h:783
iterator end()
Returns an iterator to the end of this image (image region) Note that the end iterator is NOT part of...
Definition: Img.h:703
ImgBase(int width, int height, int type)
constructor initialize internal data with width, height, and type note: the image data is allocated b...
Definition: Img.h:131
Simple Wrapper for Boost::geometry polygon.
ImgTypedBase< Self, Pixel > Base
Definition: Img.h:804
void reflect(BinaryDeserializer< Derived > &r)
reflect method for binaryStream deserialization This method implements the reflection for width...
Definition: Img.h:480
void resize(const Size2i &s)
Resizes the image to the specified dimensions (without changing its type, i.e.
Definition: Img.h:408
Img(const Size2i &size, int type, int channels)
constructs a new image of given size and type note: the image data is allocated but not initialized ...
Definition: Img.h:961
VersionType version(VersionType version, const T *caller=NULL)
Definition: BinarySerializer.h:935
Img< int16, 2 > Img16S2
Image: 16-bit integers, 2 channel.
Definition: Img.h:1143
Img< TPixel, TChannels > img_cast(Img<> &other)
Casts an untyped Img<> into a typed image with the specified pixel type and channel count...
Definition: Img.h:1066
This class provides templatized multidimensional sizes for multidimensional geometric objects...
Img(const cv::Mat &data)
Definition: Img.h:812
Img()
Definition: Img.h:941
ImgIterator classes that wrap OpenCV iterators.
const_iterator end() const
Definition: Img.h:1047
uint8 * data()
Returns pointer to underlying image data.
Definition: Img.h:380
void reflect(JSONSerializer &r)
reflect method for json serialization This method uses the reflect method of the ImageBase class ...
Definition: Img.h:763
bool codec(T &obj)
Decodes the specified object from the serialized data.
Definition: BinarySerializer.h:978
Img< T, TChannels > Self
Definition: Img.h:803
ImgTypedBase< Self, Pixel > Base
Definition: Img.h:872
TImg * This()
Definition: Img.h:769
const cv::Mat & getMat() const
Returns const access to internal data.
Definition: Img.h:326
Definition: BinarySerializer.h:257
Untyped const image iterator, that allows to iterate over images or image regions pixel by pixel simi...
Definition: ImgIterator.h:374
Img< T, 1 > Self
Definition: Img.h:871
const uint8 * data(int y) const
Returns const pointer to underlying image data starting at scanline y.
Definition: Img.h:390
ImgBase class.
Definition: Img.h:85
TImg clone() const
Deep copy of image data.
Definition: Img.h:435
#define MIRA_NO_GENERIC_REFLECT_MEMBER(Type)
Exception that is thrown, if a certain type does not support the specified reflector/serializer/deser...
Definition: NoGenericReflect.h:85
Img(const Size2i &size)
constructs a new image of given size note: the image data is allocated but not initialized ...
Definition: Img.h:840
std::size_t step() const
Number of bytes from one row to the next.
Definition: Img.h:344
dynamic ImgPixel class template ImgPixel class providing flexible types and channels.
Definition: ImgPixel.h:110
const uchar * sliceEnd
Definition: ImgIterator.h:160
void read(T *data, std::size_t count)
Definition: BinarySerializer.h:952
UntypedImgConstIterator const_iterator
Definition: Img.h:1030
untyped image class This class provides a fully dynamic image, where type and channels are not known ...
Definition: Img.h:932
Img< uint8, 2 > Img8U2
Image: unsigned 8-bit integers, 2 channels.
Definition: Img.h:1095
Img< uint16, 2 > Img16U2
Image: unsigned 16-bit integers, 2 channel.
Definition: Img.h:1119
Img< int16, 4 > Img16S4
Image: 16-bit integers, 4 channel.
Definition: Img.h:1155
iterator end()
Definition: Img.h:1036
const_iterator begin() const
Definition: Img.h:1043
Img(int width, int height)
constructs a new image of given size note: the image data is allocated but not initialized ...
Definition: Img.h:832
cv::Mat mData
Definition: Img.h:518
const TPixel & operator()(int x, int y) const
const access pixel(x,y)
Definition: Img.h:667
void clear()
Sets each pixel and each channel to zero.
Definition: Img.h:425
Img< double, 4 > Img64F4
Image: double precision floating-point numbers, 4 channel.
Definition: Img.h:1203
Image iterator that allows to iterate over images or image regions pixel by pixel similar to iterator...
Definition: ImgIterator.h:324
const uchar * ptr
Definition: ImgIterator.h:158
Provides the MIRA_NO_GENERIC_REFLECT macros.
Img< float, 4 > Img32F4
Image: floating-point numbers, 4 channel.
Definition: Img.h:1179
uint8 * data(int y)
Returns pointer to underlying image data starting at scanline y.
Definition: Img.h:387
T Pixel
Definition: Img.h:867