1 // This is mul/vimt/tests/test_find_peaks.cxx
2 #include <iostream>
3 #include <vector>
4 #include "testlib/testlib_test.h"
5 #ifdef _MSC_VER
6 #  include "vcl_msvc_warnings.h"
7 #endif
8 #include "vxl_config.h" // for vxl_byte
9 #include <vimt/algo/vimt_find_peaks.h>
10 #include "vgl/vgl_point_2d.h"
11 
test_find_peaks_byte()12 static void test_find_peaks_byte()
13 {
14   vimt_image_2d_of<vxl_byte> image0;
15   image0.image().set_size(10,10);
16   image0.image().fill(10);
17   image0.image()(3,7)=18;  // One peak
18   image0.image()(7,5)=19;  // Another peak
19 
20   std::vector<vgl_point_2d<unsigned> > im_peaks;
21   std::vector<vxl_byte> peak_values;
22   vimt_find_image_peaks(im_peaks, peak_values, image0.image(), vxl_byte(12), 2);
23 
24   TEST("Number of peaks",im_peaks.size(),2);
25   TEST_NEAR("Peak 0",(im_peaks[0]-vgl_point_2d<unsigned>(7,5)).sqr_length(),0,1e-6);
26   TEST_NEAR("Peak 1",(im_peaks[1]-vgl_point_2d<unsigned>(3,7)).sqr_length(),0,1e-6);
27 
28   vimt_find_image_peaks_3x3(im_peaks,peak_values,image0.image());
29 
30   TEST("Number of peaks",im_peaks.size(),2);
31   TEST_NEAR("Peak 0",(im_peaks[0]-vgl_point_2d<unsigned>(7,5)).sqr_length(),0,1e-6);
32   TEST("Peak 0 value",peak_values[0],19);
33   TEST_NEAR("Peak 1",(im_peaks[1]-vgl_point_2d<unsigned>(3,7)).sqr_length(),0,1e-6);
34   TEST("Peak 1 value",peak_values[1],18);
35 
36   std::vector<vgl_point_2d<double> > w_peaks;
37   vimt_transform_2d w2i;
38   w2i.set_translation(2,3);
39   image0.set_world2im(w2i);
40   vimt_find_world_peaks_3x3(w_peaks,image0);
41 
42   TEST("Number of peaks",w_peaks.size(),2);
43   TEST_NEAR("Peak 0",(w_peaks[0]-vgl_point_2d<double>(5,2)).sqr_length(),0,1e-6);
44   TEST_NEAR("Peak 1",(w_peaks[1]-vgl_point_2d<double>(1,4)).sqr_length(),0,1e-6);
45 
46   vgl_point_2d<unsigned> im_p1 = vimt_find_max(image0.image());
47   TEST_NEAR("Global peak",(im_p1-vgl_point_2d<unsigned>(7,5)).sqr_length(),0,1e-6);
48 
49   vgl_point_2d<double> w_p1 = vimt_find_max(image0);
50   TEST_NEAR("Global peak",(w_p1-vgl_point_2d<double>(5,2)).sqr_length(),0,1e-6);
51 }
52 
test_find_peaks()53 static void test_find_peaks()
54 {
55   test_find_peaks_byte();
56 }
57 
58 TESTMAIN(test_find_peaks);
59