Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
tutorial-bridge-opencv-image.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/io/vpImageIo.h>
6 
7 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_IMGCODECS)
8 #include <opencv2/core/core.hpp>
9 #include <opencv2/imgproc/imgproc.hpp>
10 #include <opencv2/imgcodecs/imgcodecs.hpp>
11 
12 int main()
13 {
14  // From ViSP to OpenCV conversion
15  {
17  vpImage<vpRGBa> Irgba;
18  vpImageIo::read(Irgba, "monkey.jpeg");
20 
23  vpImageIo::read(Igrey, "monkey.jpeg");
25 
27  cv::Mat cv_img_color;
28  vpImageConvert::convert(Irgba, cv_img_color);
30 
32  cv::Mat cv_img_grey;
33  vpImageConvert::convert(Igrey, cv_img_grey);
35 
36  std::cout << "Save converted images from vpImage to cv::Mat" << std::endl;
37  std::cout << "- monkey-cv-color.jpeg" << std::endl;
38  std::cout << "- monkey-cv-grey.jpeg" << std::endl;
40  cv::imwrite("monkey-cv-color.jpeg", cv_img_color);
43  cv::imwrite("monkey-cv-grey.jpeg", cv_img_grey);
45  }
46 
47  // From OpenCV to ViSP conversion
48  {
50 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
51  cv::Mat cv_img_color = cv::imread("monkey.jpeg", cv::IMREAD_COLOR);
52 #else
53  cv::Mat cv_img_color = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_COLOR);
54 #endif
56 
58  vpImage<vpRGBa> Irgba;
59  vpImageConvert::convert(cv_img_color, Irgba);
61 
63 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
64  cv::Mat cv_img_grey = cv::imread("monkey.jpeg", cv::IMREAD_GRAYSCALE);
65 #else
66  cv::Mat cv_img_grey = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
67 #endif
69 
72  vpImageConvert::convert(cv_img_grey, Igrey);
74 
75  std::cout << "Save converted images from cv::Mat to vpImage" << std::endl;
76  std::cout << "- monkey-vp-color.jpeg" << std::endl;
77  std::cout << "- monkey-vp-grey.jpeg" << std::endl;
79  vpImageIo::write(Irgba, "monkey-vp-color.jpeg");
82  vpImageIo::write(Igrey, "monkey-vp-grey.jpeg");
84  }
85 }
86 #else
87 int main()
88 {
89  std::cout << "This tutorial required OpenCV imgproc and imgcodecs modules." << std::endl;
90  return EXIT_SUCCESS;
91 }
92 #endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287