#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030500) && defined(VISP_HAVE_THREADS)
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
#include <thread>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
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 num = 0, den1 = 0, den2 = 0;
for (
unsigned int i = 0; i < I1.
getSize(); 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 num = 0, den1 = 0, den2 = 0;
for (
unsigned int i = 0; i < I1.
getSize(); 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));
}
}
TEST_CASE("Test grayscale JPEG image loading", "[image_I/O]")
{
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesJpeg[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Approx(ccThreshJPG));
};
}
}
TEST_CASE("Test RGBA JPEG image loading", "[image_I/O]")
{
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesJpeg[j] + " backend")
{
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesJpeg[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc >= Approx(ccThreshJPG));
};
}
}
TEST_CASE("Test grayscale PNG image loading", "[image_I/O]")
{
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Approx(ccThreshPNG));
};
}
}
TEST_CASE("Test RGBA PNG image loading", "[image_I/O]")
{
for (size_t j = 0; j < backends.size(); j++) {
SECTION(backendNamesPng[j] + " backend")
{
double cc = computePearsonCC(I_ref, I);
std::cout << backendNamesPng[j] << " backend Pearson correlation coefficient: " << cc << std::endl;
CHECK(cc == Approx(ccThreshPNG));
};
}
}
TEST_CASE("Test grayscale JPEG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
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 >= Approx(ccThreshJPG));
};
}
}
TEST_CASE("Test RGBA JPEG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
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 >= Approx(ccThreshJPG));
};
}
}
TEST_CASE("Test grayscale PNG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
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 == Approx(ccThreshPNG));
};
}
}
TEST_CASE("Test RGBA PNG image saving", "[image_I/O]")
{
std::string directory_filename_tmp =
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 == Approx(ccThreshPNG));
};
}
}
TEST_CASE("Test grayscale in-memory PNG image encoding/decoding", "[image_I/O]")
{
for (size_t i = 0; i < backendsInMemory.size(); i++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
std::vector<unsigned char> buffer;
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
CHECK(I_ref == I);
};
}
};
}
}
TEST_CASE("Test color in-memory PNG image encoding/decoding", "[image_I/O]")
{
for (size_t i = 0; i < backendsInMemory.size(); i++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
{
std::vector<unsigned char> buffer;
const bool saveAlpha = false;
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
CHECK(I_ref == I);
};
}
}
{
std::vector<unsigned char> buffer;
const bool saveAlpha = true;
REQUIRE(!buffer.empty());
for (size_t j = 0; j < backendsInMemory.size(); j++) {
SECTION(backendNamesPngInMemory[i] + " backend")
{
CHECK(I_ref == I);
};
}
}
};
}
}
int main(int argc, char *argv[])
{
Catch::Session session;
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.
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg-turbo.
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
double getSum(const vpImage< bool > *p_mask=nullptr, unsigned int *nbValidPoints=nullptr) const
Compute the sum of image intensities.
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")