NeuralEngine A Game Engine with embeded Machine Learning algorithms based on Gaussian Processes. |

FgArmaCv.h
1#pragma once
2
3#include <armadillo>
4#include <opencv2/opencv.hpp>
5
6// convert an OpenCV multi-channel matrix to Armadillo cube. A copy is made
7 template <typename T, int NC>
8Cube<T> to_arma(const cv::Mat_<cv::Vec<T, NC>> &src)
9{
10 vector<cv::Mat_<T>> channels;
11 Cube<T> dst(src.cols, src.rows, NC);
12 for (int c = 0; c < NC; ++c)
13 channels.push_back({ src.rows, src.cols, dst.slice(c).memptr() });
14 cv::split(src, channels);
15 return dst;
16}
17
18// convert an OpenCV matrix to Armadillo matrix. NOTE: a copy is made
19 template <typename T>
20Mat<T> to_arma(const cv::Mat_<T> &src)
21{
22 Mat<T> dst(src.cols, src.rows);
23 src.copyTo({ src.rows, src.cols, dst.memptr() });
24 return dst;
25}
26
27// convert an Armadillo cube to OpenCV matrix. NOTE: a copy is made
28 template <typename T>
29cv::Mat to_cvmat(const Cube<T> &src)
30{
31 vector<cv::Mat_<T>> channels;
32 for (size_t c = 0; c < src.n_slices; ++c)
33 {
34 auto *data = const_cast<T*>(src.slice(c).memptr());
35 channels.push_back({ int(src.n_cols), int(src.n_rows), data });
36 }
37 cv::Mat dst;
38 cv::merge(channels, dst);
39 return dst;
40}
41
42// convert an Armadillo matrix to OpenCV matrix. NOTE: no copy is made
43 template <typename T>
44cv::Mat_<T> to_cvmat(const Mat<T> &src)
45{
46 return cv::Mat_<double>{int(src.n_cols), int(src.n_rows), const_cast<T*>(src.memptr())};
47}
int