MIRA
ImgPixel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_IMGPIXEL_H_
48 #define _MIRA_IMGPIXEL_H_
49 
50 #include <opencv2/core/core.hpp>
51 
52 #include <image/Color.h>
53 
54 namespace mira {
55 
57 
64 template<typename T, int Channels>
65 class ImgPixelBase {
66 public:
68  T operator[](int i) const {
69  return v[i];
70  }
72  T& operator[](int i) {
73  return v[i];
74  }
75 
77  T operator()(int i) const {
78  return v[i];
79  }
81  T& operator()(int i) {
82  return v[i];
83  }
84 
85 public:
86  bool operator==(const ImgPixelBase& other) const {
87  return v == other.v;
88  }
89 
90 public:
92  operator cv::Scalar() const {
93  return (cv::Scalar) v;
94  }
95 
96 protected:
97  cv::Vec<T, Channels> v;
98 };
99 
101 
109 template<typename T, int Channels>
110 class ImgPixel: public ImgPixelBase<T, Channels> {
111 public:
114  }
115 
117  explicit ImgPixel(const cv::Scalar& s) {
118  this->v = s;
119  }
120 };
121 
125 template<typename T>
126 class ImgPixel<T, 1> : public ImgPixelBase<T, 1> {
127 public:
130  }
131 
133  ImgPixel(T v0) {
134  this->v[0] = v0;
135  }
136 };
137 
139 
143 template<typename T>
144 class ImgPixel<T, 2> : public ImgPixelBase<T, 2> {
145 public:
148  }
149 
151  ImgPixel(T v0, T v1) {
152  this->v[0] = v0;
153  this->v[1] = v1;
154  }
155 };
156 
158 
162 template<typename T>
163 class ImgPixel<T, 3> : public ImgPixelBase<T, 3> {
164 public:
167  }
168 
170  ImgPixel(T v0, T v1, T v2) {
171  this->v[0] = v0;
172  this->v[1] = v1;
173  this->v[2] = v2;
174  }
175 
177  ImgPixel(const ColorBase& color) {
178  Color::RGB c = color.toRGB();
179  this->v[0] = (T) (c.b * 255.0f);
180  this->v[1] = (T) (c.g * 255.0f);
181  this->v[2] = (T) (c.r * 255.0f);
182  }
183 };
184 
188 template<typename T>
189 class ImgPixel<T, 4> : public ImgPixelBase<T, 4> {
190 public:
192  }
194  ImgPixel(T v0, T v1, T v2, T v3) {
195  this->v[0] = v0;
196  this->v[1] = v1;
197  this->v[2] = v2;
198  this->v[3] = v3;
199  }
200 
204  ImgPixel(const ColorBase& color) {
205  cv::Scalar c = color;
206  this->v = c;
207  }
208 
209 };
210 
212 
213 }// namespace mira
214 
216 
217 namespace mira { namespace detail {
218 
219 // make sure depth and type are defined in cv::DataType<ImgPixel>
220 // (deprecated from base classes in OpenCV 3.3):
221 // hierarchy below checks for existence of cv::DataType<Vec>::depth,
222 // cv::DataType<Vec>::type separately and adds them if required,
223 // with original definitions taken from OpenCV
224 
226 
227 template<typename T, int Channels,
228  bool HasDepth = MIRA_HAS_MEMBER_TEMPLATE(T, depth)::value>
230  public cv::DataType<cv::Vec<T, Channels> > {
231 };
232 
233 template<typename T, int Channels>
234 class CVDataTypeVecEnsureDepth<T, Channels, false> :
235  public cv::DataType<cv::Vec<T, Channels> > {
236 
237  typedef cv::DataType<cv::Vec<T, Channels> > Base;
238 public:
239  enum { depth = cv::DataType<typename Base::channel_type>::depth };
240 };
241 
243 
244 template<typename T, int Channels,
245  bool HasType = MIRA_HAS_MEMBER_TEMPLATE(T, type)::value>
247  public CVDataTypeVecEnsureDepth<T, Channels> {
248 };
249 
250 template<typename T, int Channels>
253 
255 public:
256  enum { type = CV_MAKETYPE(Base::depth, Base::channels) };
257 };
258 
259 }}
260 
261 namespace cv {
262 
263 template<typename T, int Channels>
264 class DataType<mira::ImgPixel<T, Channels> > :
266 };
267 
268 }
269 
271 
272 #endif
T operator()(int i) const
access to index i of vector
Definition: ImgPixel.h:77
base interface for all colors in different color spaces The class provides a base interface for all c...
Definition: Color.h:75
bool operator==(const ImgPixelBase &other) const
Definition: ImgPixel.h:86
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
virtual Color::RGB toRGB() const =0
converts the color of every color space to a RGB color.
T & operator()(int i)
const access to index i of vector
Definition: ImgPixel.h:81
float r
Definition: Color.h:152
ImgPixel(T v0, T v1)
constructor setting the values of the pixel
Definition: ImgPixel.h:151
ImgPixel(const ColorBase &color)
constructor that takes ColorBase, internally the cv::Scalar conversion operator of the color is used...
Definition: ImgPixel.h:204
float g
Definition: Color.h:152
#define MIRA_HAS_MEMBER_TEMPLATE(Class, Identifier)
Definition: HasMember.h:140
ImgPixel(const ColorBase &color)
construction from a color of ColorBase
Definition: ImgPixel.h:177
T operator[](int i) const
access to index i of vector
Definition: ImgPixel.h:68
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
ImgPixel(T v0)
constructor setting the value of the pixel
Definition: ImgPixel.h:133
MIRA_MEMBER_DETECTOR(mNoPublicDefaultConstructor)
Definition: ImgPixel.h:229
ImgPixel()
The default constructor.
Definition: ImgPixel.h:129
This file contains color classes for the Img class.
ImgPixel()
The default constructor.
Definition: ImgPixel.h:113
float b
Definition: Color.h:152
ImgPixel()
The default constructor.
Definition: ImgPixel.h:147
ImgPixel(T v0, T v1, T v2, T v3)
constructor setting the values of the pixel
Definition: ImgPixel.h:194
ImgPixel(T v0, T v1, T v2)
constructor setting the values of the pixel
Definition: ImgPixel.h:170
dynamic ImgPixel class template ImgPixel class providing flexible types and channels.
Definition: ImgPixel.h:110
The different color spaces.
Definition: Color.h:104
T & operator[](int i)
const access to index i of vector
Definition: ImgPixel.h:72
ImgPixel(const cv::Scalar &s)
setting cv::Vector from Scalar
Definition: ImgPixel.h:117
ImgPixel()
The default constructor.
Definition: ImgPixel.h:166
ImgPixel()
Definition: ImgPixel.h:191
base interface for ImgPixel classes This class wraps typed cv::Vect to represent an ImagePixel The Ve...
Definition: ImgPixel.h:65
cv::Vec< T, Channels > v
Definition: ImgPixel.h:97