Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpImageIoOpenCV.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
39 #include <visp3/core/vpConfig.h>
40 
41 #include "vpImageIoBackend.h"
42 
43 #ifdef 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 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGCODECS)) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) \
60  && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
61 
62 BEGIN_VISP_NAMESPACE
78 void readOpenCV(vpImage<unsigned char> &I, const std::string &filename)
79 {
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;
85 #else
86  int flags = CV_LOAD_IMAGE_GRAYSCALE;
87 #endif
88  cv::Mat Ip = cv::imread(filename.c_str(), flags);
89  if (!Ip.empty())
91  else
92  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
93 #endif
94 }
95 
113 void readOpenCV(vpImage<vpRGBa> &I, const std::string &filename)
114 {
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;
120 #else
121  int flags = CV_LOAD_IMAGE_COLOR;
122 #endif
123  cv::Mat Ip = cv::imread(filename.c_str(), flags);
124  if (!Ip.empty())
126  else
127  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
128 #endif
129 }
130 
131 void readOpenCV(vpImage<float> &I, const std::string &filename)
132 {
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;
138 #else
139  int flags = CV_LOAD_IMAGE_COLOR;
140 #endif
141  cv::Mat Ip = cv::imread(filename.c_str(), flags);
142  if (!Ip.empty())
144  else
145  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
146 #else
147  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
148 #endif
149 }
150 
151 void readOpenCV(vpImage<vpRGBf> &I, const std::string &filename)
152 {
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;
158 #else
159  int flags = CV_LOAD_IMAGE_COLOR;
160 #endif
161  cv::Mat Ip = cv::imread(filename.c_str(), flags);
162  if (!Ip.empty())
164  else
165  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
166 #else
167  throw(vpImageException(vpImageException::ioError, "Can't read the image"));
168 #endif
169 }
170 
177 void readPNGfromMemOpenCV(const std::vector<unsigned char> &buffer, vpImage<unsigned char> &I)
178 {
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);
183 }
184 
191 void readPNGfromMemOpenCV(const std::vector<unsigned char> &buffer, vpImage<vpRGBa> &I_color)
192 {
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);
195  vpImageConvert::convert(img, I_color);
196 }
197 
205 void writeOpenCV(const vpImage<unsigned char> &I, const std::string &filename, int quality)
206 {
207  cv::Mat Ip;
209 
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);
214 }
215 
223 void writeOpenCV(const vpImage<vpRGBa> &I, const std::string &filename, int quality)
224 {
225  cv::Mat Ip;
227 
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);
232 }
233 
234 void writeOpenCV(const vpImage<float> &I, const std::string &filename)
235 {
236  cv::Mat Ip;
238 
239  cv::imwrite(filename.c_str(), Ip);
240 }
241 
242 void writeOpenCV(const vpImage<vpRGBf> &I, const std::string &filename)
243 {
244  cv::Mat Ip;
246 
247  cv::imwrite(filename.c_str(), Ip);
248 }
249 
256 void writePNGtoMemOpenCV(const vpImage<unsigned char> &I, std::vector<unsigned char> &buffer)
257 {
258  cv::Mat1b img(I.getRows(), I.getCols(), I.bitmap);
259  bool result = cv::imencode(".png", img, buffer);
260 
261  if (!result) {
262  std::string message = "Cannot write png to memory";
264  }
265 }
266 
274 void writePNGtoMemOpenCV(const vpImage<vpRGBa> &I_color, std::vector<unsigned char> &buffer, bool saveAlpha)
275 {
276  const int height = I_color.getRows();
277  const int width = I_color.getCols();
278  const int channels = saveAlpha ? 4 : 3;
279 
280  if (saveAlpha) {
281  cv::Mat4b img(height, width, reinterpret_cast<cv::Vec4b *>(I_color.bitmap));
282  // No need to perform RGB to BGR conversion
283  bool result = cv::imencode(".png", img, buffer);
284 
285  if (!result) {
286  std::string message = "Cannot write png to memory";
288  }
289  }
290  else {
291  unsigned char *bitmap = new unsigned char[height * width * channels];
292  vpImageConvert::RGBaToRGB(reinterpret_cast<unsigned char *>(I_color.bitmap), bitmap, height*width);
293 
294  cv::Mat3b img(height, width, reinterpret_cast<cv::Vec3b *>(bitmap));
295  // No need to perform RGB to BGR conversion
296  bool result = cv::imencode(".png", img, buffer);
297  delete[] bitmap;
298 
299  if (!result) {
300  std::string message = "Cannot write png to memory";
302  }
303  }
304 }
305 
306 END_VISP_NAMESPACE
307 
308 #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.
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:544
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