39 #include <visp3/core/vpConfig.h>
41 #include "vpImageIoBackend.h"
43 #ifdef 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 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
60 && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
79 #if defined(VISP_HAVE_OPENCV)
80 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
81 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
82 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
83 int flags = cv::IMREAD_GRAYSCALE;
85 int flags = CV_LOAD_IMAGE_GRAYSCALE;
87 cv::Mat Ip = cv::imread(filename.c_str(), flags);
114 #if defined(VISP_HAVE_OPENCV)
115 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
116 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
117 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
118 int flags = cv::IMREAD_COLOR;
120 int flags = CV_LOAD_IMAGE_COLOR;
122 cv::Mat Ip = cv::imread(filename.c_str(), flags);
132 #if defined(VISP_HAVE_OPENCV)
133 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
134 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
135 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
136 int flags = cv::IMREAD_COLOR;
138 int flags = CV_LOAD_IMAGE_COLOR;
140 cv::Mat Ip = cv::imread(filename.c_str(), flags);
152 #if defined(VISP_HAVE_OPENCV)
153 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
154 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
155 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
156 int flags = cv::IMREAD_COLOR;
158 int flags = CV_LOAD_IMAGE_COLOR;
160 cv::Mat Ip = cv::imread(filename.c_str(), flags);
178 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
179 cv::Mat1b img = cv::imdecode(buf, cv::IMREAD_GRAYSCALE);
180 I.
resize(img.rows, img.cols);
181 std::copy(img.begin(), img.end(), I.
bitmap);
190 void readPNGfromMemOpenCV(
const std::vector<unsigned char> &buffer,
vpImage<vpRGBa> &I_color)
192 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
193 cv::Mat3b img = cv::imdecode(buf, cv::IMREAD_COLOR);
209 std::vector<int> compression_params;
210 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
211 compression_params.push_back(quality);
212 cv::imwrite(filename.c_str(), Ip, compression_params);
222 void writeOpenCV(
const vpImage<vpRGBa> &I,
const std::string &filename,
int quality)
227 std::vector<int> compression_params;
228 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
229 compression_params.push_back(quality);
230 cv::imwrite(filename.c_str(), Ip, compression_params);
233 void writeOpenCV(
const vpImage<float> &I,
const std::string &filename)
238 cv::imwrite(filename.c_str(), Ip);
241 void writeOpenCV(
const vpImage<vpRGBf> &I,
const std::string &filename)
246 cv::imwrite(filename.c_str(), Ip);
258 bool result = cv::imencode(
".png", img, buffer);
261 std::string message =
"Cannot write png to memory";
273 void writePNGtoMemOpenCV(
const vpImage<vpRGBa> &I_color, std::vector<unsigned char> &buffer,
bool saveAlpha)
275 const int height = I_color.
getRows();
276 const int width = I_color.
getCols();
277 const int channels = saveAlpha ? 4 : 3;
280 cv::Mat4b img(height, width,
reinterpret_cast<cv::Vec4b *
>(I_color.
bitmap));
282 bool result = cv::imencode(
".png", img, buffer);
285 std::string message =
"Cannot write png to memory";
290 unsigned char *bitmap =
new unsigned char[height * width * channels];
293 cv::Mat3b img(height, width,
reinterpret_cast<cv::Vec3b *
>(bitmap));
295 bool result = cv::imencode(
".png", img, buffer);
299 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.
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
unsigned int getCols() const
Type * bitmap
points toward the bitmap
unsigned int getRows() const