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))
80 #if defined(VISP_HAVE_OPENCV)
81 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
82 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
83 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
84 int flags = cv::IMREAD_GRAYSCALE;
86 int flags = CV_LOAD_IMAGE_GRAYSCALE;
88 cv::Mat Ip = cv::imread(filename.c_str(), flags);
115 #if defined(VISP_HAVE_OPENCV)
116 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
117 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
118 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
119 int flags = cv::IMREAD_COLOR;
121 int flags = CV_LOAD_IMAGE_COLOR;
123 cv::Mat Ip = cv::imread(filename.c_str(), flags);
133 #if defined(VISP_HAVE_OPENCV)
134 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
135 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
136 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
137 int flags = cv::IMREAD_COLOR;
139 int flags = CV_LOAD_IMAGE_COLOR;
141 cv::Mat Ip = cv::imread(filename.c_str(), flags);
153 #if defined(VISP_HAVE_OPENCV)
154 #if VISP_HAVE_OPENCV_VERSION >= 0x030200
155 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
156 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
157 int flags = cv::IMREAD_COLOR;
159 int flags = CV_LOAD_IMAGE_COLOR;
161 cv::Mat Ip = cv::imread(filename.c_str(), flags);
179 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
180 cv::Mat1b img = cv::imdecode(buf, cv::IMREAD_GRAYSCALE);
181 I.
resize(img.rows, img.cols);
182 std::copy(img.begin(), img.end(), I.
bitmap);
191 void readPNGfromMemOpenCV(
const std::vector<unsigned char> &buffer,
vpImage<vpRGBa> &I_color)
193 cv::Mat1b buf(
static_cast<int>(buffer.size()), 1,
const_cast<unsigned char *
>(buffer.data()));
194 cv::Mat3b img = cv::imdecode(buf, cv::IMREAD_COLOR);
210 std::vector<int> compression_params;
211 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
212 compression_params.push_back(quality);
213 cv::imwrite(filename.c_str(), Ip, compression_params);
223 void writeOpenCV(
const vpImage<vpRGBa> &I,
const std::string &filename,
int quality)
228 std::vector<int> compression_params;
229 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
230 compression_params.push_back(quality);
231 cv::imwrite(filename.c_str(), Ip, compression_params);
234 void writeOpenCV(
const vpImage<float> &I,
const std::string &filename)
239 cv::imwrite(filename.c_str(), Ip);
242 void writeOpenCV(
const vpImage<vpRGBf> &I,
const std::string &filename)
247 cv::imwrite(filename.c_str(), Ip);
259 bool result = cv::imencode(
".png", img, buffer);
262 std::string message =
"Cannot write png to memory";
274 void writePNGtoMemOpenCV(
const vpImage<vpRGBa> &I_color, std::vector<unsigned char> &buffer,
bool saveAlpha)
276 const int height = I_color.
getRows();
277 const int width = I_color.
getCols();
278 const int channels = saveAlpha ? 4 : 3;
281 cv::Mat4b img(height, width,
reinterpret_cast<cv::Vec4b *
>(I_color.
bitmap));
283 bool result = cv::imencode(
".png", img, buffer);
286 std::string message =
"Cannot write png to memory";
291 unsigned char *bitmap =
new unsigned char[height * width * channels];
294 cv::Mat3b img(height, width,
reinterpret_cast<cv::Vec3b *
>(bitmap));
296 bool result = cv::imencode(
".png", img, buffer);
300 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