39#include <visp3/core/vpConfig.h>
41#include "vpImageIoBackend.h"
43#if defined(VISP_HAVE_OPENCV)
44#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
45#if defined(HAVE_OPENCV_IMGCODECS)
46#include <opencv2/imgcodecs.hpp>
49#if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
50#include <opencv2/core/core.hpp>
51#include <opencv2/highgui/highgui.hpp>
52#include <opencv2/imgproc/imgproc.hpp>
57#include <visp3/core/vpImageConvert.h>
59#if defined(VISP_HAVE_OPENCV) && \
60 (((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || \
61 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)))
81#if defined(VISP_HAVE_OPENCV)
82#if VISP_HAVE_OPENCV_VERSION >= 0x030200
83 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
84#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
85 int flags = cv::IMREAD_GRAYSCALE;
87 int flags = CV_LOAD_IMAGE_GRAYSCALE;
89 cv::Mat Ip = cv::imread(
filename.c_str(), flags);
116#if defined(VISP_HAVE_OPENCV)
117#if VISP_HAVE_OPENCV_VERSION >= 0x030200
118 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
119#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
120 int flags = cv::IMREAD_COLOR;
122 int flags = CV_LOAD_IMAGE_COLOR;
124 cv::Mat Ip = cv::imread(
filename.c_str(), flags);
134#if defined(VISP_HAVE_OPENCV)
135#if VISP_HAVE_OPENCV_VERSION >= 0x030200
136 int flags = cv::IMREAD_ANYDEPTH | cv::IMREAD_IGNORE_ORIENTATION;
137#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
138 int flags = cv::IMREAD_ANYDEPTH;
140 int flags = CV_LOAD_IMAGE_ANYDEPTH;
142 cv::Mat Ip = cv::imread(
filename.c_str(), flags);
154#if defined(VISP_HAVE_OPENCV)
155#if VISP_HAVE_OPENCV_VERSION >= 0x030200
156 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
157#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
158 int flags = cv::IMREAD_COLOR;
160 int flags = CV_LOAD_IMAGE_COLOR;
162 cv::Mat Ip = cv::imread(
filename.c_str(), flags);
180 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
181 cv::Mat1b img = cv::imdecode(buf, cv::IMREAD_GRAYSCALE);
182 I.resize(img.rows, img.cols);
183 std::copy(img.begin(), img.end(), I.bitmap);
192void readPNGfromMemOpenCV(
const std::vector<unsigned char> &buffer,
vpImage<vpRGBa> &I_color)
194 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
195 cv::Mat3b img = cv::imdecode(buf, cv::IMREAD_COLOR);
211 std::vector<int> compression_params;
212 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
213 compression_params.push_back(quality);
214 cv::imwrite(
filename.c_str(), Ip, compression_params);
224void writeOpenCV(
const vpImage<vpRGBa> &I,
const std::string &filename,
int quality)
229 std::vector<int> compression_params;
230 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
231 compression_params.push_back(quality);
232 cv::imwrite(
filename.c_str(), Ip, compression_params);
235void writeOpenCV(
const vpImage<float> &I,
const std::string &filename)
259 cv::Mat1b img(I.getRows(), I.getCols(), I.bitmap);
260 bool result = cv::imencode(
".png", img, buffer);
263 std::string message =
"Cannot write png to memory";
275void writePNGtoMemOpenCV(
const vpImage<vpRGBa> &I_color, std::vector<unsigned char> &buffer,
bool saveAlpha)
279 const int channels = saveAlpha ? 4 : 3;
282 cv::Mat4b img(height, width,
reinterpret_cast<cv::Vec4b *
>(I_color.
bitmap));
284 bool result = cv::imencode(
".png", img, buffer);
287 std::string message =
"Cannot write png to memory";
292 unsigned char *bitmap =
new unsigned char[
height *
width * channels];
295 cv::Mat3b img(height, width,
reinterpret_cast<cv::Vec3b *
>(bitmap));
297 bool result = cv::imencode(
".png", img, buffer);
301 std::string message =
"Cannot write png to memory";
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
Error that can be emitted by the vpImage class and its derivatives.
Definition of the vpImage class member functions.
unsigned int getCols() const
Type * bitmap
points toward the bitmap
unsigned int getRows() const