Visual Servoing Platform  version 3.6.1 under development (2024-11-23)
tutorial-bridge-opencv-matrix.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/io/vpImageIo.h>
5 
6 #if defined(HAVE_OPENCV_IMGPROC)
7 #include <opencv2/core/core.hpp>
8 #include <opencv2/imgproc/imgproc.hpp>
9 #endif
10 
11 int main()
12 {
13 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
14 #ifdef ENABLE_VISP_NAMESPACE
15  using namespace VISP_NAMESPACE_NAME;
16 #endif
17  {
18  std::cout << "From OpenCV to ViSP conversion" << std::endl;
20  cv::Mat M_cv = (cv::Mat_<double>(3, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
21  std::cout << "M_cv: \n" << M_cv << std::endl;
23 
25  vpMatrix M(static_cast<unsigned int>(M_cv.rows), static_cast<unsigned int>(M_cv.cols));
26  memcpy(M.data, M_cv.data, sizeof(double) * static_cast<size_t>(M_cv.rows * M_cv.cols));
27  std::cout << "M: \n" << M << std::endl;
29  }
30 
31  {
32  std::cout << "From ViSP to OpenCV conversion" << std::endl;
34  vpMatrix M(3, 4, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
35  std::cout << "M: \n" << M << std::endl;
37 
39  cv::Mat tmp(static_cast<int>(M.getRows()), static_cast<int>(M.getCols()), CV_64F, static_cast<void *>(M.data));
40  cv::Mat M_cv_deep = tmp.clone();
41  std::cout << "M_cv_deep: \n" << M_cv_deep << std::endl;
43 
45  cv::Mat M_cv(static_cast<int>(M.getRows()), static_cast<int>(M.getCols()), CV_64F, static_cast<void *>(M.data));
46  std::cout << "M_cv: \n" << M_cv << std::endl;
48 
50  std::cout << "Set M = eye" << std::endl;
51  M.eye();
52  std::cout << "M: \n" << M << std::endl;
53  std::cout << "M_cv: \n" << M_cv << std::endl;
55 }
56 #endif
57 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169