Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
ProgVolumeEnhanceContrast Class Reference

#include <volume_enhance_contrast.h>

Inheritance diagram for ProgVolumeEnhanceContrast:
Inheritance graph
[legend]
Collaboration diagram for ProgVolumeEnhanceContrast:
Collaboration graph
[legend]

Public Member Functions

void show ()
 
void enhance (MultidimArray< double > &vol)
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Public Attributes

FileName fnIn
 
FileName fnOut
 
double alpha
 
double lowerIntensity
 
bool removeBg
 
FileName fnMask
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Protected Member Functions

void defineParams ()
 
void readParams ()
 
void run ()
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 

Additional Inherited Members

- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Parameters for enhance contrast program

Definition at line 47 of file volume_enhance_contrast.h.

Member Function Documentation

◆ defineParams()

void ProgVolumeEnhanceContrast::defineParams ( )
protectedvirtual

Params definition

Reimplemented from XmippProgram.

Definition at line 32 of file volume_enhance_contrast.cpp.

33 {
34  addUsageLine("Enhance volume contrast by applying a non-linear transformation to gray levels");
35  addUsageLine("+The contrast enhancement is based on an evolution of an article of");
36  addUsageLine("+[[http://www.ncbi.nlm.nih.gov/pubmed/16579377][Sean Matz]] and is further ");
37  addUsageLine("+explained [[http://biocomp.cnb.csic.es/~coss/Articulos/Fuentes2010.pdf][here]]");
38  addUsageLine("+The algorithm detects the molecule borders and applies a nonlinear transformation ");
39  addUsageLine("+of the gray values");
40  addSeeAlsoLine("volume_correct_bfactor");
41  addParamsLine(" -i <file> : Input volume");
42  addParamsLine(" [-o <file=\"\">] : Output volume");
43  addParamsLine(" [--removeBackground] : Remove the noise of the background");
44  addParamsLine(" [--alpha+ <a=0.01>] : Confidence interval for background identification");
45  addParamsLine(" [--lowerIntensity+ <intensity=0>] : Only process if the gray value is higher than this value");
46  addParamsLine(" [--saveMask+ <filename=\"\">] : Filename for the background mask");
47  addExampleLine("xmipp_volume_enhance_contrast -i volume.vol -o volumeEnhanced.vol");
48 }
void addSeeAlsoLine(const char *seeAlso)
void addExampleLine(const char *example, bool verbatim=true)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ enhance()

void ProgVolumeEnhanceContrast::enhance ( MultidimArray< double > &  vol)

Enhance contrast of a volume.

Definition at line 86 of file volume_enhance_contrast.cpp.

87 {
88  // 1.-Scale volume between 0 and 255----------------------------------
89  double minVal=0., maxVal=0.;
90  vol.computeDoubleMinMax(minVal,maxVal);
91  vol.rangeAdjust(0,255);
92 
93  double vol_avg = vol.computeAvg();
94 
95  // Padd volume with two new rows and cols
96  if( FINISHINGZ(vol) - STARTINGZ(vol) > 0 )
97  {
98  vol.selfWindow( STARTINGZ(vol)-2,STARTINGY(vol)-2,STARTINGX(vol)-2,
99  FINISHINGZ(vol)+2, FINISHINGY(vol)+2,FINISHINGX(vol)+2, vol_avg );
100  }
101  else
102  {
103  vol.selfWindow( STARTINGZ(vol),STARTINGY(vol)-2,STARTINGX(vol)-2,
104  FINISHINGZ(vol), FINISHINGY(vol)+2,FINISHINGX(vol)+2, vol_avg );
105  }
106 
107  Image<double> save;
108 #ifdef DEBUG
109 
110  save()=vol;
111  save.write("PPP1_init.vol");
112 #endif
113 
114  // 2.-Elimination of the background-----------------------------------
116  double bg_mean;
117  detectBackground(vol,mask,0.01,bg_mean);
118 #ifdef DEBUG
119 
120  save()=mask;
121  save.write("PPPmask.vol");
122 #endif
123 
124  // We change 0<->1
126  {
127  if (A3D_ELEM(mask,k,i,j)==1)
128  {
129  A3D_ELEM(mask,k,i,j)=0;
130  }
131  else
132  {
133  A3D_ELEM(mask,k,i,j)=1;
134  }
135  } // Now if 0:background and if 1:mol
136 #ifdef DEBUG
137  save()=mask;
138  save.write("PPPmask_c.vol");
139 #endif
140 
141  if (fnMask!="")
142  {
143  save()=mask;
144  save.write(fnMask);
145  }
146  if (removeBg==true)
147  {
148  // We put all the background with the same value
150  if (DIRECT_A3D_ELEM(mask,k,i,j)==0)
151  DIRECT_A3D_ELEM(vol,k,i,j)=bg_mean;
152  }
153 #ifdef DEBUG
154  save()=vol;
155  save.write("PPP2_no_bg.vol");
156 #endif
157 
158  // 3.-BSplines + diff = edges-------------------------------------------
159  MultidimArray<double> vol_edge;
160  computeEdges(vol,vol_edge);
161 
162 #ifdef DEBUG
163  save()=vol_edge;
164  save.write("PPP3_edge.vol");
165 #endif
166  // 4.-MEAN EDGE GRAY VALUE
167 
168  // 4.1.-Variable Neighbourhood
169 #define COMPUTE_STATISTICS(V,N,k,i,j,avg,stddev,cubeSize) \
170  {\
171  int k0,kF,i0,iF,j0,jF; \
172  k0=XMIPP_MAX(k-N,STARTINGZ(V)); \
173  kF=XMIPP_MIN(k+N,FINISHINGZ(V)); \
174  i0=XMIPP_MAX(i-N,STARTINGY(V)); \
175  iF=XMIPP_MIN(i+N,FINISHINGY(V)); \
176  j0=XMIPP_MAX(j-N,STARTINGX(V)); \
177  jF=XMIPP_MIN(j+N,FINISHINGX(V)); \
178  double sum=0, sum2=0; \
179  for (int kk=k0; kk<=kF; ++kk) \
180  for (int ii=i0; ii<=iF; ++ii) \
181  for (int jj=j0; jj<=jF; ++jj) \
182  { \
183  double v=A3D_ELEM(V,kk,ii,jj); \
184  sum+=v; \
185  sum2+=v*v; \
186  } \
187  cubeSize=(kF-k0+1)*(iF-i0+1)*(jF-j0+1); \
188  avg=sum/cubeSize; \
189  stddev=sum2/cubeSize-avg*avg; \
190  stddev=sqrt(XMIPP_MAX(stddev,0)); \
191  }
192 
193  // We use gaussian because we have a T-Student with more than 100
194  float z=icdf_gauss(1-(0.01/2)); // degrees of freedom
195  // We create a volume with the neighbor sizes
196  MultidimArray<int> vol_tam;
197  vol_tam.resize(vol);
198  vol_tam.initConstant(4);
199 
201  {
202  if (A3D_ELEM(mask,k,i,j)==1)
203  {
204  // only compute if the pixel is not background
205  int cubeDim=1; // 3x3x3
206  int newCubeDim=1; // 3x3x3
207  int max_tam=4; // 9x9x9
208  double avgSmall, stddevSmall, NSmall;
209  COMPUTE_STATISTICS(vol,cubeDim,k,i,j,avgSmall,stddevSmall,NSmall);
210  bool found=false;
211  while (!found)
212  {
213  // Big Cube
214  newCubeDim=newCubeDim+1;
215  double avgBig, stddevBig, NBig;
216  COMPUTE_STATISTICS(vol,newCubeDim,k,i,j,avgBig,stddevBig,NBig);
217  // Computing the degrees of freedom
218  //double num, den;
219  //num=(((stddevSmall*stddevSmall)/NSmall)+((stddevBig*stddevBig)/NBig))*
220  // (((stddevSmall*stddevSmall)/NSmall)+((stddevBig*stddevBig)/NBig));
221  //den=((stddevSmall*stddevSmall)/((NSmall-1)*(NSmall*NSmall)))+
222  // ((stddevBig*stddevBig)/((NBig-1)*(NBig*NBig)));
223  //double df;
224  //df=num/den; // Degrees of freedom
225  // Confidence Intervals --> Comparison of two cubes
226  double K, d, A, B;
227  K=sqrt(((stddevSmall*stddevSmall)/NSmall)+((stddevBig*stddevBig)/NBig));
228  d=avgSmall-avgBig;
229  A=d-(K*z);
230  B=d+(K*z);
231  // If the interval [A,B] contains the cero there are the same
232  if ((A<0) && (0<B))
233  { // equal continue or not
234  if (newCubeDim>=max_tam)
235  { // not continue
236  found=true;
237  A3D_ELEM(vol_tam,k,i,j)=newCubeDim;
238  }
239  else
240  { // continue searching
241  avgSmall=avgBig;
242  stddevSmall=stddevBig;
243  NSmall=NBig;
244  }
245  }
246  else // Not equal -> we stop searching
247  {
248  found=true; // Size found
249  A3D_ELEM(vol_tam,k,i,j)=newCubeDim-1; // it's the latest
250  }
251 
252  } // end of while
253  } // end of if
254  } // end of FOR_ALL_ELEMENTS
255 #ifdef DEBUG
256  save()=vol_tam;
257  save.write("PPP4_vol_tam.vol");
258 #endif
259 
260  // 4.2.- Compute the mean edge gray value
261  MultidimArray<double> VolxEdge;
262  VolxEdge=vol;
263  VolxEdge*=vol_edge;
264  // Macro for compute de Mean Edge Gray Value
265 #define COMPUTE_MEGV(VxE,V_E,N,k,i,j,pixel) \
266  {\
267  int k0,kF,i0,iF,j0,jF; \
268  k0=XMIPP_MAX(k-N,STARTINGZ(V_E)); \
269  kF=XMIPP_MIN(k+N,FINISHINGZ(V_E)); \
270  i0=XMIPP_MAX(i-N,STARTINGY(V_E)); \
271  iF=XMIPP_MIN(i+N,FINISHINGY(V_E)); \
272  j0=XMIPP_MAX(j-N,STARTINGX(V_E)); \
273  jF=XMIPP_MIN(j+N,FINISHINGX(V_E)); \
274  double sum1=0, sum2=0; \
275  for (int kk=k0; kk<=kF; kk++) \
276  for (int ii=i0; ii<=iF; ii++) \
277  for (int jj=j0; jj<=jF; jj++) \
278  { \
279  double v1=A3D_ELEM(VxE,kk,ii,jj); \
280  sum1=sum1+v1; \
281  double v2=A3D_ELEM(V_E,kk,ii,jj); \
282  sum2=sum2+v2; \
283  } \
284  pixel=sum1/sum2; \
285  }
286 
287  MultidimArray<double> Vol_E;
288  Vol_E=vol;
289  int tam;
290  double pixel;
291  // We compute the MEGV for all the volume
293  { // Only compute if no background
294  if (A3D_ELEM(mask,k,i,j)==1)
295  {
296  tam=A3D_ELEM(vol_tam,k,i,j);
297  COMPUTE_MEGV(VolxEdge,vol_edge,tam,k,i,j,pixel);
298  A3D_ELEM(Vol_E,k,i,j)=pixel;
299  }
300  }
301 #ifdef DEBUG
302  save()=Vol_E;
303  save.write("PPP5_vol_megv.vol");
304 #endif
305 
306  //5.-Nonlinear function
307 
308  MultidimArray<double> vol_f=vol;
309  double a, b, x, E;
310  double threshold=lowerIntensity*255;
311 
313  {
314  x=A3D_ELEM(vol,k,i,j);
315  if (A3D_ELEM(mask,k,i,j)==1 && x>threshold)
316  {
317  E=A3D_ELEM(Vol_E,k,i,j);
318  // We search the interval
319  if ((x>=0) && (x<=3))
320  {
321  a=0;
322  b=3;
323  }
324  else if ((x>3) && (x<=8))
325  {
326  a=3;
327  b=8;
328  }
329  else if ((x>8) && (x<=16))
330  {
331  a=8;
332  b=16;
333  }
334  else if ((x>16) && (x<=30))
335  {
336  a=16;
337  b=30;
338  }
339  else if ((x>30) && (x<=49))
340  {
341  a=30;
342  b=49;
343  }
344  else if ((x>49) && (x<=75))
345  {
346  a=49;
347  b=75;
348  }
349  else if ((x>75) && (x<=107))
350  {
351  a=75;
352  b=107;
353  }
354  else if ((x>107) && (x<=147))
355  {
356  a=107;
357  b=147;
358  }
359  else if ((x>147) && (x<=196))
360  {
361  a=147;
362  b=196;
363  }
364  else
365  {
366  a=196;
367  b=255;
368  }
369  // nonlinear function
370  double x_new;
371  if (x<=E)
372  {
373  x_new=E-sqrt(((E-a)*(E-a))-((x-a)*(x-a)));
374  }
375  if (x>E)
376  {
377  x_new=E+sqrt(((b-E)*(b-E))-((x-b)*(x-b)));
378  }
379  A3D_ELEM(vol_f,k,i,j)=x_new;
380  }
381  }
382 #ifdef DEBUG
383  save()=vol_f;
384  save.write("PPP6_vol_f.vol");
385 #endif
386 
387  // Unpadd volume with two new rows and cols
388  if( FINISHINGZ(vol_f) - STARTINGZ(vol_f) > 0 )
389  {
390  vol_f.selfWindow( STARTINGZ(vol_f)+2,STARTINGY(vol_f)+2,STARTINGX(vol_f)+2,
391  FINISHINGZ(vol_f)-2, FINISHINGY(vol_f)-2,FINISHINGX(vol_f)-2 );
392  }
393  else
394  {
395  vol_f.selfWindow( STARTINGZ(vol_f),STARTINGY(vol_f)+2,STARTINGX(vol_f)+2,
396  FINISHINGZ(vol_f), FINISHINGY(vol_f)-2,FINISHINGX(vol_f)-2 );
397  }
398 
399  //6.-Re-Scale (now we put the original values different from [0-255])
400  double x_s,x_ns;
402  {
403  x_s=A3D_ELEM(vol_f,k,i,j);
404  x_ns=((maxVal-minVal)/255)*(x_s+((255*minVal)/(maxVal-minVal)));
405  A3D_ELEM(vol_f,k,i,j)=x_ns;
406  }
407  // OUTPUT
408  vol=vol_f;
409 } // End of enhance
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
#define FINISHINGX(v)
#define COMPUTE_MEGV(VxE, V_E, N, k, i, j, pixel)
void sqrt(Image< double > &op)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
#define COMPUTE_STATISTICS(V, N, k, i, j, avg, stddev, cubeSize)
double icdf_gauss(double p)
void rangeAdjust(T minF, T maxF)
void initConstant(T val)
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V)
#define FINISHINGZ(v)
#define STARTINGX(v)
doublereal * x
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * d
#define STARTINGY(v)
void detectBackground(const MultidimArray< double > &vol, MultidimArray< double > &mask, double alpha, double &final_mean)
Definition: filters.cpp:197
void threshold(double *phi, unsigned long nvox, double limit)
Definition: lib_vwk.cpp:524
#define A3D_ELEM(V, k, i, j)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
doublereal * b
void computeDoubleMinMax(double &minval, double &maxval) const
double z
void computeEdges(const MultidimArray< double > &vol, MultidimArray< double > &vol_edge)
Definition: filters.cpp:3517
#define DIRECT_A3D_ELEM(v, k, i, j)
#define j
#define FINISHINGY(v)
constexpr int K
double computeAvg() const
#define STARTINGZ(v)
doublereal * a

◆ readParams()

void ProgVolumeEnhanceContrast::readParams ( )
protectedvirtual

Read parameters from command line

Reimplemented from XmippProgram.

Definition at line 51 of file volume_enhance_contrast.cpp.

52 {
53  fnIn=getParam("-i");
54  fnOut=getParam("-o");
55  if (fnOut=="")
56  fnOut=fnIn;
57  alpha = getDoubleParam("--alpha");
58  lowerIntensity = getDoubleParam("--lowerIntensity");
59  removeBg = checkParam("--removeBackground");
60  fnMask = getParam("--saveMask");
61 }
double getDoubleParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)
bool checkParam(const char *param)

◆ run()

void ProgVolumeEnhanceContrast::run ( )
protectedvirtual

This function will be start running the program. it also should be implemented by derived classes.

Reimplemented from XmippProgram.

Definition at line 63 of file volume_enhance_contrast.cpp.

64 {
65  Image<double> img;
66  img.read(fnIn);
67  enhance(img());
68  img.write(fnOut);
69 }
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void enhance(MultidimArray< double > &vol)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)

◆ show()

void ProgVolumeEnhanceContrast::show ( )

Show parameters. This function calls show_specific.

Definition at line 72 of file volume_enhance_contrast.cpp.

73 {
74  if (verbose==0)
75  return;
76  std::cout
77  << "Input: " << fnIn << std::endl
78  << "Output: " << fnOut << std::endl
79  << "Alpha: " << alpha << std::endl
80  << "lowerIntensity: " << lowerIntensity << std::endl
81  << "removeBackground: " << removeBg << std::endl
82  << "saveMask: " << fnMask << std::endl;
83 }
int verbose
Verbosity level.

Member Data Documentation

◆ alpha

double ProgVolumeEnhanceContrast::alpha

Definition at line 54 of file volume_enhance_contrast.h.

◆ fnIn

FileName ProgVolumeEnhanceContrast::fnIn

Definition at line 51 of file volume_enhance_contrast.h.

◆ fnMask

FileName ProgVolumeEnhanceContrast::fnMask

Definition at line 63 of file volume_enhance_contrast.h.

◆ fnOut

FileName ProgVolumeEnhanceContrast::fnOut

Definition at line 51 of file volume_enhance_contrast.h.

◆ lowerIntensity

double ProgVolumeEnhanceContrast::lowerIntensity

Definition at line 57 of file volume_enhance_contrast.h.

◆ removeBg

bool ProgVolumeEnhanceContrast::removeBg

Definition at line 60 of file volume_enhance_contrast.h.


The documentation for this class was generated from the following files: