Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-bridge-opencv-image.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
6#if defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_IMGCODECS)
7
8#include <visp3/core/vpImageConvert.h>
9#include <visp3/io/vpImageIo.h>
10
11#include <opencv2/core/core.hpp>
12#include <opencv2/imgproc/imgproc.hpp>
13#include <opencv2/imgcodecs/imgcodecs.hpp>
14
15int main()
16{
17#ifdef ENABLE_VISP_NAMESPACE
18 using namespace VISP_NAMESPACE_NAME;
19#endif
20 // From ViSP to OpenCV conversion
21 {
23 vpImage<vpRGBa> Irgba;
24 std::string image_name = "monkey.jpeg";
25 std::cout << "Read color image: " << image_name << std::endl;
26 vpImageIo::read(Irgba, image_name);
28
31 std::cout << "Read gray image: " << image_name << std::endl;
32 vpImageIo::read(Igray, image_name);
34
36 cv::Mat cv_img_color;
37 vpImageConvert::convert(Irgba, cv_img_color);
39
41 cv::Mat cv_img_gray;
42 vpImageConvert::convert(Igray, cv_img_gray);
44
45 std::cout << "Save converted images from vpImage to cv::Mat" << std::endl;
46 std::cout << "- monkey-cv-color.jpeg" << std::endl;
47 std::cout << "- monkey-cv-gray.jpeg" << std::endl;
49 cv::imwrite("monkey-cv-color.jpeg", cv_img_color);
52 cv::imwrite("monkey-cv-gray.jpeg", cv_img_gray);
54 }
55
56 // From OpenCV to ViSP conversion
57 {
59 std::string image_name = "monkey.jpeg";
60 std::cout << "Read color image: " << image_name << std::endl;
61#if VISP_HAVE_OPENCV_VERSION >= 0x030000
62 cv::Mat cv_img_color = cv::imread("monkey.jpeg", cv::IMREAD_COLOR);
63#else
64 cv::Mat cv_img_color = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_COLOR);
65#endif
67
69 vpImage<vpRGBa> Irgba;
70 vpImageConvert::convert(cv_img_color, Irgba);
72
74 std::cout << "Read gray image: " << image_name << std::endl;
75#if VISP_HAVE_OPENCV_VERSION >= 0x030000
76 cv::Mat cv_img_gray = cv::imread("monkey.jpeg", cv::IMREAD_GRAYSCALE);
77#else
78 cv::Mat cv_img_gray = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
79#endif
81
84 vpImageConvert::convert(cv_img_gray, Igray);
86
87 std::cout << "Save converted images from cv::Mat to vpImage" << std::endl;
88 std::cout << "- monkey-vp-color.jpeg" << std::endl;
89 std::cout << "- monkey-vp-gray.jpeg" << std::endl;
91 vpImageIo::write(Irgba, "monkey-vp-color.jpeg");
94 vpImageIo::write(Igray, "monkey-vp-gray.jpeg");
96 }
97}
98#else
99int main()
100{
101#if !defined(HAVE_OPENCV_IMGPROC)
102 std::cout << "This tutorial requires OpenCV imgproc module." << std::endl;
103#endif
104#if !defined(HAVE_OPENCV_IMGPROC)
105 std::cout << "This tutorial requires OpenCV imgcodecs module." << std::endl;
106#endif
107 return EXIT_SUCCESS;
108}
109#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)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131