Xmipp  v3.23.11-Nereus
threshold.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "threshold.h"
27 
28 /* Read parameters --------------------------------------------------------- */
30 {
32  selectionMethod=getParam("--select");
33  if (selectionMethod=="abs_below")
35  else if (selectionMethod=="below")
37  else if (selectionMethod=="above")
39  threshold=getDoubleParam("--select",1);
40  substitutionMethod=getParam("--substitute");
41  if (substitutionMethod=="value")
42  newValue=getDoubleParam("--substitute",1);
43  else if (substitutionMethod=="noise")
44  {
45  noiseAvg=getDoubleParam("--substitute",1);
46  noiseStddev=getDoubleParam("--substitute",2);
47  }
48 }
49 
50 /* Usage ------------------------------------------------------------------- */
52 {
53  addUsageLine("Threshold volumes and images ");
56  addSeeAlsoLine("transform_mask, transform_morphology");
57  addParamsLine(" --select <mode> : Select pixels meeting");
58  addParamsLine(" where <mode>");
59  addParamsLine(" abs_below <th> : Absolute value below a threshold");
60  addParamsLine(" below <th> : Below a threshold");
61  addParamsLine(" above <th> : Above a threshold");
62  addParamsLine(" --substitute <substitutionMode=value> : Substitute selected pixels by");
63  addParamsLine(" where <substitutionMode>");
64  addParamsLine(" binarize : Selected are set to 0, non-selected to 1");
65  addParamsLine(" value <new=0> : New value");
66  addParamsLine(" noise <avg=0> <stddev=1> : Gaussian noise");
67  addParamsLine(" avg : Average of non-selected");
68  addExampleLine("Threshold a volume below a threshold",false);
69  addExampleLine("xmipp_transform_threshold -i volume.vol -o volumeThresholded.vol --select below 0.01 --substitute value 0");
70  addExampleLine("Generate a binary mask based on a threshold and apply it",false);
71  addExampleLine("xmipp_transform_threshold -i volume.vol -o mask.vol --select below 0.5 --substitute binarize");
72  addExampleLine("xmipp_transform_morphology -i mask.vol --dil");
73  addExampleLine("xmipp_transform_mask -i volume.vol -o volumeMasked.vol --mask mask.vol");
74 }
75 
76 /* Show ------------------------------------------------------------------- */
78 {
79  if (verbose==0)
80  return;
82  std::cout
83  << "Selection method: " << selectionMethod << std::endl
84  << "Threshold: " << threshold << std::endl
85  << "Substitution method: " << substitutionMethod << std::endl;
86  if (substitutionMethod=="value")
87  std::cout << "New value: " << newValue << std::endl;
88  else if (substitutionMethod=="noise")
89  {
90  std::cout << "Noise average: " << noiseAvg << std::endl;
91  std::cout << "Noise std.dev.: " << noiseStddev << std::endl;
92  }
93 }
94 
95 /* Process image ------------------------------------------------------------- */
96 void ProgThreshold::processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
97 {
98  Image<double> I;
99  I.read(fnImg);
100  MultidimArray<double> &mI=I();
101 
102  // Compute substitute value
103  double substituteValue=0.0;
104  if (substitutionMethod=="value")
105  substituteValue=newValue;
106  else if (substitutionMethod=="noise")
107  substituteValue=rnd_gaus(noiseAvg,noiseStddev);
108  else if (substitutionMethod=="avg")
109  {
110  double N=0;
111  switch (iSelectionMethod)
112  {
113  case 0:
115  {
116  double pixval=DIRECT_MULTIDIM_ELEM(mI,n);
117  if (fabs(pixval)>threshold)
118  {
119  substituteValue+=pixval;
120  ++N;
121  }
122  }
123  break;
124  case 1:
126  {
127  double pixval=DIRECT_MULTIDIM_ELEM(mI,n);
128  if (pixval>threshold)
129  {
130  substituteValue+=pixval;
131  ++N;
132  }
133  }
134  break;
135  case 2:
137  {
138  double pixval=DIRECT_MULTIDIM_ELEM(mI,n);
139  if (pixval<threshold)
140  {
141  substituteValue+=pixval;
142  ++N;
143  }
144  }
145  break;
146  }
147  substituteValue/=N;
148  }
149 
150  // Apply threshold
151  bool binarize=substitutionMethod=="binarize";
152  switch (iSelectionMethod)
153  {
154  case 0:
156  {
157  if (fabs(DIRECT_MULTIDIM_ELEM(mI,n))<threshold)
158  DIRECT_MULTIDIM_ELEM(mI,n)=substituteValue;
159  else if (binarize)
160  DIRECT_MULTIDIM_ELEM(mI,n)=1;
161  }
162  break;
163  case 1:
165  {
167  DIRECT_MULTIDIM_ELEM(mI,n)=substituteValue;
168  else if (binarize)
169  DIRECT_MULTIDIM_ELEM(mI,n)=1;
170  }
171  break;
172  case 2:
174  {
176  DIRECT_MULTIDIM_ELEM(mI,n)=substituteValue;
177  else if (binarize)
178  DIRECT_MULTIDIM_ELEM(mI,n)=1;
179  }
180  break;
181  }
182 
183  // Write result
184  I.write(fnImgOut);
185 }
double getDoubleParam(const char *param, int arg=0)
double newValue
Definition: threshold.h:51
void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
Process image or volume.
Definition: threshold.cpp:96
double threshold
Definition: threshold.h:45
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)
String substitutionMethod
Definition: threshold.h:48
String selectionMethod
Definition: threshold.h:42
void readParams()
Definition: threshold.cpp:29
void addSeeAlsoLine(const char *seeAlso)
int iSelectionMethod
Definition: threshold.h:72
const char * getParam(const char *param, int arg=0)
void defineParams()
Definition: threshold.cpp:51
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
void addExampleLine(const char *example, bool verbatim=true)
int verbose
Verbosity level.
void show() const override
double noiseAvg
Definition: threshold.h:54
double rnd_gaus()
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
bool each_image_produces_an_output
Indicate that an output is produced for each image in the input.
void addUsageLine(const char *line, bool verbatim=false)
int * n
void addParamsLine(const String &line)
double noiseStddev
Definition: threshold.h:57