eos 1.4.0
Loading...
Searching...
No Matches
opencv_interop.hpp
1/*
2 * eos - A 3D Morphable Model fitting library written in modern C++11/14.
3 *
4 * File: include/eos/core/image/opencv_interop.hpp
5 *
6 * Copyright 2017 Patrik Huber
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#pragma once
21
22#ifndef EOS_IMAGE_OPENCV_INTEROP_HPP
23#define EOS_IMAGE_OPENCV_INTEROP_HPP
24
25#include "eos/core/Image.hpp"
26
27#include "opencv2/core/core.hpp"
28
29#include <array>
30#include <cstdint>
31#include <stdexcept>
32
33namespace eos {
34namespace core {
35
36// We can support different types by making this a template and constexpr if? :-)
37inline cv::Mat to_mat(const Image4u& image)
38{
39 cv::Mat opencv_matrix(static_cast<int>(image.height()), static_cast<int>(image.width()), CV_8UC4);
40 for (int c = 0; c < image.width(); ++c)
41 { // size_t
42 for (int r = 0; r < image.height(); ++r)
43 {
44 // auto vals = image(r, c);
45 opencv_matrix.at<cv::Vec4b>(r, c) =
46 cv::Vec4b(image(r, c)[0], image(r, c)[1], image(r, c)[2], image(r, c)[3]);
47 }
48 }
49 return opencv_matrix;
50};
51
52inline cv::Mat to_mat(const Image3u& image)
53{
54 cv::Mat opencv_matrix(image.height(), image.width(), CV_8UC3);
55 for (int c = 0; c < image.width(); ++c)
56 { // size_t
57 for (int r = 0; r < image.height(); ++r)
58 {
59 // auto vals = image(r, c);
60 opencv_matrix.at<cv::Vec3b>(r, c) = cv::Vec3b(image(r, c)[0], image(r, c)[1], image(r, c)[2]);
61 }
62 }
63 return opencv_matrix;
64};
65
66inline cv::Mat to_mat(const Image1d& image)
67{
68 cv::Mat opencv_matrix(static_cast<int>(image.height()), static_cast<int>(image.width()), CV_64FC1);
69 for (int c = 0; c < image.width(); ++c)
70 { // size_t
71 for (int r = 0; r < image.height(); ++r)
72 {
73 // auto vals = image(r, c);
74 opencv_matrix.at<double>(r, c) = image(r, c);
75 }
76 }
77 return opencv_matrix;
78};
79
80inline cv::Mat to_mat(const Image1u& image)
81{
82 cv::Mat opencv_matrix(static_cast<int>(image.height()), static_cast<int>(image.width()), CV_8UC1);
83 for (int c = 0; c < image.width(); ++c)
84 { // size_t
85 for (int r = 0; r < image.height(); ++r)
86 {
87 // auto vals = image(r, c);
88 opencv_matrix.at<unsigned char>(r, c) = image(r, c);
89 }
90 }
91 return opencv_matrix;
92};
93
98inline Image3u from_mat(const cv::Mat& image)
99{
100 if (image.type() != CV_8UC3)
101 {
102 throw std::runtime_error("Can only convert a CV_8UC3 cv::Mat to an eos::core::Image3u.");
103 }
104
105 Image3u converted(image.rows, image.cols);
106 for (int r = 0; r < image.rows; ++r)
107 {
108 for (int c = 0; c < image.cols; ++c)
109 {
110 converted(r, c) = {image.at<cv::Vec3b>(r, c)[0], image.at<cv::Vec3b>(r, c)[1],
111 image.at<cv::Vec3b>(r, c)[2]};
112 }
113 }
114 return converted;
115};
116
121inline Image4u from_mat_with_alpha(const cv::Mat& image)
122{
123 if (image.type() != CV_8UC3 && image.type() != CV_8UC4)
124 {
125 throw std::runtime_error("Can only convert a CV_8UC3 or CV_8UC4 cv::Mat to an eos::core::Image4u.");
126 }
127
128 Image4u converted(image.rows, image.cols);
129 if (image.type() == CV_8UC3)
130 {
131 for (int r = 0; r < image.rows; ++r)
132 {
133 for (int c = 0; c < image.cols; ++c)
134 {
135 converted(r, c) = {image.at<cv::Vec3b>(r, c)[0], image.at<cv::Vec3b>(r, c)[1],
136 image.at<cv::Vec3b>(r, c)[2], 255};
137 }
138 }
139 } else if (image.type() == CV_8UC4)
140 {
141 for (int r = 0; r < image.rows; ++r)
142 {
143 for (int c = 0; c < image.cols; ++c)
144 {
145 converted(r, c) = {image.at<cv::Vec4b>(r, c)[0], image.at<cv::Vec4b>(r, c)[1],
146 image.at<cv::Vec4b>(r, c)[2], image.at<cv::Vec4b>(r, c)[3]};
147 }
148 }
149 }
150 return converted;
151};
152
153} /* namespace core */
154} /* namespace eos */
155
156#endif /* EOS_IMAGE_OPENCV_INTEROP_HPP */
Class to represent images.
Definition: Image.hpp:44
Image3u from_mat(const cv::Mat &image)
Definition: opencv_interop.hpp:98
Image4u from_mat_with_alpha(const cv::Mat &image)
Definition: opencv_interop.hpp:121
Namespace containing all of eos's 3D model fitting functionality.