YARP
Yet Another Robot Platform
Cv-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_CV_CV_INL_H
10 #define YARP_CV_CV_INL_H
11 
12 #include<cassert>
13 
14 namespace yarp {
15 namespace cv {
16 
17 template <typename T>
18 struct type_code;
19 
20 template<> struct type_code<yarp::sig::PixelMono> : std::integral_constant<int, CV_8UC1> {};
21 template<> struct type_code<yarp::sig::PixelMono16> : std::integral_constant<int, CV_16UC1> {};
22 template<> struct type_code<yarp::sig::PixelMonoSigned> : std::integral_constant<int, CV_8SC1> {};
23 template<> struct type_code<yarp::sig::PixelFloat> : std::integral_constant<int, CV_32FC1> {};
24 template<> struct type_code<yarp::sig::PixelInt> : std::integral_constant<int, CV_32SC1> {};
25 template<> struct type_code<yarp::sig::PixelBgr> : std::integral_constant<int, CV_8UC3> {};
26 template<> struct type_code<yarp::sig::PixelRgb> : std::integral_constant<int, CV_8UC3> {};
27 template<> struct type_code<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_8SC3> {};
28 template<> struct type_code<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_32FC3> {};
29 template<> struct type_code<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_32SC3> {};
30 template<> struct type_code<yarp::sig::PixelHsv> : std::integral_constant<int, CV_8UC3> {};
31 template<> struct type_code<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_32FC3> {};
32 template<> struct type_code<yarp::sig::PixelRgba> : std::integral_constant<int, CV_8UC4> {};
33 template<> struct type_code<yarp::sig::PixelBgra> : std::integral_constant<int, CV_8UC4> {};
34 
35 // -1 : no conversion required.
36 template <typename T>
38 
39 template<> struct convert_code_to_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
40 template<> struct convert_code_to_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
41 template<> struct convert_code_to_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
42 template<> struct convert_code_to_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
43 template<> struct convert_code_to_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
44 template<> struct convert_code_to_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
45 template<> struct convert_code_to_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_RGB2BGR> {};
46 template<> struct convert_code_to_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_RGB2BGR> {};
47 template<> struct convert_code_to_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_RGB2BGR> {};
48 template<> struct convert_code_to_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_RGB2BGR> {};
49 template<> struct convert_code_to_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_HSV2BGR> {};
50 template<> struct convert_code_to_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_HSV2BGR> {};
51 template<> struct convert_code_to_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_RGBA2BGRA> {};
52 template<> struct convert_code_to_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
53 
54 template <typename T>
56 
57 template<> struct convert_code_from_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
58 template<> struct convert_code_from_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
59 template<> struct convert_code_from_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
60 template<> struct convert_code_from_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
61 template<> struct convert_code_from_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
62 template<> struct convert_code_from_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
63 template<> struct convert_code_from_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_BGR2RGB> {};
64 template<> struct convert_code_from_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_BGR2RGB> {};
65 template<> struct convert_code_from_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_BGR2RGB> {};
66 template<> struct convert_code_from_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_BGR2RGB> {};
67 template<> struct convert_code_from_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_BGR2HSV> {};
68 template<> struct convert_code_from_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_BGR2HSV> {};
69 template<> struct convert_code_from_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_BGRA2RGBA> {};
70 template<> struct convert_code_from_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
71 
72 
73 } // namespace cv
74 } // namespace yarp
75 
76 
77 template<typename T>
78 ::cv::Mat yarp::cv::toCvMat(yarp::sig::ImageOf<T>& yarpImage)
79 {
80  ::cv::Mat outMat (yarpImage.height(), yarpImage.width(), yarp::cv::type_code<T>::value,
81  yarpImage.getRawImage(), yarpImage.getRowSize()); // RVO
82  if (convert_code_to_cv<T>::value >= 0)
83  {
84  ::cv::cvtColor(outMat, outMat, convert_code_to_cv<T>::value);
85  }
86  return outMat;
87 }
88 
89 
90 template<typename T>
92 {
93  constexpr size_t align_8_bytes = 8;
94  constexpr size_t align_4_bytes = 4;
95 
96  yarp::sig::ImageOf<T> outImg;
97  // Checking cv::Mat::type() compatibility with the T PixelType
98  assert(yarp::cv::type_code<T>::value == cvImage.type());
100  {
101  ::cv::cvtColor(cvImage, cvImage, convert_code_from_cv<T>::value);
102  }
103  // Check the cv::Mat alignment
104  if (cvImage.step % align_8_bytes == 0) {
105  outImg.setQuantum(align_8_bytes);
106  }
107  else if (cvImage.step % align_4_bytes == 0) {
108  outImg.setQuantum(align_4_bytes);
109  }
110  outImg.setExternal(cvImage.data, cvImage.cols, cvImage.rows);
111  return outImg;
112 }
113 
114 #endif // YARP_CV_CV_INL_H
Typed image class.
Definition: Image.h:647
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:510
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:878
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:179
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:534
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
::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:91
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:555
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:441
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
float PixelFloat
Floating point pixel type.
Definition: Image.h:572
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:446
The main, catch-all namespace for YARP.
Definition: environment.h:25
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:525
Packed BGRA pixel type.
Definition: Image.h:500
Floating point HSV pixel type.
Definition: Image.h:623
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:545
Floating point RGB pixel type.
Definition: Image.h:579
Integer RGB pixel type.
Definition: Image.h:601
Signed, packed RGB pixel type.
Definition: Image.h:562
Packed RGB pixel type.
Definition: Image.h:453
Packed RGBA pixel type.
Definition: Image.h:475