1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/PTdecode/CImg-1.3.0/examples/use_skeleton.cpp Mon Aug 03 14:09:20 2009 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* 1.5 + # 1.6 + # File : use_skeleton.cpp 1.7 + # ( C++ source file ) 1.8 + # 1.9 + # Description : Example of use for the CImg plugin 'plugins/skeleton.h'. 1.10 + # This file is a part of the CImg Library project. 1.11 + # ( http://cimg.sourceforge.net ) 1.12 + # 1.13 + # Copyright : Francois-Xavier Dupe 1.14 + # ( http://www.greyc.ensicaen.fr/~fdupe/ ) 1.15 + # 1.16 + # License : CeCILL v2.0 1.17 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.18 + # 1.19 + # This software is governed by the CeCILL license under French law and 1.20 + # abiding by the rules of distribution of free software. You can use, 1.21 + # modify and/ or redistribute the software under the terms of the CeCILL 1.22 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.23 + # "http://www.cecill.info". 1.24 + # 1.25 + # As a counterpart to the access to the source code and rights to copy, 1.26 + # modify and redistribute granted by the license, users are provided only 1.27 + # with a limited warranty and the software's author, the holder of the 1.28 + # economic rights, and the successive licensors have only limited 1.29 + # liability. 1.30 + # 1.31 + # In this respect, the user's attention is drawn to the risks associated 1.32 + # with loading, using, modifying and/or developing or reproducing the 1.33 + # software by the user in light of its specific status of free software, 1.34 + # that may mean that it is complicated to manipulate, and that also 1.35 + # therefore means that it is reserved for developers and experienced 1.36 + # professionals having in-depth computer knowledge. Users are therefore 1.37 + # encouraged to load and test the software's suitability as regards their 1.38 + # requirements in conditions enabling the security of their systems and/or 1.39 + # data to be ensured and, more generally, to use and operate it in the 1.40 + # same conditions as regards security. 1.41 + # 1.42 + # The fact that you are presently reading this means that you have had 1.43 + # knowledge of the CeCILL license and that you accept its terms. 1.44 + # 1.45 +*/ 1.46 + 1.47 +#include <queue> 1.48 +#define cimg_plugin "plugins/skeleton.h" 1.49 +#include "../CImg.h" 1.50 +using namespace cimg_library; 1.51 + 1.52 +#ifndef cimg_imagepath 1.53 +#define cimg_imagepath "img/" 1.54 +#endif 1.55 + 1.56 +int main (int argc, char **argv) { 1.57 + 1.58 + cimg_usage("Compute the skeleton of a shape, using Hamilton-Jacobi equations"); 1.59 + 1.60 + // Read command line arguments 1.61 + cimg_help("Input/Output options\n" 1.62 + "--------------------"); 1.63 + const char* file_i = cimg_option("-i",cimg_imagepath "milla.bmp","Input (black&white) image"); 1.64 + const int median = cimg_option("-median",0,"Apply median filter"); 1.65 + const bool invert = cimg_option("-inv",false,"Invert image values"); 1.66 + const char* file_o = cimg_option("-o",(char*)0,"Output skeleton image"); 1.67 + const bool display = cimg_option("-visu",true,"Display results"); 1.68 + 1.69 + cimg_help("Skeleton computation parameters\n" 1.70 + "-------------------------------"); 1.71 + const float thresh = cimg_option("-t",-0.3f,"Threshold"); 1.72 + const bool curve = cimg_option("-curve",false,"Create medial curve"); 1.73 + 1.74 + cimg_help("Torsello correction parameters\n" 1.75 + "------------------------------"); 1.76 + const bool correction = cimg_option("-corr",false,"Torsello correction"); 1.77 + const float dlt1 = 2; 1.78 + const float dlt2 = cimg_option("-dlt",1.0f,"Discrete step"); 1.79 + 1.80 + cimg_help("Sampling parameters\n" 1.81 + "-------------------"); 1.82 + const float sX = cimg_option("-sizeX",1.0f,"X-Size of the pixel/voxel"); 1.83 + const float sY = cimg_option("-sizeY",1.0f,"Y-Size of the pixel/voxel"); 1.84 + const float sZ = cimg_option("-sizeZ",1.0f,"Z-Size of the pixel/voxel"); 1.85 + 1.86 + // Load the image (forcing it to be scalar with 2 values { 0,1 }). 1.87 + CImg<unsigned int> image0(file_i), image = image0.get_pointwise_norm().quantize(2).normalize(0,1); 1.88 + if (median) image.blur_median(median); 1.89 + if (invert) (image-=1)*=-1; 1.90 + if (display) (image0.get_normalize(0,255)<<image.get_normalize(0,255)).display("Input image - Binary image"); 1.91 + 1.92 + // Compute distance map. 1.93 + CImgList<float> visu; 1.94 + CImg<float> distance = image.get_distance(0,sX,sY,sZ); 1.95 + if (display) visu.insert(distance); 1.96 + 1.97 + // Compute the gradient of the distance function, and the flux (divergence) of the gradient field. 1.98 + const CImgList<float> grad = distance.get_gradient("xyz"); 1.99 + CImg<float> flux = image.get_flux(grad,sY,sZ); 1.100 + if (display) visu.insert(flux); 1.101 + 1.102 + // Use the Torsello correction of the flux if necessary. 1.103 + if (correction) { 1.104 + CImg<float> 1.105 + logdensity = image.get_logdensity(distance,grad,flux,dlt1), 1.106 + nflux = image.get_corrected_flux(logdensity,grad,flux,dlt2); 1.107 + if (display) visu.insert(logdensity).insert(nflux); 1.108 + flux = nflux; 1.109 + } 1.110 + 1.111 + if (visu) { 1.112 + cimglist_apply(visu,normalize)(0,255); 1.113 + visu.display(visu.size==2?"Distance function - Flux":"Distance function - Flux - Log-density - Corrected flux"); 1.114 + } 1.115 + 1.116 + // Compute the skeleton 1.117 + const CImg<unsigned int> skel = image.get_skeleton(flux,distance,curve,thresh); 1.118 + if (display) { 1.119 + (image0.resize(-100,-100,1,3)*=0.7f).get_shared_channel(1)|=skel*255.0; 1.120 + image0.draw_image(0,0,0,0,image*255.0,0.5f).display("Image + Skeleton"); 1.121 + } 1.122 + 1.123 + // Save output image if necessary. 1.124 + if (file_o) skel.save(file_o); 1.125 + 1.126 + return 0; 1.127 +}