blob: 7004b1f8b3a1442f49fcd2777ab0d55f32d38c79 [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*
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.eu )
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*/
43
44#include <queue>
45#define cimg_plugin "plugins/skeleton.h"
46#include "CImg.h"
47using namespace cimg_library;
48#ifndef cimg_imagepath
49#define cimg_imagepath "img/"
50#endif
51
52// Main procedure
53//----------------
54int main (int argc, char **argv) {
55
56 cimg_usage("Compute the skeleton of a shape, using Hamilton-Jacobi equations");
57
58 // Read command line arguments
59 cimg_help("Input/Output options\n"
60 "--------------------");
61 const char* file_i = cimg_option("-i",cimg_imagepath "milla.bmp","Input (black&white) image");
62 const int median = cimg_option("-median",0,"Apply median filter");
63 const bool invert = cimg_option("-inv",false,"Invert image values");
64 const char* file_o = cimg_option("-o",(char*)0,"Output skeleton image");
65 const bool display = cimg_option("-visu",true,"Display results");
66
67 cimg_help("Skeleton computation parameters\n"
68 "-------------------------------");
69 const float thresh = cimg_option("-t",-0.3f,"Threshold");
70 const bool curve = cimg_option("-curve",false,"Create medial curve");
71
72 cimg_help("Torsello correction parameters\n"
73 "------------------------------");
74 const bool correction = cimg_option("-corr",false,"Torsello correction");
75 const float dlt1 = 2;
76 const float dlt2 = cimg_option("-dlt",1.0f,"Discrete step");
77
78 // Load the image (forcing it to be scalar with 2 values { 0,1 }).
79 CImg<unsigned int> image0(file_i), image = image0.get_norm().quantize(2).normalize(0.0f,1.0f).round();
80 if (median) image.blur_median(median);
81 if (invert) (image-=1)*=-1;
82 if (display) (image0.get_normalize(0,255),image.get_normalize(0,255)).display("Input image - Binary image");
83
84 // Compute distance map.
85 CImgList<float> visu;
86 CImg<float> distance = image.get_distance(0);
87 if (display) visu.insert(distance);
88
89 // Compute the gradient of the distance function, and the flux (divergence) of the gradient field.
90 const CImgList<float> grad = distance.get_gradient("xyz");
91 CImg<float> flux = image.get_flux(grad,1,1);
92 if (display) visu.insert(flux);
93
94 // Use the Torsello correction of the flux if necessary.
95 if (correction) {
96 CImg<float>
97 logdensity = image.get_logdensity(distance,grad,flux,dlt1),
98 nflux = image.get_corrected_flux(logdensity,grad,flux,dlt2);
99 if (display) visu.insert(logdensity).insert(nflux);
100 flux = nflux;
101 }
102
103 if (visu) {
104 cimglist_apply(visu,normalize)(0,255);
105 visu.display(visu.size()==2?"Distance function - Flux":"Distance function - Flux - Log-density - Corrected flux");
106 }
107
108 // Compute the skeleton
109 const CImg<unsigned int> skel = image.get_skeleton(flux,distance,curve,thresh);
110 if (display) {
111 (image0.resize(-100,-100,1,3)*=0.7f).get_shared_channel(1)|=skel*255.0;
112 image0.draw_image(0,0,0,0,image*255.0,0.5f).display("Image + Skeleton");
113 }
114
115 // Save output image if necessary.
116 if (file_o) skel.save(file_o);
117
118 return 0;
119}