Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpImageIoOpenCV.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * OpenCV backend for image I/O operations.
32 */
33
38
39#include <visp3/core/vpConfig.h>
40
41#include "vpImageIoBackend.h"
42
43#if defined(VISP_HAVE_OPENCV)
44#if (VISP_HAVE_OPENCV_VERSION >= 0x030000) // Require opencv >= 3.0.0
45#if defined(HAVE_OPENCV_IMGCODECS)
46#include <opencv2/imgcodecs.hpp>
47#endif
48#else // Require opencv >= 2.4.8
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>
53#endif
54#endif
55#endif
56
57#include <visp3/core/vpImageConvert.h>
58
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)))
62
79void readOpenCV(vpImage<unsigned char> &I, const std::string &filename)
80{
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;
86#else
87 int flags = CV_LOAD_IMAGE_GRAYSCALE;
88#endif
89 cv::Mat Ip = cv::imread(filename.c_str(), flags);
90 if (!Ip.empty())
92 else
93 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
94#endif
95}
96
114void readOpenCV(vpImage<vpRGBa> &I, const std::string &filename)
115{
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;
121#else
122 int flags = CV_LOAD_IMAGE_COLOR;
123#endif
124 cv::Mat Ip = cv::imread(filename.c_str(), flags);
125 if (!Ip.empty())
127 else
128 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
129#endif
130}
131
132void readOpenCV(vpImage<float> &I, const std::string &filename)
133{
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;
139#else
140 int flags = CV_LOAD_IMAGE_ANYDEPTH;
141#endif
142 cv::Mat Ip = cv::imread(filename.c_str(), flags);
143 if (!Ip.empty())
145 else
146 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
147#else
148 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
149#endif
150}
151
152void readOpenCV(vpImage<vpRGBf> &I, const std::string &filename)
153{
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;
159#else
160 int flags = CV_LOAD_IMAGE_COLOR;
161#endif
162 cv::Mat Ip = cv::imread(filename.c_str(), flags);
163 if (!Ip.empty())
165 else
166 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
167#else
168 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
169#endif
170}
171
178void readPNGfromMemOpenCV(const std::vector<unsigned char> &buffer, vpImage<unsigned char> &I)
179{
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);
184}
185
192void readPNGfromMemOpenCV(const std::vector<unsigned char> &buffer, vpImage<vpRGBa> &I_color)
193{
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);
196 vpImageConvert::convert(img, I_color);
197}
198
206void writeOpenCV(const vpImage<unsigned char> &I, const std::string &filename, int quality)
207{
208 cv::Mat Ip;
210
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);
215}
216
224void writeOpenCV(const vpImage<vpRGBa> &I, const std::string &filename, int quality)
225{
226 cv::Mat Ip;
228
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);
233}
234
235void writeOpenCV(const vpImage<float> &I, const std::string &filename)
236{
237 cv::Mat Ip;
239
240 cv::imwrite(filename.c_str(), Ip);
241}
242
243void writeOpenCV(const vpImage<vpRGBf> &I, const std::string &filename)
244{
245 cv::Mat Ip;
247
248 cv::imwrite(filename.c_str(), Ip);
249}
250
257void writePNGtoMemOpenCV(const vpImage<unsigned char> &I, std::vector<unsigned char> &buffer)
258{
259 cv::Mat1b img(I.getRows(), I.getCols(), I.bitmap);
260 bool result = cv::imencode(".png", img, buffer);
261
262 if (!result) {
263 std::string message = "Cannot write png to memory";
265 }
266}
267
275void writePNGtoMemOpenCV(const vpImage<vpRGBa> &I_color, std::vector<unsigned char> &buffer, bool saveAlpha)
276{
277 const int height = I_color.getRows();
278 const int width = I_color.getCols();
279 const int channels = saveAlpha ? 4 : 3;
280
281 if (saveAlpha) {
282 cv::Mat4b img(height, width, reinterpret_cast<cv::Vec4b *>(I_color.bitmap));
283 // No need to perform RGB to BGR conversion
284 bool result = cv::imencode(".png", img, buffer);
285
286 if (!result) {
287 std::string message = "Cannot write png to memory";
289 }
290 }
291 else {
292 unsigned char *bitmap = new unsigned char[height * width * channels];
293 vpImageConvert::RGBaToRGB(reinterpret_cast<unsigned char *>(I_color.bitmap), bitmap, height*width);
294
295 cv::Mat3b img(height, width, reinterpret_cast<cv::Vec3b *>(bitmap));
296 // No need to perform RGB to BGR conversion
297 bool result = cv::imencode(".png", img, buffer);
298 delete[] bitmap;
299
300 if (!result) {
301 std::string message = "Cannot write png to memory";
303 }
304 }
305}
306
307END_VISP_NAMESPACE
308
309#endif
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.
@ ioError
Image io error.
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getCols() const
Definition vpImage.h:171
Type * bitmap
points toward the bitmap
Definition vpImage.h:135
unsigned int getRows() const
Definition vpImage.h:212