Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-npz.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
32
33#include <visp3/core/vpConfig.h>
34#include <iostream>
35
36// Check if cxx14 or higher
37#if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L))) \
38 && defined(VISP_HAVE_DISPLAY) && defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
39
40#include <memory>
41#include <complex>
42#include <visp3/core/vpIoTools.h>
43#include <visp3/io/vpImageIo.h>
44#include <visp3/gui/vpDisplayFactory.h>
45
46int main()
47{
48#ifdef ENABLE_VISP_NAMESPACE
49 using namespace VISP_NAMESPACE_NAME;
50#endif
51
52 {
54 const std::string save_string = "Open Source Visual Servoing Platform";
55 std::vector<char> vec_save_string(save_string.begin(), save_string.end());
57
59 const std::string npz_filename = "tutorial_npz_read_write.npz";
60 const std::string identifier = "My string data";
61 visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() }, "w");
63
65 const std::string identifier2 = "My string data 2";
66 visp::cnpy::npz_save(npz_filename, identifier2, save_string, "a");
68 }
69 {
71 const std::string npz_filename = "tutorial_npz_read_write.npz";
72 visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
74
76 const std::string identifier = "My string data";
77 if (npz_data.find(identifier) != npz_data.end()) {
78 visp::cnpy::NpyArray arr_string_data = npz_data[identifier];
79 std::vector<char> vec_arr_string_data = arr_string_data.as_vec<char>();
80 const std::string read_string(vec_arr_string_data.begin(), vec_arr_string_data.end());
81 std::cout << "Read string: " << read_string << std::endl;
82 }
84
86 const std::string identifier2 = "My string data 2";
87 if (npz_data.find(identifier2) != npz_data.end()) {
88 visp::cnpy::NpyArray arr_string_data = npz_data[identifier2];
89 const std::string read_string2 = arr_string_data.as_utf8_string_vec()[0];
90 std::cout << "Read string 2: " << read_string2 << std::endl;
91 }
93 }
94
95 {
97 const std::string npz_filename = "tutorial_npz_read_write.npz";
98 const std::string int_identifier = "My int data";
99 int int_data = 99;
100 visp::cnpy::npz_save(npz_filename, int_identifier, &int_data, { 1 }, "w");
101
102 const std::string double_identifier = "My double data";
103 double double_data = 3.14;
104 visp::cnpy::npz_save(npz_filename, double_identifier, &double_data, { 1 }, "a");
105
106 const std::string complex_identifier = "My complex data";
107 std::complex<double> complex_data(int_data, double_data);
108 visp::cnpy::npz_save(npz_filename, complex_identifier, &complex_data, { 1 }, "a");
110 }
111 {
113 const std::string npz_filename = "tutorial_npz_read_write.npz";
114 visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
115 const std::string int_identifier = "My int data";
116 const std::string double_identifier = "My double data";
117 const std::string complex_identifier = "My complex data";
118
119 visp::cnpy::npz_t::iterator it_int = npz_data.find(int_identifier);
120 visp::cnpy::npz_t::iterator it_double = npz_data.find(double_identifier);
121 visp::cnpy::npz_t::iterator it_complex = npz_data.find(complex_identifier);
122 if (it_int != npz_data.end() && it_double != npz_data.end() && it_complex != npz_data.end()) {
123 visp::cnpy::NpyArray arr_data_int = it_int->second;
124 visp::cnpy::NpyArray arr_data_double = it_double->second;
125 visp::cnpy::NpyArray arr_data_complex = it_complex->second;
126
127 int int_data = *arr_data_int.data<int>();
128 double double_data = *arr_data_double.data<double>();
129 std::complex<double> complex_data = *arr_data_complex.data<std::complex<double>>();
130 std::cout << "Read int data: " << int_data << std::endl;
131 std::cout << "Read double data: " << double_data << std::endl;
132 std::cout << "Read complex data, real: " << complex_data.real() << " ; imag: " << complex_data.imag() << std::endl;
133 }
135 }
136
137 {
139 vpImage<vpRGBa> img;
140 const std::string img_filename = "ballons.jpg";
141 vpImageIo::read(img, img_filename);
144 if (img.getSize() != 0) {
145 const std::string npz_filename = "tutorial_npz_read_write.npz";
146 const std::string img_identifier = "My color image";
147 visp::cnpy::npz_save(npz_filename, img_identifier, &img.bitmap[0], { img.getRows(), img.getCols() }, "w");
148 }
150 }
151 {
153 const std::string npz_filename = "tutorial_npz_read_write.npz";
154 visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
155 const std::string img_identifier = "My color image";
156 visp::cnpy::npz_t::iterator it_img = npz_data.find(img_identifier);
157
158 if (it_img != npz_data.end()) {
159 visp::cnpy::NpyArray arr_data_img = it_img->second;
160 const bool copy_data = false;
161 vpImage<vpRGBa> img(arr_data_img.data<vpRGBa>(), static_cast<unsigned int>(arr_data_img.shape[0]), static_cast<unsigned int>(arr_data_img.shape[1]), copy_data);
162 std::cout << "Img: " << img.getWidth() << "x" << img.getHeight() << std::endl;
163
164 std::shared_ptr<vpDisplay> ptr_display = vpDisplayFactory::createDisplay(img);
165
167 vpDisplay::displayText(img, 20, 20, "vpImage<vpRGBa>", vpColor::red);
168 vpDisplay::flush(img);
170 }
172 }
173
174 {
176 vpImage<vpRGBa> img;
177 const std::string img_filename = "ballons.jpg";
178 vpImageIo::read(img, img_filename);
179 if (img.getSize() != 0) {
180 std::vector<unsigned char> vec_data_img;
181 vec_data_img.resize(3*img.getSize());
182 vpImageConvert::RGBaToRGB(reinterpret_cast<unsigned char *>(img.bitmap), vec_data_img.data(),
183 img.getSize());
184
185 const std::string npz_filename = "tutorial_npz_read_write.npz";
186 const std::string img_identifier = "My RGB image";
187 visp::cnpy::npz_save(npz_filename, img_identifier, &vec_data_img[0], { img.getRows(), img.getCols(), 3 }, "w");
188 }
190 }
191 {
193 const std::string npz_filename = "tutorial_npz_read_write.npz";
194 visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
195 const std::string img_identifier = "My RGB image";
196 visp::cnpy::npz_t::iterator it_img = npz_data.find(img_identifier);
197
198 if (it_img != npz_data.end()) {
199 visp::cnpy::NpyArray arr_data_img = it_img->second;
200 vpImage<vpRGBa> img(static_cast<unsigned int>(arr_data_img.shape[0]), static_cast<unsigned int>(arr_data_img.shape[1]));
201 vpImageConvert::RGBToRGBa(arr_data_img.data<unsigned char>(), reinterpret_cast<unsigned char *>(img.bitmap),
202 img.getSize());
203
204 std::shared_ptr<vpDisplay> ptr_display = vpDisplayFactory::createDisplay(img);
205
207 vpDisplay::displayText(img, 20, 20, "RGBToRGBa", vpColor::red);
208 vpDisplay::flush(img);
210 }
212 }
213
214 return EXIT_SUCCESS;
215}
216#else
217int main()
218{
219 std::cerr << "This tutorial requires C++ version >= C++14." << std::endl;
220 std::cerr << "This tutorial requires display (X11 or GDI) capability." << std::endl;
221#ifndef VISP_HAVE_MINIZ
222 std::cerr << "This tutorial requires having enabled npz I/O functions." << std::endl;
223#endif
224 return EXIT_FAILURE;
225}
226#endif
static const vpColor red
Definition vpColor.h:198
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
VISP_EXPORT npz_t npz_load(const std::string &fname)
std::map< std::string, NpyArray > npz_t
Definition vpIoTools.h:177
VISP_EXPORT void npz_save(const std::string &zipname, std::string fname, const std::vector< std::string > &data_vec, const std::vector< size_t > &shape, const std::string &mode="w")
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
std::vector< size_t > shape
Definition vpIoTools.h:170
std::vector< T > as_vec() const
Definition vpIoTools.h:124
std::vector< std::string > as_utf8_string_vec() const
Definition vpIoTools.h:138