Visual Servoing Platform  version 3.4.1 under development (2021-06-21)
tutorial-bridge-opencv-matrix.cpp
#include <visp3/core/vpImageConvert.h>
#include <visp3/io/vpImageIo.h>
#if VISP_HAVE_OPENCV_VERSION >= 0x020300
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif
int main()
{
#if VISP_HAVE_OPENCV_VERSION >= 0x020300 && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
{
std::cout << "From OpenCV to ViSP conversion" << std::endl;
cv::Mat M_cv = (cv::Mat_<double>(3, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
std::cout << "M_cv: \n" << M_cv << std::endl;
vpMatrix M(static_cast<unsigned int>(M_cv.rows), static_cast<unsigned int>(M_cv.cols));
memcpy(M.data, M_cv.data, sizeof(double) * static_cast<size_t>(M_cv.rows * M_cv.cols));
std::cout << "M: \n" << M << std::endl;
}
{
std::cout << "From ViSP to OpenCV conversion" << std::endl;
vpMatrix M(3, 4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} );
std::cout << "M: \n" << M << std::endl;
cv::Mat tmp(static_cast<int>(M.getRows()), static_cast<int>(M.getCols()), CV_64F, static_cast<void *>(M.data));
cv::Mat M_cv_deep = tmp.clone();
std::cout << "M_cv_deep: \n" << M_cv_deep << std::endl;
cv::Mat M_cv(static_cast<int>(M.getRows()), static_cast<int>(M.getCols()), CV_64F, static_cast<void *>(M.data));
std::cout << "M_cv: \n" << M_cv << std::endl;
std::cout << "Set M = eye" << std::endl;
M.eye();
std::cout << "M: \n" << M << std::endl;
std::cout << "M_cv: \n" << M_cv << std::endl;
}
#endif
}