Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
catchImageLoadSave.cpp
/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2024 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Check that the different image I/O backends work correctly.
*/
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030500) && defined(VISP_HAVE_THREADS)
#include <catch_amalgamated.hpp>
#include <thread>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
static const std::string ipath = vpIoTools::getViSPImagesDataPath();
static const std::string path = ipath + "/Solvay/Solvay_conference_1927_Version2_640x440";
static const double ccThreshPNG = 1.0;
static const double ccThreshJPG = 0.99;
static const std::vector<vpImageIo::vpImageIoBackendType> backends
{
#if defined(VISP_HAVE_JPEG) && defined(VISP_HAVE_PNG)
#endif
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
#endif
#if defined VISP_HAVE_SIMDLIB
#endif
#if defined VISP_HAVE_STBIMAGE
#endif
};
static const std::vector<std::string> backendNamesJpeg
{
#if defined(VISP_HAVE_JPEG)
"libjpeg",
#endif
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
"OpenCV",
#endif
"simd", "stb"
};
static std::vector<std::string> backendNamesPng
{
#if defined(VISP_HAVE_PNG)
"libpng",
#endif
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
"OpenCV",
#endif
"simd", "stb"
};
static const std::vector<vpImageIo::vpImageIoBackendType> backendsInMemory
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
#endif
#if defined VISP_HAVE_STBIMAGE
#endif
};
static std::vector<std::string> backendNamesPngInMemory
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
"OpenCV",
#endif
#if defined VISP_HAVE_STBIMAGE
"stb"
#endif
};
static const unsigned int imgWidth = 640;
static const unsigned int imgHeight = 440;
namespace
{
double computePearsonCC(const vpImage<unsigned char> &I1, const vpImage<unsigned char> &I2)
{
double m1 = I1.getSum() / I1.getSize();
double m2 = I2.getSum() / I2.getSize();
double num = 0, den1 = 0, den2 = 0;
for (unsigned int i = 0; i < I1.getSize(); i++) {
double x1 = I1.bitmap[i];
double x2 = I2.bitmap[i];
double x1_m = x1 - m1;
double x2_m = x2 - m2;
num += x1_m * x2_m;
den1 += x1_m * x1_m;
den2 += x2_m * x2_m;
}
return num / (std::sqrt(den1) * std::sqrt(den2));
}
double computePearsonCC(const vpImage<vpRGBa> &I1, const vpImage<vpRGBa> &I2)
{
double m1 = I1.getSum() / (3 * I1.getSize());
double m2 = I2.getSum() / (3 * I2.getSize());
double num = 0, den1 = 0, den2 = 0;
for (unsigned int i = 0; i < I1.getSize(); i++) {
double x1 = I1.bitmap[i].R + I1.bitmap[i].G + I1.bitmap[i].B;
double x2 = I2.bitmap[i].R + I2.bitmap[i].G + I2.bitmap[i].B;
double x1_m = x1 - m1;
double x2_m = x2 - m2;
num += x1_m * x2_m;
den1 += x1_m * x1_m;
den2 += x2_m * x2_m;
}
return num / (std::sqrt(den1) * std::sqrt(den2));
}
} // namespace
TEST_CASE("Test grayscale JPEG image loading", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".jpg");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::read(I, path + ".jpg", backends[j]));
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesJpeg[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Catch::Approx(ccThreshJPG));
CHECK(I.getWidth() == imgWidth);
CHECK(I.getHeight() == imgHeight);
};
}
}
TEST_CASE("Test RGBA JPEG image loading", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".jpg");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::read(I, path + ".jpg", backends[j]));
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesJpeg[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Catch::Approx(ccThreshJPG));
CHECK(I.getWidth() == imgWidth);
CHECK(I.getHeight() == imgHeight);
};
}
}
TEST_CASE("Test grayscale PNG image loading", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::read(I, path + ".png", backends[j]));
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Catch::Approx(ccThreshPNG));
CHECK(I.getWidth() == imgWidth);
CHECK(I.getHeight() == imgHeight);
};
}
}
TEST_CASE("Test RGBA PNG image loading", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::read(I, path + ".png", backends[j]));
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Catch::Approx(ccThreshPNG));
CHECK(I.getWidth() == imgWidth);
CHECK(I.getHeight() == imgHeight);
};
}
}
TEST_CASE("Test grayscale JPEG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
vpIoTools::makeDirectory(directory_filename_tmp);
REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
vpImageIo::read(I, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]));
vpImageIo::read(I_read, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg");
double cc = computePearsonCC(I_read, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Catch::Approx(ccThreshJPG));
};
}
REQUIRE(vpIoTools::remove(directory_filename_tmp));
}
TEST_CASE("Test RGBA JPEG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
vpIoTools::makeDirectory(directory_filename_tmp);
REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
vpImageIo::read(I, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]));
vpImageIo::read(I_read, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg");
double cc = computePearsonCC(I_read, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Catch::Approx(ccThreshJPG));
};
}
REQUIRE(vpIoTools::remove(directory_filename_tmp));
}
TEST_CASE("Test grayscale PNG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
vpIoTools::makeDirectory(directory_filename_tmp);
REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
vpImageIo::read(I, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]));
vpImageIo::read(I_read, directory_filename_tmp + "/ViSP_tmp_perf_write.png");
double cc = computePearsonCC(I_read, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Catch::Approx(ccThreshPNG));
};
}
REQUIRE(vpIoTools::remove(directory_filename_tmp));
}
TEST_CASE("Test RGBA PNG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
vpIoTools::makeDirectory(directory_filename_tmp);
REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
vpImageIo::read(I, path + ".png");
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
CHECK_NOTHROW(vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]));
vpImageIo::read(I_read, directory_filename_tmp + "/ViSP_tmp_perf_write.png");
double cc = computePearsonCC(I_read, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Catch::Approx(ccThreshPNG));
};
}
REQUIRE(vpIoTools::remove(directory_filename_tmp));
}
TEST_CASE("Test grayscale in-memory PNG image encoding/decoding", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".png");
REQUIRE(I_ref.getSize() > 0);
for (size_t i = 0; i < backendsInMemory.size(); i++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
std::vector<unsigned char> buffer;
vpImageIo::writePNGtoMem(I_ref, buffer, backendsInMemory[i]);
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
vpImageIo::readPNGfromMem(buffer, I, backendsInMemory[j]);
CHECK(I_ref == I);
};
}
};
}
}
TEST_CASE("Test color in-memory PNG image encoding/decoding", "[image_I/O]")
{
vpImageIo::read(I_ref, path + ".png");
REQUIRE(I_ref.getSize() > 0);
for (size_t i = 0; i < backendsInMemory.size(); i++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
{
std::vector<unsigned char> buffer;
const bool saveAlpha = false;
vpImageIo::writePNGtoMem(I_ref, buffer, backendsInMemory[i], saveAlpha);
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
vpImageIo::readPNGfromMem(buffer, I, backendsInMemory[j]);
CHECK(I_ref == I);
};
}
}
{
std::vector<unsigned char> buffer;
const bool saveAlpha = true;
vpImageIo::writePNGtoMem(I_ref, buffer, backendsInMemory[i], saveAlpha);
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
vpImageIo::readPNGfromMem(buffer, I, backendsInMemory[j]);
CHECK(I_ref == I);
};
}
}
};
}
}
int main(int argc, char *argv[])
{
Catch::Session session; // There must be exactly one instance
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
#include <iostream>
int main() { return EXIT_SUCCESS; }
#endif
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition: vpImageIo.h:133
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition: vpImageIo.h:132
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg-turbo.
Definition: vpImageIo.h:130
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition: vpImageIo.h:131
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1409
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1524
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
double getSum(const vpImage< bool > *p_mask=nullptr, unsigned int *nbValidPoints=nullptr) const
Compute the sum of image intensities.
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
Type * bitmap
points toward the bitmap
Definition: vpImage.h:135
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string getTempPath()
Definition: vpIoTools.cpp:189
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:921
static std::string makeTempDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:708
unsigned char B
Blue component.
Definition: vpRGBa.h:169
unsigned char R
Red component.
Definition: vpRGBa.h:167
unsigned char G
Green component.
Definition: vpRGBa.h:168
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")