Visual Servoing Platform  version 3.4.1 under development (2021-06-21)
tutorial-bridge-opencv-image.cpp
#include <iostream>
#include <visp3/core/vpImageConvert.h>
#include <visp3/io/vpImageIo.h>
#if VISP_HAVE_OPENCV_VERSION >= 0x020300
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif
int main()
{
#if VISP_HAVE_OPENCV_VERSION >= 0x020300
// 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");
}
#endif
}