Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testImageAddSub.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test image addition / subtraction.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
42 #if defined(VISP_HAVE_CATCH2)
43 #define CATCH_CONFIG_RUNNER
44 #include "common.hpp"
45 #include <catch.hpp>
46 #include <visp3/core/vpImageTools.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/io/vpImageIo.h>
49 
50 #ifdef ENABLE_VISP_NAMESPACE
51 using namespace VISP_NAMESPACE_NAME;
52 #endif
53 
54 TEST_CASE("Test vpImageTools::imageAdd()", "[image_add]")
55 {
56  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
58  vpImageIo::read(I, filepath);
59 
60  SECTION("I + Inull = I")
61  {
62  vpImage<unsigned char> Inull(I.getHeight(), I.getWidth(), 0);
64  vpImageTools::imageAdd(I, Inull, Iadd);
65  CHECK((Iadd == I));
66  }
67 
68  SECTION("I + I without saturation")
69  {
70  const bool saturation = false;
72  common_tools::imageAddRef(I, I, Iref, saturation);
73 
75  vpImageTools::imageAdd(I, I, Iadd, saturation);
76  CHECK((Iadd == Iref));
77  }
78 
79  SECTION("I + I with saturation")
80  {
81  const bool saturation = true;
83  common_tools::imageAddRef(I, I, Iref, saturation);
84 
86  vpImageTools::imageAdd(I, I, Iadd, saturation);
87  CHECK((Iadd == Iref));
88  }
89 }
90 
91 TEST_CASE("Test vpImageTools::imageDifference()", "[image_difference]")
92 {
93  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
95  vpImageIo::read(I, filepath);
96 
97  SECTION("I - Inull = I")
98  {
99  vpImage<unsigned char> Inull(I.getHeight(), I.getWidth(), 0);
101  vpImageTools::imageSubtract(I, Inull, Isub);
102  CHECK((Isub == I));
103  }
104 
105  SECTION("I - I2 without saturation")
106  {
107  const bool saturation = false;
110  common_tools::fill(I2);
111 
112  common_tools::imageSubtractRef(I, I2, Iref, saturation);
113 
115  vpImageTools::imageSubtract(I, I2, Isub, saturation);
116  CHECK((Isub == Iref));
117  }
118 
119  SECTION("I - I2 with saturation ")
120  {
121  const bool saturation = true;
124  common_tools::fill(I2);
125 
126  common_tools::imageSubtractRef(I, I2, Iref, saturation);
127 
129  vpImageTools::imageSubtract(I, I2, Isub, saturation);
130  CHECK((Isub == Iref));
131  }
132 }
133 
134 int main(int argc, char *argv[])
135 {
136  Catch::Session session; // There must be exactly one instance
137 
138  // Let Catch (using Clara) parse the command line
139  session.applyCommandLine(argc, argv);
140 
141  int numFailed = session.run();
142 
143  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
144  // This clamping has already been applied, so just return it here
145  // You can also do any post run clean-up here
146  return numFailed;
147 }
148 #else
149 int main() { return EXIT_SUCCESS; }
150 #endif
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void imageSubtract(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Ires, bool saturate=false)
static void imageAdd(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Ires, bool saturate=false)
unsigned int getWidth() const
Definition: vpImage.h:242
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