YARP
Yet Another Robot Platform
Cv-inl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_CV_CV_INL_H
7 #define YARP_CV_CV_INL_H
8 
9 #include<cassert>
10 
11 namespace yarp {
12 namespace cv {
13 
14 template <typename T>
15 struct type_code;
16 
17 template<> struct type_code<yarp::sig::PixelMono> : std::integral_constant<int, CV_8UC1> {};
18 template<> struct type_code<yarp::sig::PixelMono16> : std::integral_constant<int, CV_16UC1> {};
19 template<> struct type_code<yarp::sig::PixelMonoSigned> : std::integral_constant<int, CV_8SC1> {};
20 template<> struct type_code<yarp::sig::PixelFloat> : std::integral_constant<int, CV_32FC1> {};
21 template<> struct type_code<yarp::sig::PixelInt> : std::integral_constant<int, CV_32SC1> {};
22 template<> struct type_code<yarp::sig::PixelBgr> : std::integral_constant<int, CV_8UC3> {};
23 template<> struct type_code<yarp::sig::PixelRgb> : std::integral_constant<int, CV_8UC3> {};
24 template<> struct type_code<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_8SC3> {};
25 template<> struct type_code<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_32FC3> {};
26 template<> struct type_code<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_32SC3> {};
27 template<> struct type_code<yarp::sig::PixelHsv> : std::integral_constant<int, CV_8UC3> {};
28 template<> struct type_code<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_32FC3> {};
29 template<> struct type_code<yarp::sig::PixelRgba> : std::integral_constant<int, CV_8UC4> {};
30 template<> struct type_code<yarp::sig::PixelBgra> : std::integral_constant<int, CV_8UC4> {};
31 
32 // -1 : no conversion required.
33 template <typename T>
35 
36 template<> struct convert_code_to_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
37 template<> struct convert_code_to_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
38 template<> struct convert_code_to_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
39 template<> struct convert_code_to_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
40 template<> struct convert_code_to_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
41 template<> struct convert_code_to_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
42 template<> struct convert_code_to_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_RGB2BGR> {};
43 template<> struct convert_code_to_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_RGB2BGR> {};
44 template<> struct convert_code_to_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_RGB2BGR> {};
45 template<> struct convert_code_to_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_RGB2BGR> {};
46 template<> struct convert_code_to_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_HSV2BGR> {};
47 template<> struct convert_code_to_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_HSV2BGR> {};
48 template<> struct convert_code_to_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_RGBA2BGRA> {};
49 template<> struct convert_code_to_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
50 
51 template <typename T>
53 
54 template<> struct convert_code_from_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
55 template<> struct convert_code_from_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
56 template<> struct convert_code_from_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
57 template<> struct convert_code_from_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
58 template<> struct convert_code_from_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
59 template<> struct convert_code_from_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
60 template<> struct convert_code_from_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_BGR2RGB> {};
61 template<> struct convert_code_from_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_BGR2RGB> {};
62 template<> struct convert_code_from_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_BGR2RGB> {};
63 template<> struct convert_code_from_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_BGR2RGB> {};
64 template<> struct convert_code_from_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_BGR2HSV> {};
65 template<> struct convert_code_from_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_BGR2HSV> {};
66 template<> struct convert_code_from_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_BGRA2RGBA> {};
67 template<> struct convert_code_from_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
68 
69 
70 } // namespace cv
71 } // namespace yarp
72 
73 
74 template<typename T>
75 ::cv::Mat yarp::cv::toCvMat(yarp::sig::ImageOf<T>& yarpImage)
76 {
77  ::cv::Mat outMat (yarpImage.height(), yarpImage.width(), yarp::cv::type_code<T>::value,
78  yarpImage.getRawImage(), yarpImage.getRowSize()); // RVO
79  if (convert_code_to_cv<T>::value >= 0)
80  {
81  ::cv::cvtColor(outMat, outMat, convert_code_to_cv<T>::value);
82  }
83  return outMat;
84 }
85 
86 
87 template<typename T>
89 {
90  constexpr size_t align_8_bytes = 8;
91  constexpr size_t align_4_bytes = 4;
92 
93  yarp::sig::ImageOf<T> outImg;
94  // Checking cv::Mat::type() compatibility with the T PixelType
95  assert(yarp::cv::type_code<T>::value == cvImage.type());
97  {
98  ::cv::cvtColor(cvImage, cvImage, convert_code_from_cv<T>::value);
99  }
100  // Check the cv::Mat alignment
101  if (cvImage.step % align_8_bytes == 0) {
102  outImg.setQuantum(align_8_bytes);
103  }
104  else if (cvImage.step % align_4_bytes == 0) {
105  outImg.setQuantum(align_4_bytes);
106  }
107  outImg.setExternal(cvImage.data, cvImage.cols, cvImage.rows);
108  return outImg;
109 }
110 
111 #endif // YARP_CV_CV_INL_H
Typed image class.
Definition: Image.h:658
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:501
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:903
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:192
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:541
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
::cv::Mat toCvMat(yarp::sig::ImageOf< T > &yarpImage)
Convert a yarp::sig::ImageOf to a cv::Mat object.
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition: Cv-inl.h:88
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:566
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:452
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:447
float PixelFloat
Floating point pixel type.
Definition: Image.h:583
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:457
The main, catch-all namespace for YARP.
Definition: dirs.h:16
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:536
Packed BGRA pixel type.
Definition: Image.h:511
Floating point HSV pixel type.
Definition: Image.h:634
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:556
Floating point RGB pixel type.
Definition: Image.h:590
Integer RGB pixel type.
Definition: Image.h:612
Signed, packed RGB pixel type.
Definition: Image.h:573
Packed RGB pixel type.
Definition: Image.h:464
Packed RGBA pixel type.
Definition: Image.h:486