Visual Servoing Platform  version 3.5.1 under development (2022-07-06)
tutorial-bridge-opencv-image.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/io/vpImageIo.h>
6 
7 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
8 #include <opencv2/core/core.hpp>
9 #include <opencv2/imgproc/imgproc.hpp>
10 #endif
11 
12 int main()
13 {
14 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
15  // From ViSP to OpenCV conversion
16  {
18  vpImage<vpRGBa> Irgba;
19  vpImageIo::read(Irgba, "monkey.jpeg");
21 
24  vpImageIo::read(Igrey, "monkey.jpeg");
26 
28  cv::Mat cv_img_color;
29  vpImageConvert::convert(Irgba, cv_img_color);
31 
33  cv::Mat cv_img_grey;
34  vpImageConvert::convert(Igrey, cv_img_grey);
36 
37  std::cout << "Save converted images from vpImage to cv::Mat" << std::endl;
38  std::cout << "- monkey-cv-color.jpeg" << std::endl;
39  std::cout << "- monkey-cv-grey.jpeg" << std::endl;
41  cv::imwrite("monkey-cv-color.jpeg", cv_img_color);
44  cv::imwrite("monkey-cv-grey.jpeg", cv_img_grey);
46  }
47 
48  // From OpenCV to ViSP conversion
49  {
51 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
52  cv::Mat cv_img_color = cv::imread("monkey.jpeg", cv::IMREAD_COLOR);
53 #else
54  cv::Mat cv_img_color = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_COLOR);
55 #endif
57 
59  vpImage<vpRGBa> Irgba;
60  vpImageConvert::convert(cv_img_color, Irgba);
62 
64 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
65  cv::Mat cv_img_grey = cv::imread("monkey.jpeg", cv::IMREAD_GRAYSCALE);
66 #else
67  cv::Mat cv_img_grey = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
68 #endif
70 
73  vpImageConvert::convert(cv_img_grey, Igrey);
75 
76  std::cout << "Save converted images from cv::Mat to vpImage" << std::endl;
77  std::cout << "- monkey-vp-color.jpeg" << std::endl;
78  std::cout << "- monkey-vp-grey.jpeg" << std::endl;
80  vpImageIo::write(Irgba, "monkey-vp-color.jpeg");
83  vpImageIo::write(Igrey, "monkey-vp-grey.jpeg");
85  }
86 #endif
87 }
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:148
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:292