Test histogram computation.
Test histogram computation.
#include <stdio.h>
#include <stdlib.h>
#include <visp3/core/vpHistogram.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpUniRand.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#define GETOPTARGS "cdi:t:h"
#ifdef ENABLE_VISP_NAMESPACE
#endif
void usage(const char *name, const char *badparam, std::string ipath)
{
fprintf(stdout, "\n\
Test histogram.\n\
\n\
SYNOPSIS\n\
%s [-i <input image path>] [-t <nb threads>]\n\
[-h]\n \
",
name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-i <input image path> %s\n\
Set image input path.\n\
From this path read \"Klimt/Klimt.ppm\"\n\
image.\n\
Setting the VISP_INPUT_IMAGE_PATH environment\n\
variable produces the same behaviour than using\n\
this option.\n\
\n\
-t <nb threads>\n\
Set the number of threads to use for the computation.\n\
-h\n\
Print the help.\n\n",
ipath.c_str());
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, std::string &ipath, unsigned int &nbThreads)
{
const char *optarg_;
int c;
switch (c) {
case 'i':
ipath = optarg_;
break;
case 't':
nbThreads = static_cast<unsigned int>(atoi(optarg_));
break;
case 'h':
usage(argv[0], nullptr, ipath);
return false;
case 'c':
case 'd':
break;
default:
usage(argv[0], optarg_, ipath);
return false;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], nullptr, ipath);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
{
unsigned int sum = 0;
for (
unsigned int cpt = 0; cpt < histogram.
getSize(); cpt++) {
sum += histogram[cpt];
}
return sum;
}
{
histogram_single_threaded.
calculate(I, nbBins, 1);
histogram_multi_threaded.
calculate(I, nbBins, 4);
unsigned int sum = 0;
for (unsigned int cpt = 0; cpt < nbBins; cpt++) {
if (histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
std::cerr << "histogram_single_threaded[" << cpt << "]=" << histogram_single_threaded[cpt]
<< " ; histogram_multi_threaded[" << cpt << "]=" << histogram_multi_threaded[cpt] << std::endl;
return false;
}
sum += histogram_single_threaded[cpt];
}
if (sum != I.getSize()) {
std::cerr << "Sum of histogram is different with the image size!" << std::endl;
return false;
}
return true;
}
int main(int argc, const char **argv)
{
try {
std::string env_ipath;
std::string opt_ipath;
std::string ipath;
unsigned int nbThreads = 4;
if (!env_ipath.empty())
ipath = env_ipath;
if (getOptions(argc, argv, opt_ipath, nbThreads) == false) {
return EXIT_FAILURE;
}
if (!opt_ipath.empty())
ipath = opt_ipath;
if (!opt_ipath.empty() && !env_ipath.empty()) {
if (ipath != env_ipath) {
std::cout << std::endl << "WARNING: " << std::endl;
std::cout << " Since -i <visp image path=" << ipath << "> "
<< " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
<< " we skip the environment variable." << std::endl;
}
}
if (opt_ipath.empty() && env_ipath.empty()) {
usage(argv[0], nullptr, ipath);
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
<< " environment variable to specify the location of the " << std::endl
<< " image path where test images are located." << std::endl
<< std::endl;
return EXIT_FAILURE;
}
std::cout <<
"Read image: " <<
filename << std::endl;
std::cout << "I=" << I.getWidth() << "x" << I.getHeight() << std::endl;
int nbIterations = 100;
unsigned int nbBins = 256;
unsigned int sum_single_thread = 0;
unsigned int sum_multi_thread = 0;
for (int iteration = 0; iteration < nbIterations; iteration++) {
sum_single_thread = histogramSum(I, nbBins, 1);
}
for (int iteration = 0; iteration < nbIterations; iteration++) {
sum_multi_thread = histogramSum(I, nbBins, nbThreads);
}
std::cout << "sum_single_thread=" << sum_single_thread << " ; t_single_thread=" << t_single_thread
<< " ms ; mean=" << t_single_thread / static_cast<double>(nbIterations) << " ms" << std::endl;
std::cout << "sum_multi_thread (nbThreads=" << nbThreads << ")=" << sum_multi_thread << " ; t_multi_thread=" << t_multi_thread
<< " ms ; mean=" << t_multi_thread / static_cast<double>(nbIterations) << " ms" << std::endl;
std::cout << "Speed-up=" << t_single_thread / static_cast<double>(t_multi_thread) << "X" << std::endl;
if (sum_single_thread != I.getSize() || sum_multi_thread != I.getSize()) {
std::cerr << "Problem with histogram!" << std::endl;
return EXIT_FAILURE;
}
nbBins = 101;
if (!compareHistogram(I, nbBins)) {
std::cerr << "Histogram are different!" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test histogram computation on empty image" << std::endl << std::flush;
for (unsigned int cpt = 0; cpt < 256; cpt++) {
if (histogram[cpt] != 0) {
std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
<< " but should be zero!" << std::endl;
}
}
}
else {
std::cerr << "Bad histogram size!" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test histogram computation on image size < nbThreads" << std::endl << std::flush;
I_test.init(3, 1);
I_test = 100;
for (unsigned int cpt = 0; cpt < 256; cpt++) {
if (cpt == 100) {
if (histogram[cpt] != I_test.getSize()) {
std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
<<
" but should be: " << I_test.
getSize() << std::endl;
return EXIT_FAILURE;
}
}
else {
if (histogram[cpt] != 0) {
std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
<< " but should be zero!" << std::endl;
}
}
}
}
else {
std::cerr << "Bad histogram size!" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test histogram computation on small image size" << std::endl << std::flush;
I_test.init(7, 1);
I_test = 50;
for (unsigned int cpt = 0; cpt < 256; cpt++) {
if (cpt == 50) {
if (histogram[cpt] != I_test.getSize()) {
std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
<<
" but should be: " << I_test.
getSize() << std::endl;
return EXIT_FAILURE;
}
}
else {
if (histogram[cpt] != 0) {
std::cerr << "Problem with histogram computation: histogram[" << cpt << "]=" << histogram[cpt]
<< " but should be zero!" << std::endl;
}
}
}
}
else {
std::cerr << "Bad histogram size!" << std::endl;
return EXIT_FAILURE;
}
unsigned int nbRows = 4, nbCols = 15;
I.init(nbRows, nbCols);
for (
unsigned int r = 0;
r < nbRows; ++
r) {
unsigned int c = 0;
for (
unsigned int i = 1;
i <= 5; ++
i) {
for (
unsigned int count = 0; count <
i; ++count) {
++c;
}
}
}
std::cout << "I:" << std::endl;
std::cout << I << std::endl;
nbBins = 256;
std::vector<unsigned int> expectedNumber(nbBins, 0);
expectedNumber[1] = 1;
expectedNumber[2] = 3;
expectedNumber[3] = 6;
expectedNumber[4] = 10;
expectedNumber[5] = 14;
expectedNumber[6] = 12;
expectedNumber[7] = 9;
expectedNumber[8] = 5;
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histo[bin] != expectedNumber[bin]) {
std::cerr << "Problem with histogram computation: histogram[" << bin << "]=" << histo[bin]
<< " but should be: " << expectedNumber[bin] << std::endl;
return EXIT_FAILURE;
}
}
std::vector<unsigned int> expectedNumberSmooth(nbBins, 0);
expectedNumberSmooth[1] = 1;
expectedNumberSmooth[2] = 3;
expectedNumberSmooth[3] = 6;
expectedNumberSmooth[4] = 10;
expectedNumberSmooth[5] = 12;
expectedNumberSmooth[6] = 11;
expectedNumberSmooth[7] = 8;
expectedNumberSmooth[8] = 4;
expectedNumberSmooth[9] = 1;
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histo[bin] != expectedNumberSmooth[bin]) {
std::cerr << "Problem with smooth computation: histogram[" << bin << "]=" << histo[bin]
<< " but should be: " << expectedNumberSmooth[bin] << std::endl;
return EXIT_FAILURE;
}
}
for (
unsigned int r = 0;
r < nbRows; ++
r) {
for (unsigned int c = 0; c < nbCols; ++c) {
if ((r == 0) || (r == (nbRows - 1))) {
}
if ((c == 0) || (c == (nbCols - 1))) {
}
}
}
std::cout << "I to which is applied the mask:" << std::endl;
for (
unsigned int r = 0;
r < nbRows; ++
r) {
for (unsigned int c = 0; c < nbCols; ++c) {
if (mask[r][c]) {
std::cout << static_cast<int>(I[r][c]);
}
else {
std::cout << "X";
}
std::cout << " ";
}
std::cout << std::endl;
}
std::vector<unsigned int> expectedNumberMask(nbBins, 0);
expectedNumberMask[1] = 1;
expectedNumberMask[2] = 3;
expectedNumberMask[3] = 4;
expectedNumberMask[4] = 5;
expectedNumberMask[5] = 7;
expectedNumberMask[6] = 4;
expectedNumberMask[7] = 5;
expectedNumberMask[8] = 5;
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histoWithMaskMono[bin] != expectedNumberMask[bin]) {
std::cerr << "Problem when using mask: histogram[" << bin << "]=" << histoWithMaskMono[bin]
<< " but should be: " << expectedNumberMask[bin] << std::endl;
return EXIT_FAILURE;
}
if (histoWithMaskMulti[bin] != expectedNumberMask[bin]) {
std::cerr << "Problem when using mask: histogram[" << bin << "]=" << histoWithMaskMulti[bin]
<< " but should be: " << expectedNumberMask[bin] << std::endl;
return EXIT_FAILURE;
}
}
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
nbBins = 8;
double min = 0., max = 1.;
double desiredStep = (max - min) / static_cast<double>(nbBins);
for (
unsigned int r = 0;
r < nbRows; ++
r) {
unsigned int c = 0;
for (
unsigned int i = 1;
i <= 5; ++
i) {
for (
unsigned int count = 0; count <
i; ++count) {
double value = uniRand.uniform(0., desiredStep) + desiredStep *
static_cast<double>(
i +
r - 1);
++c;
}
}
}
std::cout << "Id = " << std::endl;
for (
unsigned int r = 0;
r < nbRows; ++
r) {
for (unsigned int c = 0; c < nbCols; ++c) {
std::cout << std::setprecision(3) << Id[
r][c];
std::cout << " ";
}
std::cout << std::endl;
}
std::vector<unsigned char> expectedNumberDouble(nbBins, 0), expectedNumberDoubleMask(nbBins, 0);
for (
unsigned int i = 0;
i < nbBins; ++
i) {
expectedNumberDouble[
i] = expectedNumber[
i + 1];
expectedNumberDoubleMask[
i] = expectedNumberMask[
i + 1];
}
histoDoubleMono.
calculate(Id, min, max, step, nbBins, 1);
std::cerr << "Problem with histogram computation of floating point images" << std::endl;
std::cout <<
"Computed step: " <<
step <<
" but should be: " << desiredStep << std::endl;
return EXIT_FAILURE;
}
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histoDoubleMono[bin] != expectedNumberDouble[bin]) {
std::cerr << "Problem with monothread histogram computation of floating point images: histogram[" << bin << "]=" << histoDoubleMono[bin]
<< " but should be: " << expectedNumberDouble[bin] << std::endl;
return EXIT_FAILURE;
}
}
histoDoubleMulti.
calculate(Id, min, max, step, nbBins, 2);
std::cerr << "Problem with histogram computation of floating point images" << std::endl;
std::cout <<
"Computed step: " <<
step <<
" but should be: " << desiredStep << std::endl;
return EXIT_FAILURE;
}
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histoDoubleMulti[bin] != expectedNumberDouble[bin]) {
std::cerr << "Problem with multithread histogram computation of floating point images: histogram[" << bin << "]=" << histoDoubleMulti[bin]
<< " but should be: " << expectedNumberDouble[bin] << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "Id with mask= " << std::endl;
for (
unsigned int r = 0;
r < nbRows; ++
r) {
for (unsigned int c = 0; c < nbCols; ++c) {
if (mask[r][c]) {
std::cout << std::setprecision(3) << Id[
r][c];
}
else {
std::cout << "XXXXX";
}
std::cout << " ";
}
std::cout << std::endl;
}
histoDoubleMonoMask.
setMask(&mask);
histoDoubleMonoMask.
calculate(Id, min, max, step, nbBins, 2);
histoDoubleMultiMask.
setMask(&mask);
histoDoubleMultiMask.
calculate(Id, min, max, step, nbBins, 2);
for (unsigned int bin = 0; bin < nbBins; ++bin) {
if (histoDoubleMonoMask[bin] != expectedNumberDoubleMask[bin]) {
std::cerr << "Problem with monothread histogram computation of floating point images using mask: histogram[" << bin << "]=" << histoDoubleMonoMask[bin]
<< " but should be: " << expectedNumberDoubleMask[bin] << std::endl;
return EXIT_FAILURE;
}
if (histoDoubleMultiMask[bin] != expectedNumberDoubleMask[bin]) {
std::cerr << "Problem with multithread histogram computation of floating point images using mask: histogram[" << bin << "]=" << histoDoubleMultiMask[bin]
<< " but should be: " << expectedNumberDoubleMask[bin] << std::endl;
return EXIT_FAILURE;
}
}
#endif
std::cout << "testHistogram is OK!" << std::endl;
return EXIT_SUCCESS;
}
std::cerr <<
"Catch an exception: " <<
e.what() << std::endl;
return EXIT_FAILURE;
}
}
error that can be emitted by ViSP classes.
Class to compute a gray level image histogram.
void smooth(unsigned int fsize=3)
void calculate(const vpImage< unsigned char > &I, unsigned int nbins=256, unsigned int nbThreads=1)
void setMask(const vpImage< bool > *p_mask)
Set a mask to ignore pixels for which the mask is false.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
static bool equal(double x, double y, double threshold=0.001)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class for generating random numbers with uniform probability density.
VISP_EXPORT double measureTimeMs()