Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
vpImageIoOpenCV.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 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) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC))
60
76void readOpenCV(vpImage<unsigned char> &I, const std::string &filename)
77{
78#if defined(VISP_HAVE_OPENCV)
79#if VISP_HAVE_OPENCV_VERSION >= 0x030200
80 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
81#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
82 int flags = cv::IMREAD_GRAYSCALE;
83#else
84 int flags = CV_LOAD_IMAGE_GRAYSCALE;
85#endif
86 cv::Mat Ip = cv::imread(filename.c_str(), flags);
87 if (!Ip.empty())
89 else
90 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
91#endif
92}
93
111void readOpenCV(vpImage<vpRGBa> &I, const std::string &filename)
112{
113#if defined(VISP_HAVE_OPENCV)
114#if VISP_HAVE_OPENCV_VERSION >= 0x030200
115 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
116#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
117 int flags = cv::IMREAD_COLOR;
118#else
119 int flags = CV_LOAD_IMAGE_COLOR;
120#endif
121 cv::Mat Ip = cv::imread(filename.c_str(), flags);
122 if (!Ip.empty())
124 else
125 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
126#endif
127}
128
129void readOpenCV(vpImage<float> &I, const std::string &filename)
130{
131#if defined(VISP_HAVE_OPENCV)
132#if VISP_HAVE_OPENCV_VERSION >= 0x030200
133 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
134#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
135 int flags = cv::IMREAD_COLOR;
136#else
137 int flags = CV_LOAD_IMAGE_COLOR;
138#endif
139 cv::Mat Ip = cv::imread(filename.c_str(), flags);
140 if (!Ip.empty())
142 else
143 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
144#else
145 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
146#endif
147}
148
149void readOpenCV(vpImage<vpRGBf> &I, const std::string &filename)
150{
151#if defined(VISP_HAVE_OPENCV)
152#if VISP_HAVE_OPENCV_VERSION >= 0x030200
153 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
154#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
155 int flags = cv::IMREAD_COLOR;
156#else
157 int flags = CV_LOAD_IMAGE_COLOR;
158#endif
159 cv::Mat Ip = cv::imread(filename.c_str(), flags);
160 if (!Ip.empty())
162 else
163 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
164#else
165 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
166#endif
167}
168
176void writeOpenCV(const vpImage<unsigned char> &I, const std::string &filename, int quality)
177{
178 cv::Mat Ip;
180
181 std::vector<int> compression_params;
182 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
183 compression_params.push_back(quality);
184 cv::imwrite(filename.c_str(), Ip, compression_params);
185}
186
194void writeOpenCV(const vpImage<vpRGBa> &I, const std::string &filename, int quality)
195{
196 cv::Mat Ip;
198
199 std::vector<int> compression_params;
200 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
201 compression_params.push_back(quality);
202 cv::imwrite(filename.c_str(), Ip, compression_params);
203}
204
205void writeOpenCV(const vpImage<float> &I, const std::string &filename)
206{
207 cv::Mat Ip;
209
210 cv::imwrite(filename.c_str(), Ip);
211}
212
213void writeOpenCV(const vpImage<vpRGBf> &I, const std::string &filename)
214{
215 cv::Mat Ip;
217
218 cv::imwrite(filename.c_str(), Ip);
219}
220
221#endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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:135