Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
tutorial-bridge-opencv-image.cpp
#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/io/vpImageIo.h>
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_IMGCODECS)
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
int main()
{
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
// From ViSP to OpenCV conversion
{
vpImageIo::read(Irgba, "monkey.jpeg");
vpImageIo::read(Igrey, "monkey.jpeg");
cv::Mat cv_img_color;
vpImageConvert::convert(Irgba, cv_img_color);
cv::Mat cv_img_grey;
vpImageConvert::convert(Igrey, cv_img_grey);
std::cout << "Save converted images from vpImage to cv::Mat" << std::endl;
std::cout << "- monkey-cv-color.jpeg" << std::endl;
std::cout << "- monkey-cv-grey.jpeg" << std::endl;
cv::imwrite("monkey-cv-color.jpeg", cv_img_color);
cv::imwrite("monkey-cv-grey.jpeg", cv_img_grey);
}
// From OpenCV to ViSP conversion
{
#if VISP_HAVE_OPENCV_VERSION >= 0x030000
cv::Mat cv_img_color = cv::imread("monkey.jpeg", cv::IMREAD_COLOR);
#else
cv::Mat cv_img_color = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_COLOR);
#endif
vpImageConvert::convert(cv_img_color, Irgba);
#if VISP_HAVE_OPENCV_VERSION >= 0x030000
cv::Mat cv_img_grey = cv::imread("monkey.jpeg", cv::IMREAD_GRAYSCALE);
#else
cv::Mat cv_img_grey = cv::imread("monkey.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
#endif
vpImageConvert::convert(cv_img_grey, Igrey);
std::cout << "Save converted images from cv::Mat to vpImage" << std::endl;
std::cout << "- monkey-vp-color.jpeg" << std::endl;
std::cout << "- monkey-vp-grey.jpeg" << std::endl;
vpImageIo::write(Irgba, "monkey-vp-color.jpeg");
vpImageIo::write(Igrey, "monkey-vp-grey.jpeg");
}
}
#else
int main()
{
std::cout << "This tutorial required OpenCV imgproc and imgcodecs modules." << std::endl;
return EXIT_SUCCESS;
}
#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:147
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291