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
11namespace yarp::cv {
12
13template <typename T>
14struct type_code;
15
16template<> struct type_code<yarp::sig::PixelMono> : std::integral_constant<int, CV_8UC1> {};
17template<> struct type_code<yarp::sig::PixelMono16> : std::integral_constant<int, CV_16UC1> {};
18template<> struct type_code<yarp::sig::PixelMonoSigned> : std::integral_constant<int, CV_8SC1> {};
19template<> struct type_code<yarp::sig::PixelFloat> : std::integral_constant<int, CV_32FC1> {};
20template<> struct type_code<yarp::sig::PixelInt> : std::integral_constant<int, CV_32SC1> {};
21template<> struct type_code<yarp::sig::PixelBgr> : std::integral_constant<int, CV_8UC3> {};
22template<> struct type_code<yarp::sig::PixelRgb> : std::integral_constant<int, CV_8UC3> {};
23template<> struct type_code<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_8SC3> {};
24template<> struct type_code<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_32FC3> {};
25template<> struct type_code<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_32SC3> {};
26template<> struct type_code<yarp::sig::PixelHsv> : std::integral_constant<int, CV_8UC3> {};
27template<> struct type_code<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_32FC3> {};
28template<> struct type_code<yarp::sig::PixelRgba> : std::integral_constant<int, CV_8UC4> {};
29template<> struct type_code<yarp::sig::PixelBgra> : std::integral_constant<int, CV_8UC4> {};
30
31// -1 : no conversion required.
32template <typename T>
34
35template<> struct convert_code_to_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
36template<> struct convert_code_to_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
37template<> struct convert_code_to_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
38template<> struct convert_code_to_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
39template<> struct convert_code_to_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
40template<> struct convert_code_to_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
41template<> struct convert_code_to_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_RGB2BGR> {};
42template<> struct convert_code_to_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_RGB2BGR> {};
43template<> struct convert_code_to_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_RGB2BGR> {};
44template<> struct convert_code_to_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_RGB2BGR> {};
45template<> struct convert_code_to_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_HSV2BGR> {};
46template<> struct convert_code_to_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_HSV2BGR> {};
47template<> struct convert_code_to_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_RGBA2BGRA> {};
48template<> struct convert_code_to_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
49
50template <typename T>
52
53template<> struct convert_code_from_cv<yarp::sig::PixelMono> : std::integral_constant<int, -1> {};
54template<> struct convert_code_from_cv<yarp::sig::PixelMono16> : std::integral_constant<int, -1> {};
55template<> struct convert_code_from_cv<yarp::sig::PixelMonoSigned> : std::integral_constant<int, -1> {};
56template<> struct convert_code_from_cv<yarp::sig::PixelFloat> : std::integral_constant<int, -1> {};
57template<> struct convert_code_from_cv<yarp::sig::PixelInt> : std::integral_constant<int, -1> {};
58template<> struct convert_code_from_cv<yarp::sig::PixelBgr> : std::integral_constant<int, -1> {};
59template<> struct convert_code_from_cv<yarp::sig::PixelRgb> : std::integral_constant<int, CV_BGR2RGB> {};
60template<> struct convert_code_from_cv<yarp::sig::PixelRgbSigned> : std::integral_constant<int, CV_BGR2RGB> {};
61template<> struct convert_code_from_cv<yarp::sig::PixelRgbFloat> : std::integral_constant<int, CV_BGR2RGB> {};
62template<> struct convert_code_from_cv<yarp::sig::PixelRgbInt> : std::integral_constant<int, CV_BGR2RGB> {};
63template<> struct convert_code_from_cv<yarp::sig::PixelHsv> : std::integral_constant<int, CV_BGR2HSV> {};
64template<> struct convert_code_from_cv<yarp::sig::PixelHsvFloat> : std::integral_constant<int, CV_BGR2HSV> {};
65template<> struct convert_code_from_cv<yarp::sig::PixelRgba> : std::integral_constant<int, CV_BGRA2RGBA> {};
66template<> struct convert_code_from_cv<yarp::sig::PixelBgra> : std::integral_constant<int, -1> {};
67
68
69} // namespace yarp::cv
70
71
72template<typename T>
73::cv::Mat yarp::cv::toCvMat(yarp::sig::ImageOf<T>& yarpImage)
74{
75 ::cv::Mat outMat (yarpImage.height(), yarpImage.width(), yarp::cv::type_code<T>::value,
76 yarpImage.getRawImage(), yarpImage.getRowSize()); // RVO
77 if (convert_code_to_cv<T>::value >= 0)
78 {
79 ::cv::cvtColor(outMat, outMat, convert_code_to_cv<T>::value);
80 }
81 return outMat;
82}
83
84
85template<typename T>
87{
88 constexpr size_t align_8_bytes = 8;
89 constexpr size_t align_4_bytes = 4;
90
92 // Checking cv::Mat::type() compatibility with the T PixelType
93 assert(yarp::cv::type_code<T>::value == cvImage.type());
95 {
96 ::cv::cvtColor(cvImage, cvImage, convert_code_from_cv<T>::value);
97 }
98 // Check the cv::Mat alignment
99 if (cvImage.step % align_8_bytes == 0) {
100 outImg.setQuantum(align_8_bytes);
101 }
102 else if (cvImage.step % align_4_bytes == 0) {
103 outImg.setQuantum(align_4_bytes);
104 }
105 outImg.setExternal(cvImage.data, cvImage.cols, cvImage.rows);
106 return outImg;
107}
108
109#endif // YARP_CV_CV_INL_H
Typed image class.
Definition: Image.h:653
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:502
size_t width() const
Gets width of image in pixels.
Definition: Image.h:163
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:904
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:189
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:542
size_t height() const
Gets height of image in pixels.
Definition: Image.h:169
::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:86
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:562
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:448
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:443
float PixelFloat
Floating point pixel type.
Definition: Image.h:579
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:453
The main, catch-all namespace for YARP.
Definition: dirs.h:16
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:532
Packed BGRA pixel type.
Definition: Image.h:507
Floating point HSV pixel type.
Definition: Image.h:630
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:552
Floating point RGB pixel type.
Definition: Image.h:586
Integer RGB pixel type.
Definition: Image.h:608
Signed, packed RGB pixel type.
Definition: Image.h:569
Packed RGB pixel type.
Definition: Image.h:460
Packed RGBA pixel type.
Definition: Image.h:482