DGtal  0.9.3
Functions
testImageAdapter.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/images/ImageContainerBySTLVector.h"
#include "DGtal/images/ImageAdapter.h"
#include "DGtal/images/ConstImageAdapter.h"
#include "DGtal/io/colormaps/GrayscaleColorMap.h"
#include "DGtal/io/readers/PGMReader.h"
#include "DGtal/io/boards/Board2D.h"
#include "ConfigTest.h"
Include dependency graph for testImageAdapter.cpp:

Go to the source code of this file.

Functions

bool testSimple ()
 
bool test_g_f_fm1 ()
 
bool test_range_constRange ()
 
bool testImageAdapter ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Martial Tola (marti.nosp@m.al.t.nosp@m.ola@l.nosp@m.iris.nosp@m..cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2012/10/12

Functions for testing class ImageAdapter.

This file is part of the DGtal library.

Definition in file testImageAdapter.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 463 of file testImageAdapter.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), test_g_f_fm1(), test_range_constRange(), testImageAdapter(), testSimple(), and DGtal::trace.

464 {
465  trace.beginBlock ( "Testing class ImageAdapter" );
466  trace.info() << "Args:";
467  for ( int i = 0; i < argc; ++i )
468  trace.info() << " " << argv[ i ];
469  trace.info() << endl;
470 
471  bool res = testSimple() && test_g_f_fm1() && test_range_constRange() && testImageAdapter(); // && ... other tests
472 
473  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
474  trace.endBlock();
475  return res ? 0 : 1;
476 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
bool test_range_constRange()
bool testSimple()
bool testImageAdapter()
bool test_g_f_fm1()
std::ostream & emphase()
std::ostream & info()

◆ test_g_f_fm1()

bool test_g_f_fm1 ( )

[ImageAdapterConstruction]

[ImageAdapterConstruction]

Definition at line 145 of file testImageAdapter.cpp.

References DGtal::Trace::beginBlock(), domain(), DGtal::Trace::endBlock(), image(), DGtal::Trace::info(), DGtal::trace, and DGtal::Trace::warning().

Referenced by main().

146 {
147  unsigned int nbok = 0;
148  unsigned int nb = 0;
149 
150  trace.beginBlock("Testing g, f and fm1 with ImageAdapter");
151 
153 
154  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
155  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
156  *it = 10;
157 
158  trace.info() << "Original image: " << image << endl;
159 
160  //ConstAdapter
163  BOOST_CONCEPT_ASSERT(( concepts::CConstImage< MyImageAdapter > ));
164  functors::Identity idD;
166  MyImageAdapter restimage(image, domain, idD, idV);
167  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
168  nbok += (restimage(Z2i::Point(3,3)) == 3) ? 1 : 0;
169  nb++;
170  trace.info() << "(" << nbok << "/" << nb << ") "
171  << " read access on restricted Image" << endl;
172 
175  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyImageAdapter2 > ));
176 
177  functors::Identity idD_2;
180 
181  MyImageAdapter2 restimage2(image, domain, idD_2, idV_2, idVm1_2);
183 
184  restimage2.setValue(Z2i::Point(2,2), true);
185  nbok += (restimage2(Z2i::Point(2,2)) == true) ? 1 : 0;
186  nb++;
187  trace.info() << "(" << nbok << "/" << nb << ") "
188  << " write on restricted Image 2" << endl;
189 
190  trace.warning()<< "Restricted image 2 at (2,2) = "<< (int)(restimage2)(Z2i::Point(2,2)) << std::endl;
191  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
192  trace.warning()<< "Original image at (3,3) = "<< (image)(Z2i::Point(3,3)) << std::endl;
193 
194  restimage2.setValue(Z2i::Point(2,2), false);
195  trace.warning()<< "Restricted image with false at (2,2) = "<< (int)(restimage2)(Z2i::Point(2,2)) << std::endl;
196  nbok += (restimage2(Z2i::Point(2,2)) == true) ? 1 : 0;
197  nb++;
198  trace.info() << "(" << nbok << "/" << nb << ") "
199  << " write on restricted Image 2" << endl;
200 
201  trace.warning()<< "Restricted image 2 at (2,2) = "<< (restimage2)(Z2i::Point(2,2)) << std::endl;
202  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
203 
205  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyImageAdapter3 > ));
206 
207  MyTransValueFunctor<Z2i::Point> idD_3(Z2i::Point(2,2));
208  functors::Identity idV_3;
209  functors::Identity idVm1_3;
210 
211  MyImageAdapter3 restimage3(image, domain, idD_3, idV_3, idVm1_3);
212 
213  restimage3.setValue(Z2i::Point(2,2), 5);
214  nbok += (image(Z2i::Point(4,4)) == 5) ? 1 : 0;
215  nb++;
216  trace.info() << "(" << nbok << "/" << nb << ") "
217  << " write on restricted Image 3" << endl;
218 
219  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
220  trace.warning()<< "Original image at (4,4) = "<< (image)(Z2i::Point(4,4)) << std::endl;
221  trace.warning()<< "Original image at (5,5) = "<< (image)(Z2i::Point(5,5)) << std::endl;
222 
223  trace.endBlock();
224 
225  return nbok == nb;
226 }
void beginBlock(const std::string &keyword="")
Aim: Define a simple functor that returns a constant value (0 by default).
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
Aim: Define a simple functor using the static cast operator.
double endBlock()
Aim: Define a simple default functor that just returns its argument.
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
Aim: implements an image adapter with a given domain (i.e. a subdomain) and 3 functors : g for domain...
Definition: ImageAdapter.h:109
MyPointD Point
Definition: testClone2.cpp:383
std::ostream & warning()
std::ostream & info()
Aim: implements a const image adapter with a given domain (i.e. a subdomain) and 2 functors : g for d...
Aim: Defines the concept describing a read-only image, which is a refinement of CPointFunctor.
Definition: CConstImage.h:94
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
Image image(domain)

◆ test_range_constRange()

bool test_range_constRange ( )

Definition at line 228 of file testImageAdapter.cpp.

References DGtal::Trace::beginBlock(), domain(), DGtal::Trace::endBlock(), image(), DGtal::Trace::info(), DGtal::HyperRectDomain< TSpace >::size(), and DGtal::trace.

Referenced by main().

229 {
230  unsigned int nbok = 0;
231  unsigned int nb = 0;
232 
233  trace.beginBlock("Testing range/constRange with ImageAdapter");
234 
236 
237  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
238  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
239  *it = 10;
240 
241  trace.info() << "Original image: " << image << endl;
242 
245  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyImageAdapter > ));
246 
247  functors::Identity idD;
248  functors::Identity idV;
249  functors::Identity idVm1;
250 
251  MyImageAdapter restimage(image, domain, idD, idV, idVm1);
252  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
253 
254  // writing values
255  const int maximalValue = domain.size();
256  MyImageAdapter::Range::OutputIterator it = restimage.range().outputIterator();
257  for (int i = 0; i < maximalValue; ++i)
258  *it++ = i;
259 
260  // reading values
261  MyImageAdapter::ConstRange r = restimage.constRange();
262  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") );
263  cout << endl;
264 
265  std::vector<int> to_vector(9);
266  std::copy(r.begin(), r.end(), to_vector.begin());
267  for (int i = 0; i < 9; i++)
268  {
269  if (to_vector[i]==i)
270  {
271  cout << "ok, ";
272  nbok += true ? 1 : 0; nb++;
273  }
274  else
275  {
276  cout << "!ok, ";
277  nbok += false ? 1 : 0; nb++;
278  }
279  }
280 
281  cout << endl;
282 
283  trace.endBlock();
284 
285  return nbok == nb;
286 }
void beginBlock(const std::string &keyword="")
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
Image::ConstRange ConstRange
double endBlock()
Aim: Define a simple default functor that just returns its argument.
Aim: implements an image adapter with a given domain (i.e. a subdomain) and 3 functors : g for domain...
Definition: ImageAdapter.h:109
std::ostream & info()
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
Image image(domain)

◆ testImageAdapter()

bool testImageAdapter ( )

[ImageAdapterWithSubdomain]

[ImageAdapterWithSubdomain]

[ImageAdapterWithSpecificDomain]

[ImageAdapterWithSpecificDomain]

Definition at line 288 of file testImageAdapter.cpp.

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::Trace::beginBlock(), DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::endBlock(), image(), DGtal::Trace::info(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::insertNew(), LibBoard::Board::saveSVG(), and DGtal::trace.

Referenced by main().

289 {
290  unsigned int nbok = 0;
291  unsigned int nb = 0;
292 
293  trace.beginBlock ("Testing ImageAdapter");
294 
297 
298  string filename = testPath + "samples/church-small.pgm";
299  VImage image = PGMReader<VImage>::importPGM(filename);
300  trace.info() << "Imported image: " << image << endl;
301 
302  Board2D aBoard;
303 
304  Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
305  aBoard.saveSVG("church.svg");
306 #ifdef WITH_CAIRO
307  aBoard.saveCairo("church.png", Board2D::CairoPNG);
308 #endif
309 
311  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyImageAdapter > ));
312 
313  nbok += true ? 1 : 0;
314  nb++;
315 
316  // 1) bell_tower
318  Z2i::Point p1( 43, 107 );
319  Z2i::Point p2( 73, 177 );
320  Z2i::Domain domain_bell_tower( p1, p2 );
321 
322  functors::Identity idbtD, idbtV, idbtVm1;
323  MyImageAdapter bell_tower(image, domain_bell_tower, idbtD, idbtV, idbtVm1);
325 
326  trace.info() << "ImageAdapter: " << bell_tower << " " << bell_tower.domain() << std::endl;
327 
328  nbok += bell_tower.isValid() ? 1 : 0;
329  nb++;
330 
331  aBoard.clear();
332  Display2DFactory::drawImage<Gray>(aBoard, bell_tower, (unsigned char)0, (unsigned char)255);
333  aBoard.saveSVG("bell_tower.svg");
334 #ifdef WITH_CAIRO
335  aBoard.saveCairo("bell_tower.png", Board2D::CairoPNG);
336 #endif
337 
338  // 2) cars
339  Z2i::Point p3( 0, 49 );
340  Z2i::Point p4( 58, 72 );
341  Z2i::Domain domain_cars( p3, p4 );
342 
343  functors::Identity idcD;
344  functors::Identity idcV;
345  functors::Identity idcVm1;
346  MyImageAdapter cars(image, domain_cars, idcD, idcV, idcVm1);
347 
348  trace.info() << "ImageAdapter: " << cars << " " << cars.domain() << std::endl;
349 
350  nbok += cars.isValid() ? 1 : 0;
351  nb++;
352 
353  aBoard.clear();
354  Display2DFactory::drawImage<Gray>(aBoard, cars, (unsigned char)0, (unsigned char)255);
355  aBoard.saveSVG("cars.svg");
356 #ifdef WITH_CAIRO
357  aBoard.saveCairo("cars.png", Board2D::CairoPNG);
358 #endif
359 
360  // 3) fill 255 for 'bell_tower' image
361  MyImageAdapter::Domain::ConstIterator bt_it = bell_tower.domain().begin();
362  MyImageAdapter::Domain::ConstIterator bt_itEnd = bell_tower.domain().end();
363  for (; bt_it != bt_itEnd; ++bt_it)
364  {
365  bell_tower.setValue(*bt_it, 255);
366  }
367 
368  aBoard.clear();
369  Display2DFactory::drawImage<Gray>(aBoard, bell_tower, (unsigned char)0, (unsigned char)255);
370  aBoard.saveSVG("bell_tower_after_filling.svg");
371 #ifdef WITH_CAIRO
372  aBoard.saveCairo("bell_tower_after_filling.png", Board2D::CairoPNG);
373 #endif
374 
375  // 4) fill 55 for 'cars' image
376  MyImageAdapter::Domain::ConstIterator c_it = cars.domain().begin();
377  MyImageAdapter::Domain::ConstIterator c_itEnd = cars.domain().end();
378  for (; c_it != c_itEnd; ++c_it)
379  {
380  cars.setValue(*c_it, 55);
381  }
382 
383  aBoard.clear();
384  Display2DFactory::drawImage<Gray>(aBoard, cars, (unsigned char)0, (unsigned char)255);
385  aBoard.saveSVG("cars_after_filling.svg");
386 #ifdef WITH_CAIRO
387  aBoard.saveCairo("cars_after_filling.png", Board2D::CairoPNG);
388 #endif
389 
390  // 5) fill 0 (only for one pixel on two) for 'floor_lamp' image
392  Z2i::Point p5( 56, 33 );
393  Z2i::Point p6( 68, 79 );
394  Z2i::Domain domain_floor_lamp( p5, p6 );
395 
396  // --- DigitalSetDomain
397  Z2i::DigitalSet mySet( domain_floor_lamp );
398 
399  unsigned int i = 0;
400  for ( Z2i::Domain::ConstIterator it = domain_floor_lamp.begin() ;
401  it != domain_floor_lamp.end();
402  ++it, ++i )
403  {
404  if (i%2)
405  mySet.insertNew( *it );
406  }
407 
408  DigitalSetDomain<Z2i::DigitalSet> my_specific_domain_floor_lamp(mySet);
409  // --- DigitalSetDomain
410 
411 
412  typedef ImageAdapter<VImage, DigitalSetDomain<Z2i::DigitalSet>, functors::Identity, VImage::Value, functors::Identity, functors::Identity> MyImageAdapter2;
413  // BOOST_CONCEPT_ASSERT(( CImage< MyImageAdapter2 > )); // pb here
414 
415  functors::Identity idflD, idflV, idflVm1;
416  MyImageAdapter2 floor_lamp(image, my_specific_domain_floor_lamp, idflD, idflV, idflVm1);
418 
419  trace.info() << "ImageAdapter: " << floor_lamp << " " << floor_lamp.domain() << std::endl;
420 
421  nbok += floor_lamp.isValid() ? 1 : 0;
422  nb++;
423 
424  aBoard.clear();
425  Display2DFactory::drawImage<Gray>(aBoard, floor_lamp, (unsigned char)0, (unsigned char)255);
426  aBoard.saveSVG("floor_lamp.svg");
427 #ifdef WITH_CAIRO
428  aBoard.saveCairo("floor_lamp.png", Board2D::CairoPNG);
429 #endif
430 
431  MyImageAdapter2::Domain::ConstIterator f_it = floor_lamp.domain().begin();
432  MyImageAdapter2::Domain::ConstIterator f_itEnd = floor_lamp.domain().end();
433  for (; f_it != f_itEnd; ++f_it)
434  {
435  floor_lamp.setValue(*f_it, 0);
436  }
437 
438  aBoard.clear();
439  Display2DFactory::drawImage<Gray>(aBoard, floor_lamp, (unsigned char)0, (unsigned char)255);
440  aBoard.saveSVG("floor_lamp_after_filling.svg");
441 #ifdef WITH_CAIRO
442  aBoard.saveCairo("floor_lamp_after_filling.png", Board2D::CairoPNG);
443 #endif
444 
445  aBoard.clear();
446  Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
447  aBoard.saveSVG("church_after_filling.svg");
448 #ifdef WITH_CAIRO
449  aBoard.saveCairo("church_after_filling.png", Board2D::CairoPNG);
450 #endif
451 
452  trace.info() << "(" << nbok << "/" << nb << ") "
453  << "true == true" << endl;
454  trace.endBlock();
455 
456  return nbok == nb;
457 }
void beginBlock(const std::string &keyword="")
MyDigitalSurface::ConstIterator ConstIterator
Trace trace
Definition: Common.h:137
Aim: Import a 2D or 3D using the Netpbm formats (ASCII mode).
Definition: PGMReader.h:96
double endBlock()
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
Aim: Define a simple default functor that just returns its argument.
Aim: implements an image adapter with a given domain (i.e. a subdomain) and 3 functors : g for domain...
Definition: ImageAdapter.h:109
Aim: Constructs a domain limited to the given digital set.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
std::ostream & info()
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
Image image(domain)
Aim: This class specializes a &#39;Board&#39; class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70

◆ testSimple()

bool testSimple ( )

Definition at line 53 of file testImageAdapter.cpp.

References DGtal::Trace::beginBlock(), domain(), DGtal::Trace::endBlock(), image(), DGtal::Trace::info(), DGtal::trace, and DGtal::Trace::warning().

Referenced by main().

54 {
55  unsigned int nbok = 0;
56  unsigned int nb = 0;
57 
58  trace.beginBlock("Testing simple ImageAdapter");
59 
61 
62  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
63  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
64  *it = 10;
65 
66  trace.info() << "Original image: " << image << endl;
67 
70  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyImageAdapter > ));
71 
74  functors::Identity idVm1;
75 
76  MyImageAdapter restimage(image, domain, idD, idV, idVm1);
77  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
78 
79  nbok += (restimage(Z2i::Point(3,3)) == 10) ? 1 : 0;
80  nb++;
81  trace.info() << "(" << nbok << "/" << nb << ") "
82  << " read access on restricted Image" << endl;
83 
84  restimage.setValue(Z2i::Point(3,3), 5);
85  nbok += (restimage(Z2i::Point(3,3)) == 5) ? 1 : 0;
86  nb++;
87  trace.info() << "(" << nbok << "/" << nb << ") "
88  << " write on restricted Image" << endl;
89 
90  nbok += (image(Z2i::Point(3,3)) == 5) ? 1 : 0;
91  nb++;
92  trace.info() << "(" << nbok << "/" << nb << ") "
93  << " written on original image" << endl;
94 
95  trace.warning()<< "Original image at (3,3) = "<< (image)(Z2i::Point(3,3)) << std::endl;
96 
97  trace.endBlock();
98 
99  return nbok == nb;
100 }
void beginBlock(const std::string &keyword="")
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
double endBlock()
Aim: Define a simple default functor that just returns its argument.
Aim: implements an image adapter with a given domain (i.e. a subdomain) and 3 functors : g for domain...
Definition: ImageAdapter.h:109
std::ostream & warning()
std::ostream & info()
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
Image image(domain)