YARP
Yet Another Robot Platform
Cv-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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
yarp::cv::toCvMat
::cv::Mat toCvMat(yarp::sig::ImageOf< T > &yarpImage)
Convert a yarp::sig::ImageOf to a cv::Mat object.
yarp::sig::PixelMonoSigned
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:555
yarp::sig::PixelBgr
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:525
yarp::sig::PixelRgbSigned
Signed, packed RGB pixel type.
Definition: Image.h:562
yarp::sig::PixelBgra
Packed BGRA pixel type.
Definition: Image.h:500
yarp::sig::PixelHsvFloat
Floating point HSV pixel type.
Definition: Image.h:623
yarp::cv::convert_code_to_cv
Definition: Cv-inl.h:37
yarp::sig::PixelRgbInt
Integer RGB pixel type.
Definition: Image.h:601
yarp::sig::ImageOf
Typed image class.
Definition: Image.h:647
yarp::sig::PixelMono
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
yarp::sig::PixelFloat
float PixelFloat
Floating point pixel type.
Definition: Image.h:572
yarp::cv::convert_code_from_cv
Definition: Cv-inl.h:55
yarp::sig::PixelInt
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:446
yarp::cv::fromCvMat
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition: Cv-inl.h:91
yarp::sig::PixelHsv
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:545
yarp::sig::PixelRgba
Packed RGBA pixel type.
Definition: Image.h:475
yarp::sig::PixelRgbFloat
Floating point RGB pixel type.
Definition: Image.h:579
yarp::sig::PixelRgb
Packed RGB pixel type.
Definition: Image.h:453
yarp::sig::PixelMono16
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:441
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::cv::type_code
Definition: Cv-inl.h:18