Visual Servoing Platform  version 3.4.0
testColorConversion.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test image conversion.
33  *
34  *****************************************************************************/
35 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_CATCH2)
45 #define CATCH_CONFIG_RUNNER
46 #include <catch.hpp>
47 #include <visp3/core/vpImageConvert.h>
48 #include "common.hpp"
49 
50 static const double maxMeanPixelError = 1.0;
51 static const unsigned int width = 223, height = 151;
52 
53 TEST_CASE("Gray to RGBa conversion", "[image_conversion]") {
54  vpImage<unsigned char> gray(height, width);
55  common_tools::fill(gray);
56 
57  vpImage<vpRGBa> rgba_ref(height, width);
58  common_tools::grayToRGBaRef(gray.bitmap, reinterpret_cast<unsigned char*>(rgba_ref.bitmap), gray.getSize());
59 
60  vpImage<vpRGBa> rgba;
61  vpImageConvert::convert(gray, rgba);
62  CHECK((rgba == rgba_ref));
63 }
64 
65 TEST_CASE("RGBa to Gray conversion", "[image_conversion]") {
66  vpImage<vpRGBa> rgba(height, width);
67  common_tools::fill(rgba);
68 
69  vpImage<unsigned char> gray_ref(height, width);
70  common_tools::RGBaToGrayRef(reinterpret_cast<unsigned char*>(rgba.bitmap), gray_ref.bitmap, rgba.getSize());
71 
72  vpImage<unsigned char> gray(height, width);
73  vpImageConvert::convert(rgba, gray);
74  double error = 0;
75  CHECK(common_tools::almostEqual(gray_ref, gray, maxMeanPixelError, error));
76  std::cout << "RGBa to Gray conversion, mean error: " << error << std::endl;
77 }
78 
79 TEST_CASE("RGB to Gray conversion", "[image_conversion]") {
80  std::vector<unsigned char> rgb(height*width*3);
81  common_tools::fill(rgb);
82 
83  vpImage<unsigned char> gray_ref(height, width);
84  common_tools::RGBToGrayRef(rgb.data(), gray_ref.bitmap, gray_ref.getWidth(), gray_ref.getHeight(), false);
85 
86  vpImage<unsigned char> gray(height, width);
87  vpImageConvert::RGBToGrey(rgb.data(), gray.bitmap, width, height, false);
88  double error = 0;
89  CHECK(common_tools::almostEqual(gray_ref, gray, maxMeanPixelError, error));
90  std::cout << "RGB to Gray conversion 1, mean error: " << error << std::endl;
91 
92  vpImage<unsigned char> gray2(height, width);
93  vpImageConvert::RGBToGrey(rgb.data(), gray2.bitmap, width*height);
94  CHECK(common_tools::almostEqual(gray_ref, gray2, maxMeanPixelError, error));
95  std::cout << "RGB to Gray conversion 2, mean error: " << error << std::endl;
96 }
97 
98 TEST_CASE("RGB <==> RGBa conversion", "[image_conversion]") {
99  vpImage<vpRGBa> rgba_ref(height, width);
100  common_tools::fill(rgba_ref);
101 
102  std::vector<unsigned char> rgb(height*width*3);
103  vpImageConvert::RGBaToRGB(reinterpret_cast<unsigned char*>(rgba_ref.bitmap), rgb.data(), rgba_ref.getSize());
104 
105  vpImage<vpRGBa> rgba(height, width);
106  vpImageConvert::RGBToRGBa(rgb.data(), reinterpret_cast<unsigned char*>(rgba.bitmap), rgba_ref.getSize());
107 
108  CHECK((rgba == rgba_ref));
109 }
110 
111 #if VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11
112 TEST_CASE("BGR to Gray conversion", "[image_conversion]") {
113  vpImage<vpRGBa> rgba_ref(height, width);
114  common_tools::fill(rgba_ref);
115 
116  vpImage<unsigned char> gray_ref;
117  vpImageConvert::convert(rgba_ref, gray_ref);
118 
119  std::vector<unsigned char> bgr;
120  common_tools::RGBaToBGR(rgba_ref, bgr);
121 
122  vpImage<unsigned char> gray(gray_ref.getHeight(), gray_ref.getWidth());
123  vpImageConvert::BGRToGrey(bgr.data(), gray.bitmap, gray.getWidth(), gray.getHeight());
124 
125  double error = 0;
126  CHECK(common_tools::almostEqual(gray_ref, gray, maxMeanPixelError, error));
127  std::cout << "BGR to Gray conversion, mean error: " << error << std::endl;
128 }
129 #endif
130 
131 TEST_CASE("BGRa to Gray conversion", "[image_conversion]") {
132  vpImage<vpRGBa> rgba_ref(height, width);
133  common_tools::fill(rgba_ref);
134 
135  vpImage<unsigned char> gray_ref;
136  vpImageConvert::convert(rgba_ref, gray_ref);
137 
138  std::vector<unsigned char> bgra;
139  common_tools::RGBaToBGRa(rgba_ref, bgra);
140 
141  vpImage<unsigned char> gray(gray_ref.getHeight(), gray_ref.getWidth());
142  vpImageConvert::BGRaToGrey(bgra.data(), gray.bitmap, gray.getWidth(), gray.getHeight());
143 
144  double error = 0;
145  CHECK(common_tools::almostEqual(gray_ref, gray, maxMeanPixelError, error));
146  std::cout << "BGRa to Gray conversion, mean error: " << error << std::endl;
147 }
148 
149 TEST_CASE("BGRa to RGBa conversion", "[image_conversion]") {
150  vpImage<vpRGBa> rgba_ref(height, width);
151  common_tools::fill(rgba_ref);
152 
153  std::vector<unsigned char> bgra_ref;
154  common_tools::RGBaToBGRa(rgba_ref, bgra_ref);
155 
156  vpImage<vpRGBa> rgba(rgba_ref.getHeight(), rgba_ref.getWidth());
157  vpImageConvert::BGRaToRGBa(bgra_ref.data(), reinterpret_cast<unsigned char*>(rgba.bitmap), rgba.getWidth(), rgba.getHeight());
158 
159  double error = 0;
160  CHECK(common_tools::almostEqual(rgba_ref, rgba, maxMeanPixelError, error));
161  std::cout << "BGRa to RGBa conversion, mean error: " << error << std::endl;
162 }
163 
164 TEST_CASE("Split <==> Merge conversion", "[image_conversion]") {
165  vpImage<vpRGBa> rgba_ref(height, width);
166  common_tools::fill(rgba_ref);
167 
168  vpImage<unsigned char> R, G, B, A;
169  vpImageConvert::split(rgba_ref, &R, &G, &B, &A);
170 
171  vpImage<vpRGBa> rgba;
172  vpImageConvert::merge(&R, &G, &B, &A, rgba);
173 
174  CHECK((rgba == rgba_ref));
175 }
176 
177 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
178 TEST_CASE("OpenCV Mat <==> vpImage conversion", "[image_conversion]") {
179 
180  SECTION("CV_8UC3 to vpRGBa")
181  {
182  cv::Mat img(height, width, CV_8UC3);
183  common_tools::fill(img);
184 
185  vpImage<vpRGBa> rgba_ref(height, width);
186  common_tools::BGRToRGBaRef(img.data, reinterpret_cast<unsigned char*>(rgba_ref.bitmap), img.cols, img.rows, false);
187 
188  vpImage<vpRGBa> rgba;
189  vpImageConvert::convert(img, rgba);
190  CHECK((rgba_ref == rgba));
191  }
192 
193  SECTION("CV_8UC1 to vpRGBa")
194  {
195  cv::Mat img(height, width, CV_8UC1);
196  common_tools::fill(img);
197 
198  vpImage<vpRGBa> rgba_ref(height, width);
199  common_tools::grayToRGBaRef(img.data, reinterpret_cast<unsigned char*>(rgba_ref.bitmap), height*width);
200 
201  vpImage<vpRGBa> rgba;
202  vpImageConvert::convert(img, rgba);
203  CHECK((rgba_ref == rgba));
204  }
205 
206  SECTION("CV_8UC3 to unsigned char")
207  {
208  cv::Mat img(height, width, CV_8UC3);
209  common_tools::fill(img);
210 
211  vpImage<unsigned char> gray_ref(height, width);
212  common_tools::BGRToGrayRef(img.data, gray_ref.bitmap, img.cols, img.rows, false);
213 
215  vpImageConvert::convert(img, gray);
216  double error = 0;
217  CHECK(common_tools::almostEqual(gray_ref, gray, maxMeanPixelError, error));
218  std::cout << "BGR to Gray conversion, mean error: " << error << std::endl;
219  }
220 
221  SECTION("CV_8UC1 to unsigned char")
222  {
223  cv::Mat img(height, width, CV_8UC1);
224  common_tools::fill(img);
225 
227  vpImageConvert::convert(img, gray);
228 
229  REQUIRE(gray.getHeight() == height);
230  REQUIRE(gray.getWidth() == width);
231 
232  for (int i = 0; i < img.rows; i++) {
233  for (int j = 0; j < img.cols; j++) {
234  REQUIRE(img.at<uchar>(i, j) == gray[i][j]);
235  }
236  }
237  }
238 }
239 #endif
240 
241 int main(int argc, char *argv[])
242 {
243  Catch::Session session; // There must be exactly one instance
244 
245  // Let Catch (using Clara) parse the command line
246  session.applyCommandLine(argc, argv);
247 
248  int numFailed = session.run();
249 
250  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
251  // This clamping has already been applied, so just return it here
252  // You can also do any post run clean-up here
253  return numFailed;
254 }
255 #else
256 int main()
257 {
258  return 0;
259 }
260 #endif
unsigned int getWidth() const
Definition: vpImage.h:246
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
static void BGRaToRGBa(unsigned char *bgra, unsigned char *rgba, unsigned int width, unsigned int height, bool flip=false)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=NULL)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false)
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 BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false, unsigned int nThreads=0)
static void BGRaToGrey(unsigned char *bgra, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false, unsigned int nThreads=0)
unsigned int getHeight() const
Definition: vpImage.h:188
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)