libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNCV.h
Go to the documentation of this file.
1 /* Copyright 2016 CoReNum
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNCV.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNCV_HEADER
23 #define CRNCV_HEADER
24 
25 #include "opencv2/core/mat.hpp"
26 #include <CRNImage/CRNImage.h>
27 
28 namespace crn
29 {
30  template<typename T> struct CVMatType {};
31  template<> struct CVMatType<uint8_t> { static constexpr int val = CV_8UC1; };
32  template<> struct CVMatType<int8_t> { static constexpr int val = CV_8SC1; };
33  template<> struct CVMatType<uint16_t> { static constexpr int val = CV_16UC1; };
34  template<> struct CVMatType<int16_t> { static constexpr int val = CV_16SC1; };
35  template<> struct CVMatType<int32_t> { static constexpr int val = CV_32SC1; };
36  template<> struct CVMatType<float> { static constexpr int val = CV_32FC1; };
37  template<> struct CVMatType<double> { static constexpr int val = CV_64FC1; };
38  template<typename T> struct CVMatType<crn::pixel::RGB<T>> { static constexpr int val = CV_MAKETYPE(CVMatType<T>::val, 3); };
39  template<> struct CVMatType<crn::pixel::HSV> { static constexpr int val = CV_8UC3; };
40  template<typename T> struct CVMatType<crn::pixel::Cart2D<T>> { static constexpr int val = CV_MAKETYPE(CVMatType<T>::val, 2); };
41  template<> struct CVMatType<crn::pixel::XYZ> { static constexpr int val = CV_64FC3; };
42  template<> struct CVMatType<crn::pixel::YUV> { static constexpr int val = CV_64FC3; };
43  template<> struct CVMatType<crn::pixel::Lab> { static constexpr int val = CV_64FC3; };
44  template<> struct CVMatType<crn::pixel::Luv> { static constexpr int val = CV_64FC3; };
45 
52  template<typename T> cv::Mat WrapCVMat(crn::Image<T> &img)
53  {
54  return cv::Mat{int(img.GetWidth()), int(img.GetHeight()), CVMatType<T>::val, reinterpret_cast<unsigned char *>(img.GetPixels())};
55  }
56 
57 }
58 
59 #endif
60 
Abstract class for images.
Definition: CRNImage.h:141
cv::Mat WrapCVMat(crn::Image< T > &img)
Wraps an image to an OpenCV matrix.
Definition: CRNCV.h:52
size_t GetHeight() const noexcept
Definition: CRNImage.h:74
size_t GetWidth() const noexcept
Definition: CRNImage.h:72
std::vector< pixel_type >::pointer GetPixels() noexcept
Gets a pointer to the pixels.
Definition: CRNImage.h:219