41 #include <visp3/core/vpHistogram.h> 42 #include <visp3/core/vpImage.h> 43 #include <visp3/core/vpIoTools.h> 44 #include <visp3/io/vpImageIo.h> 45 #include <visp3/io/vpParseArgv.h> 55 #define GETOPTARGS "cdi:t:h" 65 void usage(
const char *name,
const char *badparam, std::string ipath)
71 %s [-i <input image path>] [-t <nb threads>]\n\ 77 -i <input image path> %s\n\ 78 Set image input path.\n\ 79 From this path read \"Klimt/Klimt.ppm\"\n\ 81 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 82 variable produces the same behaviour than using\n\ 86 Set the number of threads to use for the computation.\n\ 88 Print the help.\n\n", ipath.c_str());
91 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
105 bool getOptions(
int argc,
const char **argv, std::string &ipath,
unsigned int &nbThreads)
116 nbThreads = (
unsigned int)atoi(optarg_);
119 usage(argv[0], NULL, ipath);
128 usage(argv[0], optarg_, ipath);
134 if ((c == 1) || (c == -1)) {
136 usage(argv[0], NULL, ipath);
137 std::cerr <<
"ERROR: " << std::endl;
138 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
152 unsigned int histogramSum(
const vpImage<unsigned char> &I,
const unsigned int nbBins,
const unsigned int nbThreads)
154 unsigned int sum = 0;
157 histogram.
calculate(I, nbBins, nbThreads);
159 for (
unsigned int cpt = 0; cpt < histogram.
getSize(); cpt++) {
160 sum += histogram[cpt];
175 histogram_single_threaded.
calculate(I, nbBins, 1);
178 histogram_multi_threaded.
calculate(I, nbBins, 4);
180 unsigned int sum = 0;
181 for (
unsigned int cpt = 0; cpt < nbBins; cpt++) {
182 if (histogram_single_threaded[cpt] != histogram_multi_threaded[cpt]) {
183 std::cerr <<
"histogram_single_threaded[" << cpt <<
"]=" << histogram_single_threaded[cpt]
184 <<
" ; histogram_multi_threaded[" << cpt <<
"]=" << histogram_multi_threaded[cpt] << std::endl;
189 sum += histogram_single_threaded[cpt];
193 std::cerr <<
"Sum of histogram is different with the image size!" << std::endl;
200 int main(
int argc,
const char **argv)
203 std::string env_ipath;
204 std::string opt_ipath;
206 std::string filename;
207 unsigned int nbThreads = 4;
214 if (!env_ipath.empty())
218 if (getOptions(argc, argv, opt_ipath, nbThreads) ==
false) {
223 if (!opt_ipath.empty())
228 if (!opt_ipath.empty() && !env_ipath.empty()) {
229 if (ipath != env_ipath) {
230 std::cout << std::endl <<
"WARNING: " << std::endl;
231 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 232 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
233 <<
" we skip the environment variable." << std::endl;
238 if (opt_ipath.empty() && env_ipath.empty()) {
239 usage(argv[0], NULL, ipath);
240 std::cerr << std::endl <<
"ERROR:" << std::endl;
241 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
242 <<
" environment variable to specify the location of the " << std::endl
243 <<
" image path where test images are located." << std::endl
257 std::cout <<
"Read image: " << filename << std::endl;
262 int nbIterations = 100;
263 unsigned int nbBins = 256;
264 unsigned int sum_single_thread = 0;
265 unsigned int sum_single_multithread = 0;
268 for (
int iteration = 0; iteration < nbIterations; iteration++) {
269 sum_single_thread = histogramSum(I, nbBins, 1);
274 for (
int iteration = 0; iteration < nbIterations; iteration++) {
275 sum_single_multithread = histogramSum(I, nbBins, nbThreads);
279 std::cout <<
"sum_single_thread=" << sum_single_thread <<
" ; t_single_thread=" << t_single_thread
280 <<
" ms ; mean=" << t_single_thread / (double)nbIterations <<
" ms" << std::endl;
281 std::cout <<
"sum_single_multithread=" << sum_single_multithread <<
" ; t_multithread=" << t_multithread
282 <<
" ms ; mean=" << t_multithread / (double)nbIterations <<
" ms" << std::endl;
283 std::cout <<
"Speed-up=" << t_single_thread / (double)t_multithread <<
"X" << std::endl;
285 if (sum_single_thread != I.
getSize() || sum_single_multithread != I.
getSize()) {
286 std::cerr <<
"Problem with histogram!" << std::endl;
291 if (!compareHistogram(I, nbBins)) {
292 std::cerr <<
"Histogram are different!" << std::endl;
300 if (histogram.
getSize() == 256) {
301 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
302 if (histogram[cpt] != 0) {
303 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
304 <<
" but should be zero!" << std::endl;
308 std::cerr <<
"Bad histogram size!" << std::endl;
316 if (histogram.
getSize() == 256) {
317 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
319 if (histogram[cpt] != I_test.
getSize()) {
320 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
321 <<
" but should be: " << I_test.
getSize() << std::endl;
325 if (histogram[cpt] != 0) {
326 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
327 <<
" but should be zero!" << std::endl;
332 std::cerr <<
"Bad histogram size!" << std::endl;
340 if (histogram.
getSize() == 256) {
341 for (
unsigned int cpt = 0; cpt < 256; cpt++) {
343 if (histogram[cpt] != I_test.
getSize()) {
344 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
345 <<
" but should be: " << I_test.
getSize() << std::endl;
349 if (histogram[cpt] != 0) {
350 std::cerr <<
"Problem with histogram computation: histogram[" << cpt <<
"]=" << histogram[cpt]
351 <<
" but should be zero!" << std::endl;
356 std::cerr <<
"Bad histogram size!" << std::endl;
360 std::cout <<
"testHistogram is OK!" << std::endl;
363 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
unsigned int getWidth() const
error that can be emited by ViSP classes.
Class to compute a gray level image histogram.
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
const char * what() const
unsigned int getSize() const
void calculate(const vpImage< unsigned char > &I, const unsigned int nbins=256, const unsigned int nbThreads=1)
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getHeight() const