22#ifndef EOS_IMAGE_OPENCV_INTEROP_HPP
23#define EOS_IMAGE_OPENCV_INTEROP_HPP
25#include "eos/core/Image.hpp"
27#include "opencv2/core/core.hpp"
37inline cv::Mat to_mat(
const Image4u& image)
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)
42 for (
int r = 0; r < image.height(); ++r)
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]);
52inline cv::Mat to_mat(
const Image3u& image)
54 cv::Mat opencv_matrix(image.height(), image.width(), CV_8UC3);
55 for (
int c = 0; c < image.width(); ++c)
57 for (
int r = 0; r < image.height(); ++r)
60 opencv_matrix.at<cv::Vec3b>(r, c) = cv::Vec3b(image(r, c)[0], image(r, c)[1], image(r, c)[2]);
66inline cv::Mat to_mat(
const Image1d& image)
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)
71 for (
int r = 0; r < image.height(); ++r)
74 opencv_matrix.at<
double>(r, c) = image(r, c);
80inline cv::Mat to_mat(
const Image1u& image)
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)
85 for (
int r = 0; r < image.height(); ++r)
88 opencv_matrix.at<
unsigned char>(r, c) = image(r, c);
100 if (image.type() != CV_8UC3)
102 throw std::runtime_error(
"Can only convert a CV_8UC3 cv::Mat to an eos::core::Image3u.");
105 Image3u converted(image.rows, image.cols);
106 for (
int r = 0; r < image.rows; ++r)
108 for (
int c = 0; c < image.cols; ++c)
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]};
123 if (image.type() != CV_8UC3 && image.type() != CV_8UC4)
125 throw std::runtime_error(
"Can only convert a CV_8UC3 or CV_8UC4 cv::Mat to an eos::core::Image4u.");
128 Image4u converted(image.rows, image.cols);
129 if (image.type() == CV_8UC3)
131 for (
int r = 0; r < image.rows; ++r)
133 for (
int c = 0; c < image.cols; ++c)
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};
139 }
else if (image.type() == CV_8UC4)
141 for (
int r = 0; r < image.rows; ++r)
143 for (
int c = 0; c < image.cols; ++c)
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]};
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.