YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
84inline ::cv::Mat yarp::cv::toCvMat(yarp::sig::Image& yarpImage)
85{
86 int val=-1;
87 int type=0;
88 if (yarpImage.getPixelCode() == VOCAB_PIXEL_RGB_INT) {type = CV_32SC1; val = CV_RGB2BGR;}
89 else if (yarpImage.getPixelCode() == VOCAB_PIXEL_RGB) { type = CV_8UC3; val = CV_RGB2BGR;}
90 else if (yarpImage.getPixelCode() == VOCAB_PIXEL_MONO_FLOAT) { type = CV_32FC1; val = -1;}
91 else
92 {
93 //not yet implemented.
94 assert(0);
95 }
96 ::cv::Mat outMat(yarpImage.height(), yarpImage.width(), type,
97 yarpImage.getRawImage(), yarpImage.getRowSize()); // RVO
98 if (val >= 0)
99 {
100 ::cv::cvtColor(outMat, outMat, val);
101 }
102 return outMat;
103}
104
105template<typename T>
107{
108 constexpr size_t align_8_bytes = 8;
109 constexpr size_t align_4_bytes = 4;
110
112 // Checking cv::Mat::type() compatibility with the T PixelType
113 assert(yarp::cv::type_code<T>::value == cvImage.type());
115 {
116 ::cv::cvtColor(cvImage, cvImage, convert_code_from_cv<T>::value);
117 }
118 // Check the cv::Mat alignment
119 if (cvImage.step % align_8_bytes == 0) {
120 outImg.setQuantum(align_8_bytes);
121 }
122 else if (cvImage.step % align_4_bytes == 0) {
123 outImg.setQuantum(align_4_bytes);
124 }
125 outImg.setExternal(cvImage.data, cvImage.cols, cvImage.rows);
126 return outImg;
127}
128
129#endif // YARP_CV_CV_INL_H
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_RGB_INT
Definition Image.h:52
@ VOCAB_PIXEL_RGB
Definition Image.h:44
A mini-server for performing network communication in the background.
Typed image class.
Definition Image.h:605
Base class for storing images.
Definition Image.h:79
void setQuantum(size_t imgQuantum)
Definition Image.cpp:451
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition Image.cpp:691
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition Image.h:197
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:390
::cv::Mat toCvMat(yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
Definition Cv-inl.h:84
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition Cv-inl.h:106
The main, catch-all namespace for YARP.
Definition dirs.h:16