Statistiques
| Révision :

root / src / edge_indicator.cpp @ 9

Historique | Voir | Annoter | Télécharger (4,62 ko)

1
/*
2

3
To compile :
4
 g++ -o edge_indicator edge_indicator.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 -fopenmp 
5
 Needs CImg.h
6

7
To execute :
8
 ./edge_indicator img K sigma [transfer_type]
9

10
 img : image in .inr or .inr.gz
11
 K : proportional to width of cell walls (0.1 - 0.3)
12
 sigma : amount of gaussian blur (float)
13
 transfer_type (optional) : by default 1/(1+x/K) type edge function. If "exp" is marked, then exp(-x/K) edge function is used
14
 
15
 * authors : Annamaria Kiss (RDP)
16
 * ENS-Lyon
17
 * 
18

19
*/
20
#include <iostream>
21
#include <math.h>
22
#include <sstream>
23
#include <fstream>
24

    
25
#include "CImg.h"
26
#include <omp.h>
27

    
28
using namespace cimg_library;
29
using namespace std;
30

    
31
#include "lsm_lib.h"
32

    
33
//-----------------------------------------------------------------------------
34
//Main
35
//-----------------------------------------------------------------------------
36
int main (int argc, char* argv[])
37
{
38
 
39
  if(argc<4 || argc>5)
40
    {
41
      cout<<"!! wrong number of arguments"<<endl;
42
      cout<<"how to execute : ./edge_indicator img K sigma [transfer_type]"<<endl;
43
      cout<<" img : image in .inr or .inr.gz"<<endl;
44
          cout<<" K : proportional to width of cell walls (0.1 - 0.3)"<<endl;
45
          cout<<" sigma : amount of gaussian blur (float)"<<endl;
46
          cout<<" transfer_type (optional) : by default 'hill' that is 1/(1+x/K) type edge function. If 'exp' is marked, then exp(-x/K) edge function is used"<<endl; 
47
      return 0;
48
    }
49

    
50
  string filter_type="hill";
51
 
52

    
53
  if (argc==5)
54
        {
55
        if (string(argv[4]).compare("exp")==0)
56
                {filter_type="exp";}
57
        }
58

    
59
  
60
  //Open image with name in argument
61
  //image is converted to unsigned char (0 to 255) and then to float
62
  string name=argv[1];
63
  CImg<> img1;
64
  CImg<char> description;
65
  float tailleVoxel[3] = {0}; // resolution initialisation
66
  bool gzipped = false;
67
  bool tiffile = false;
68
  
69
  if(name.compare(name.size()-4,4,".inr")==0)
70
    {   
71
      img1.load(name.c_str());
72
      img1.get_load_inr(name.c_str(),tailleVoxel); // reads resolution
73
    }
74
  else if(name.compare(name.size()-7,7,".inr.gz")==0)
75
    {
76
      gzipped = true;
77
      string oldname = name;
78
      name.erase(name.size()-3);
79
      string zip="gunzip -c "+oldname+" > "+name;
80
      if(system(zip.c_str())); // decompress image file
81
      img1.load(name.c_str()); //read image
82
      img1.get_load_inr(name.c_str(),tailleVoxel); // read resolution
83
      zip="rm "+name; 
84
      if(system(zip.c_str())); //removes decompressed image    
85
    }
86
  else if(name.compare(name.size()-4,4,".tif")==0)
87
        {
88
          tiffile = true;
89
          cout<<"file to read : "<<name<<endl;
90
          img1.load_tiff(name.c_str(),0,~0U,1,tailleVoxel,&description);
91
        }
92
  else
93
    {cout<<"!! wrong file extension (not an inr/inr.gz/tif)  : "<<name<<endl;
94
      return 0;}
95
 
96
  CImg<float> img=img1;
97
  img1.assign();
98
  cout<<"image : "<<name<<endl;
99
  cout<<"gzipped : "<<gzipped<<endl;
100
  cout<<"image depth : "<<img.depth()<<endl;
101
  if(img.depth()<=1)
102
    {
103
      cout<<"This is not a 3D image"<<endl;
104
      return  0;
105
    }
106
  //---------------------------------------------------------Parameters
107
  float dt=0.1;
108
  float  K= atof(argv[2]);
109
  float sigma=atof(argv[3]);
110

    
111
  cout<<"Voxel size : ("<<tailleVoxel[0]<<","<<tailleVoxel[1]<<","<<tailleVoxel[2]<<")"<<endl;
112
  
113
  //--------------------------------------------------------Name and directories
114
  string insert=string("_edgeind-K")+argv[2]+"s"+argv[3]+"-"+filter_type;
115
  name.insert(name.size()-4,insert);
116
  size_t test=name.rfind("/");
117
  if(test!=name.npos)
118
    {name.erase(0,test+1);}
119
  
120
  //---------------------------------------------------------------------Processing
121
      cout<<"3D image"<<endl;
122
      cout <<"Computing Hessian"<<endl;
123

    
124
      int nthreads;
125
      CImg<float> fx;
126
      CImg<float> edgeind;
127
      
128
      // make a blur before taking derivatives
129
          CImg<float> imgBlur=img.get_blur(sigma);
130
      fx=edge_lambda3(imgBlur);
131
      
132
    if (filter_type.compare("hill")==0)
133
                {
134
                        edgeind=edge_indicator3(fx,K);
135
                }
136
                else
137
                {
138
                        edgeind=edge_indicator3exp(fx,K);
139
                } 
140
      
141
 
142
        float maxi=edgeind.max();
143
        float mini=edgeind.min();
144
        edgeind=maxi-edgeind;
145
        maxi=edgeind.max();
146
        mini=edgeind.min();
147
        edgeind=edgeind*255./(maxi-mini);
148
    //edgeind=(edgeind-maxi)*255./(mini-maxi);
149

    
150
          //Saving results
151
              string newname=name;
152
              CImg<unsigned char> imgbis=edgeind;
153
              //CImg<float> imgbis=edgeind;
154
              cout<<"Save image"<<endl;
155
              cout<<"image : "<<newname<<endl;
156
              if (tiffile)
157
                        {
158
                                imgbis.save_tiff(newname.c_str(),0,tailleVoxel,description.data());
159
                        }
160
                  else
161
                        {
162
                          imgbis.save_inr(newname.c_str(),tailleVoxel);
163
                          if (gzipped) 
164
                          {
165
                                string zip="gzip -f "+newname;
166
                                if(system(zip.c_str()));
167
                                }
168
                  }
169
              imgbis.assign();
170

    
171
  cout << "Done!"<<endl;
172

    
173

    
174
  return 0;
175
}