Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
perfColorConversion.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:
* Benchmark color image conversion.
*/
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2) && defined(VISP_HAVE_THREADS)
#include <catch_amalgamated.hpp>
#include "common.hpp"
#include <thread>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
static std::string ipath = vpIoTools::getViSPImagesDataPath();
static std::string imagePathColor = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
static std::string imagePathGray = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
static int nThreads = 0;
TEST_CASE("Benchmark rgba to grayscale (naive code)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
BENCHMARK("Benchmark rgba to grayscale (naive code)")
{
common_tools::RGBaToGrayRef(reinterpret_cast<unsigned char *>(I.bitmap), I_gray.bitmap, I.getSize());
return I_gray;
};
}
TEST_CASE("Benchmark rgba to grayscale (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
BENCHMARK("Benchmark rgba to grayscale (ViSP)")
{
vpImageConvert::convert(I, I_gray, nThreads);
return I_gray;
};
}
TEST_CASE("Benchmark grayscale to rgba (naive code)", "[benchmark]")
{
vpImageIo::read(I, imagePathGray);
vpImage<vpRGBa> I_color(I.getHeight(), I.getWidth());
BENCHMARK("Benchmark grayscale to rgba (naive code)")
{
common_tools::grayToRGBaRef(I.bitmap, reinterpret_cast<unsigned char *>(I_color.bitmap), I.getSize());
return I_color;
};
}
TEST_CASE("Benchmark grayscale to rgba (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathGray);
vpImage<vpRGBa> I_color(I.getHeight(), I.getWidth());
BENCHMARK("Benchmark grayscale to rgba (ViSP)")
{
return I_color;
};
}
TEST_CASE("Benchmark split RGBa (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
BENCHMARK("Benchmark split RGBa (ViSP)")
{
vpImageConvert::split(I, &R, &G, &B, &A);
return R;
};
}
TEST_CASE("Benchmark merge to RGBa (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
vpImageConvert::split(I, &R, &G, &B, &A);
vpImage<vpRGBa> I_merge(I.getHeight(), I.getWidth());
BENCHMARK("Benchmark merge to RGBa (ViSP)")
{
vpImageConvert::merge(&R, &G, &B, &A, I_merge);
return I_merge;
};
}
TEST_CASE("Benchmark bgr to grayscale (naive code)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgr;
common_tools::RGBaToBGR(I, bgr);
BENCHMARK("Benchmark bgr to grayscale (naive code)")
{
common_tools::BGRToGrayRef(bgr.data(), reinterpret_cast<unsigned char *>(I_gray.bitmap), I_gray.getWidth(),
I_gray.getHeight(), false);
return I_gray;
};
}
TEST_CASE("Benchmark bgr to grayscale (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgr;
common_tools::RGBaToBGR(I, bgr);
BENCHMARK("Benchmark bgr to grayscale (ViSP)")
{
vpImageConvert::BGRToGrey(bgr.data(), I_gray.bitmap, I.getWidth(), I.getHeight(), false, nThreads);
return I_gray;
};
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
SECTION("OpenCV Mat type")
{
cv::Mat img;
BENCHMARK("Benchmark bgr to grayscale (ViSP + OpenCV Mat type)")
{
vpImageConvert::convert(img, I_gray, false, nThreads);
return I_gray;
};
}
#endif
}
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
TEST_CASE("Benchmark bgr to grayscale (OpenCV)", "[benchmark]")
{
cv::Mat img = cv::imread(imagePathColor);
cv::Mat img_gray(img.size(), CV_8UC1);
BENCHMARK("Benchmark bgr to grayscale (OpenCV)")
{
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
return img_gray;
};
}
#endif
TEST_CASE("Benchmark bgr to rgba (naive code)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgr;
common_tools::RGBaToBGR(I, bgr);
vpImage<vpRGBa> I_bench(I.getHeight(), I.getWidth());
BENCHMARK("Benchmark bgr to rgba (naive code)")
{
common_tools::BGRToRGBaRef(bgr.data(), reinterpret_cast<unsigned char *>(I_bench.bitmap), I.getWidth(),
I.getHeight(), false);
return I_bench;
};
}
TEST_CASE("Benchmark bgr to rgba (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgr;
common_tools::RGBaToBGR(I, bgr);
SECTION("Check BGR to RGBa conversion")
{
common_tools::BGRToRGBaRef(bgr.data(), reinterpret_cast<unsigned char *>(ref.bitmap), I.getWidth(), I.getHeight(),
false);
vpImageConvert::BGRToRGBa(bgr.data(), reinterpret_cast<unsigned char *>(rgba.bitmap), I.getWidth(), I.getHeight(),
false);
CHECK((rgba == ref));
}
BENCHMARK("Benchmark bgr to rgba (ViSP)")
{
vpImageConvert::BGRToRGBa(bgr.data(), reinterpret_cast<unsigned char *>(I_rgba.bitmap), I.getWidth(), I.getHeight(),
false);
return I_rgba;
};
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
SECTION("OpenCV Mat type")
{
cv::Mat img;
BENCHMARK("Benchmark bgr to rgba (ViSP + OpenCV Mat type)")
{
return I_rgba;
};
}
#endif
}
TEST_CASE("Benchmark bgra to rgba (naive code)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgra;
common_tools::RGBaToBGRa(I, bgra);
vpImage<vpRGBa> I_bench(I.getHeight(), I.getWidth());
BENCHMARK("Benchmark bgra to rgba (naive code)")
{
common_tools::BGRaToRGBaRef(bgra.data(), reinterpret_cast<unsigned char *>(I_bench.bitmap), I.getWidth(),
I.getHeight(), false);
return I_bench;
};
}
TEST_CASE("Benchmark bgra to rgba (ViSP)", "[benchmark]")
{
vpImageIo::read(I, imagePathColor);
std::vector<unsigned char> bgra;
common_tools::RGBaToBGRa(I, bgra);
SECTION("Check BGRa to RGBa conversion")
{
common_tools::BGRaToRGBaRef(bgra.data(), reinterpret_cast<unsigned char *>(ref.bitmap), I.getWidth(), I.getHeight(),
false);
vpImageConvert::BGRaToRGBa(bgra.data(), reinterpret_cast<unsigned char *>(rgba.bitmap), I.getWidth(), I.getHeight(),
false);
CHECK((rgba == ref));
}
BENCHMARK("Benchmark bgra to rgba (ViSP)")
{
vpImageConvert::BGRaToRGBa(bgra.data(), reinterpret_cast<unsigned char *>(I_rgba.bitmap), I.getWidth(),
I.getHeight(), false);
return I_rgba;
};
}
int main(int argc, char *argv[])
{
Catch::Session session;
bool runBenchmark = false;
auto cli = session.cli()
| Catch::Clara::Opt(runBenchmark)["--benchmark"]("run benchmark?")
| Catch::Clara::Opt(imagePathColor, "imagePathColor")["--imagePathColor"]("Path to color image")
| Catch::Clara::Opt(imagePathGray, "imagePathColor")["--imagePathGray"]("Path to gray image")
| Catch::Clara::Opt(nThreads, "nThreads")["--nThreads"]("Number of threads");
session.cli(cli);
session.applyCommandLine(argc, argv);
if (runBenchmark) {
vpImage<vpRGBa> I_color;
vpImageIo::read(I_color, imagePathColor);
std::cout << "imagePathColor:\n\t" << imagePathColor << "\n\t" << I_color.getWidth() << "x" << I_color.getHeight()
<< std::endl;
vpImageIo::read(I_gray, imagePathGray);
std::cout << "imagePathGray:\n\t" << imagePathGray << "\n\t" << I_gray.getWidth() << "x" << I_gray.getHeight()
<< std::endl;
std::cout << "nThreads: " << nThreads << " / available threads: " << std::thread::hardware_concurrency()
<< std::endl;
int numFailed = session.run();
return numFailed;
}
return EXIT_SUCCESS;
}
#else
#include <iostream>
int main() { return EXIT_SUCCESS; }
#endif
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false, unsigned int nThreads=0)
static void BGRToRGBa(unsigned char *bgr, unsigned char *rgba, unsigned int width, unsigned int height, bool flip=false)
static void BGRaToRGBa(unsigned char *bgra, unsigned char *rgba, unsigned int width, unsigned int height, bool flip=false)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
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 createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427