#include <visp3/core/vpConfig.h>
#include <iostream>
#if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L))) \
&& (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
#include <memory>
#include <complex>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayX.h>
int main()
{
#ifdef ENABLE_VISP_NAMESPACE
#endif
{
const std::string save_string = "Open Source Visual Servoing Platform";
std::vector<char> vec_save_string(save_string.begin(), save_string.end());
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string identifier = "My string data";
visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() },
"w");
}
{
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string identifier = "My string data";
if (npz_data.find(identifier) != npz_data.end()) {
std::vector<char> vec_arr_string_data = arr_string_data.
as_vec<
char>();
const std::string read_string = std::string(vec_arr_string_data.begin(), vec_arr_string_data.end());
std::cout << "Read string: " << read_string << std::endl;
}
}
{
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string int_identifier = "My int data";
int int_data = 99;
const std::string double_identifier = "My double data";
double double_data = 3.14;
const std::string complex_identifier = "My complex data";
std::complex<double> complex_data(int_data, double_data);
}
{
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string int_identifier = "My int data";
const std::string double_identifier = "My double data";
const std::string complex_identifier = "My complex data";
visp::cnpy::npz_t::iterator it_int = npz_data.find(int_identifier);
visp::cnpy::npz_t::iterator it_double = npz_data.find(double_identifier);
visp::cnpy::npz_t::iterator it_complex = npz_data.find(complex_identifier);
if (it_int != npz_data.end() && it_double != npz_data.end() && it_complex != npz_data.end()) {
int int_data = *arr_data_int.
data<
int>();
double double_data = *arr_data_double.
data<
double>();
std::complex<double> complex_data = *arr_data_complex.
data<std::complex<double>>();
std::cout << "Read int data: " << int_data << std::endl;
std::cout << "Read double data: " << double_data << std::endl;
std::cout << "Read complex data, real: " << complex_data.real() << " ; imag: " << complex_data.imag() << std::endl;
}
}
{
const std::string img_filename = "ballons.jpg";
if (img.getSize() != 0) {
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string img_identifier = "My color image";
visp::cnpy::npz_save(npz_filename, img_identifier, &img.bitmap[0], { img.getRows(), img.getCols() },
"w");
}
}
{
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string img_identifier = "My color image";
visp::cnpy::npz_t::iterator it_img = npz_data.find(img_identifier);
if (it_img != npz_data.end()) {
const bool copy_data = false;
std::cout << "Img: " << img.getWidth() << "x" << img.getHeight() << std::endl;
std::unique_ptr<vpDisplay> ptr_display;
#if defined(VISP_HAVE_X11)
ptr_display = std::make_unique<vpDisplayX>(img);
#elif defined(VISP_HAVE_GDI)
ptr_display = std::make_unique<vpDisplayGDI>(img);
#endif
}
}
{
const std::string img_filename = "ballons.jpg";
if (img.getSize() != 0) {
std::vector<unsigned char> vec_data_img;
vec_data_img.resize(3*img.getSize());
img.getSize());
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string img_identifier = "My RGB image";
visp::cnpy::npz_save(npz_filename, img_identifier, &vec_data_img[0], { img.getRows(), img.getCols(), 3 },
"w");
}
}
{
const std::string npz_filename = "tutorial_npz_read_write.npz";
const std::string img_identifier = "My RGB image";
visp::cnpy::npz_t::iterator it_img = npz_data.find(img_identifier);
if (it_img != npz_data.end()) {
img.getSize());
std::unique_ptr<vpDisplay> ptr_display;
#if defined(VISP_HAVE_X11)
ptr_display = std::make_unique<vpDisplayX>(img);
#elif defined(VISP_HAVE_GDI)
ptr_display = std::make_unique<vpDisplayGDI>(img);
#endif
}
}
return EXIT_SUCCESS;
}
#else
int main()
{
std::cerr << "This tutorial requires C++ version >= C++14." << std::endl;
std::cerr << "This tutorial requires display (X11 or GDI) capability." << std::endl;
#ifndef VISP_HAVE_MINIZ
std::cerr << "This tutorial requires having enabled npz I/O functions." << std::endl;
#endif
return EXIT_FAILURE;
}
#endif
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)
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
VISP_EXPORT npz_t npz_load(std::string fname)
std::map< std::string, NpyArray > npz_t
std::vector< size_t > shape
std::vector< T > as_vec() const