Mon, 03 Aug 2009 23:39:53 +0100
add basic test routine for Ptouch library
1 /*
2 #
3 # File : use_skeleton.cpp
4 # ( C++ source file )
5 #
6 # Description : Example of use for the CImg plugin 'plugins/skeleton.h'.
7 # This file is a part of the CImg Library project.
8 # ( http://cimg.sourceforge.net )
9 #
10 # Copyright : Francois-Xavier Dupe
11 # ( http://www.greyc.ensicaen.fr/~fdupe/ )
12 #
13 # License : CeCILL v2.0
14 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15 #
16 # This software is governed by the CeCILL license under French law and
17 # abiding by the rules of distribution of free software. You can use,
18 # modify and/ or redistribute the software under the terms of the CeCILL
19 # license as circulated by CEA, CNRS and INRIA at the following URL
20 # "http://www.cecill.info".
21 #
22 # As a counterpart to the access to the source code and rights to copy,
23 # modify and redistribute granted by the license, users are provided only
24 # with a limited warranty and the software's author, the holder of the
25 # economic rights, and the successive licensors have only limited
26 # liability.
27 #
28 # In this respect, the user's attention is drawn to the risks associated
29 # with loading, using, modifying and/or developing or reproducing the
30 # software by the user in light of its specific status of free software,
31 # that may mean that it is complicated to manipulate, and that also
32 # therefore means that it is reserved for developers and experienced
33 # professionals having in-depth computer knowledge. Users are therefore
34 # encouraged to load and test the software's suitability as regards their
35 # requirements in conditions enabling the security of their systems and/or
36 # data to be ensured and, more generally, to use and operate it in the
37 # same conditions as regards security.
38 #
39 # The fact that you are presently reading this means that you have had
40 # knowledge of the CeCILL license and that you accept its terms.
41 #
42 */
44 #include <queue>
45 #define cimg_plugin "plugins/skeleton.h"
46 #include "../CImg.h"
47 using namespace cimg_library;
49 #ifndef cimg_imagepath
50 #define cimg_imagepath "img/"
51 #endif
53 int main (int argc, char **argv) {
55 cimg_usage("Compute the skeleton of a shape, using Hamilton-Jacobi equations");
57 // Read command line arguments
58 cimg_help("Input/Output options\n"
59 "--------------------");
60 const char* file_i = cimg_option("-i",cimg_imagepath "milla.bmp","Input (black&white) image");
61 const int median = cimg_option("-median",0,"Apply median filter");
62 const bool invert = cimg_option("-inv",false,"Invert image values");
63 const char* file_o = cimg_option("-o",(char*)0,"Output skeleton image");
64 const bool display = cimg_option("-visu",true,"Display results");
66 cimg_help("Skeleton computation parameters\n"
67 "-------------------------------");
68 const float thresh = cimg_option("-t",-0.3f,"Threshold");
69 const bool curve = cimg_option("-curve",false,"Create medial curve");
71 cimg_help("Torsello correction parameters\n"
72 "------------------------------");
73 const bool correction = cimg_option("-corr",false,"Torsello correction");
74 const float dlt1 = 2;
75 const float dlt2 = cimg_option("-dlt",1.0f,"Discrete step");
77 cimg_help("Sampling parameters\n"
78 "-------------------");
79 const float sX = cimg_option("-sizeX",1.0f,"X-Size of the pixel/voxel");
80 const float sY = cimg_option("-sizeY",1.0f,"Y-Size of the pixel/voxel");
81 const float sZ = cimg_option("-sizeZ",1.0f,"Z-Size of the pixel/voxel");
83 // Load the image (forcing it to be scalar with 2 values { 0,1 }).
84 CImg<unsigned int> image0(file_i), image = image0.get_pointwise_norm().quantize(2).normalize(0,1);
85 if (median) image.blur_median(median);
86 if (invert) (image-=1)*=-1;
87 if (display) (image0.get_normalize(0,255)<<image.get_normalize(0,255)).display("Input image - Binary image");
89 // Compute distance map.
90 CImgList<float> visu;
91 CImg<float> distance = image.get_distance(0,sX,sY,sZ);
92 if (display) visu.insert(distance);
94 // Compute the gradient of the distance function, and the flux (divergence) of the gradient field.
95 const CImgList<float> grad = distance.get_gradient("xyz");
96 CImg<float> flux = image.get_flux(grad,sY,sZ);
97 if (display) visu.insert(flux);
99 // Use the Torsello correction of the flux if necessary.
100 if (correction) {
101 CImg<float>
102 logdensity = image.get_logdensity(distance,grad,flux,dlt1),
103 nflux = image.get_corrected_flux(logdensity,grad,flux,dlt2);
104 if (display) visu.insert(logdensity).insert(nflux);
105 flux = nflux;
106 }
108 if (visu) {
109 cimglist_apply(visu,normalize)(0,255);
110 visu.display(visu.size==2?"Distance function - Flux":"Distance function - Flux - Log-density - Corrected flux");
111 }
113 // Compute the skeleton
114 const CImg<unsigned int> skel = image.get_skeleton(flux,distance,curve,thresh);
115 if (display) {
116 (image0.resize(-100,-100,1,3)*=0.7f).get_shared_channel(1)|=skel*255.0;
117 image0.draw_image(0,0,0,0,image*255.0,0.5f).display("Image + Skeleton");
118 }
120 // Save output image if necessary.
121 if (file_o) skel.save(file_o);
123 return 0;
124 }