Visual Servoing Platform version 3.5.0
vpImageIoOpenCV.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2022 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * OpenCV backend for image I/O operations.
33 *
34 *****************************************************************************/
35
41#include "vpImageIoBackend.h"
42
43#ifdef VISP_HAVE_OPENCV
44#if (VISP_HAVE_OPENCV_VERSION >= 0x030000) // Require opencv >= 3.0.0
45# include <opencv2/imgcodecs.hpp>
46#elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
47# include <opencv2/core/core.hpp>
48# include <opencv2/highgui/highgui.hpp>
49# include <opencv2/imgproc/imgproc.hpp>
50#elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
51# include <opencv2/core/core.hpp>
52# include <opencv2/highgui/highgui.hpp>
53# include <opencv2/highgui/highgui_c.h>
54# include <opencv2/legacy/legacy.hpp>
55#else
56# include <highgui.h>
57#endif
58#endif
59
60#include <visp3/core/vpImageConvert.h>
61
62
63#if defined(VISP_HAVE_OPENCV)
64
80void readOpenCV(vpImage<unsigned char> &I, const std::string &filename)
81{
82#if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
83#if VISP_HAVE_OPENCV_VERSION >= 0x030200
84 int flags = cv::IMREAD_GRAYSCALE | cv::IMREAD_IGNORE_ORIENTATION;
85#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
86 int flags = cv::IMREAD_GRAYSCALE;
87#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
88 int flags = CV_LOAD_IMAGE_GRAYSCALE;
89#endif
90 cv::Mat Ip = cv::imread(filename.c_str(), flags);
91 if (!Ip.empty())
93 else
94 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
95#else
96 IplImage *Ip = NULL;
97 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
98 if (Ip != NULL)
100 else
101 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
102 cvReleaseImage(&Ip);
103#endif
104}
105
123void readOpenCV(vpImage<vpRGBa> &I, const std::string &filename)
124{
125#if defined(VISP_HAVE_OPENCV) && VISP_HAVE_OPENCV_VERSION >= 0x020100
126#if VISP_HAVE_OPENCV_VERSION >= 0x030200
127 int flags = cv::IMREAD_COLOR | cv::IMREAD_IGNORE_ORIENTATION;
128#elif VISP_HAVE_OPENCV_VERSION >= 0x030000
129 int flags = cv::IMREAD_COLOR;
130#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
131 int flags = CV_LOAD_IMAGE_COLOR;
132#endif
133 cv::Mat Ip = cv::imread(filename.c_str(), flags);
134 if (!Ip.empty())
136 else
137 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
138#else
139 IplImage *Ip = NULL;
140 Ip = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
141 if (Ip != NULL)
143 else
144 throw(vpImageException(vpImageException::ioError, "Can't read the image"));
145 cvReleaseImage(&Ip);
146#endif
147}
148
156void writeOpenCV(const vpImage<unsigned char> &I, const std::string &filename, int quality)
157{
158#if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
159 cv::Mat Ip;
161
162 std::vector<int> compression_params;
163 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
164 compression_params.push_back(quality);
165 cv::imwrite(filename.c_str(), Ip, compression_params);
166#else
167 IplImage *Ip = NULL;
169
170 cvSaveImage(filename.c_str(), Ip);
171
172 cvReleaseImage(&Ip);
173 (void)quality;
174#endif
175}
176
184void writeOpenCV(const vpImage<vpRGBa> &I, const std::string &filename, int quality)
185{
186#if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
187 cv::Mat Ip;
189
190 std::vector<int> compression_params;
191 compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
192 compression_params.push_back(quality);
193 cv::imwrite(filename.c_str(), Ip, compression_params);
194#else
195 IplImage *Ip = NULL;
197
198 cvSaveImage(filename.c_str(), Ip);
199
200 cvReleaseImage(&Ip);
201 (void)quality;
202#endif
203}
204
205#endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emited by the vpImage class and its derivates.