Xmipp  v3.23.11-Nereus
image_histogram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es (2000)
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 <core/xmipp_image.h>
27 #include <core/metadata_vec.h>
28 #include <data/mask.h>
29 #include <core/histogram.h>
30 
32 {
33 public:
40  double m, M; // range for histogram
41  int StepsNo;
43 
44  void defineParams()
45  {
46  addUsageLine("Calculate histogram from a list of images/volumes");
47  addParamsLine(" -i <input_file> : input metadata, image, volume or stack");
48  addParamsLine(" [-o <text_file>] : output text file with histogram");
49  addParamsLine(" [--range <m> <M>] : range for the histogram, automatic calculated if not provided");
50  addParamsLine(" [--steps <N=100>] : number of subdivisions");
51  addParamsLine(" [--norm] : normalize histogram ");
52  mask.defineParams(this, INT_MASK, nullptr, "Histogram constrained to the mask area.");
53 
54  addExampleLine("Create the histogram from an stack setting number of steps to 20", false);
55  addExampleLine("xmipp_histogram -i input/images_some.stk -o hist.txt --steps 20");
56  addExampleLine("You can use the =hist.txt= file with =gnuplot= to produce graphical histogram", false);
57  addExampleLine("After you enter =gnuplot= terminal you can use:", false);
58  addExampleLine("gnuplot> plot \"kk.txt\" using 1:2 title \"Histogram\" with steps");
59 
60  addSeeAlsoLine("metadata_histogram");
61  }
62 
63  void readParams()
64  {
65  fn_in = getParam("-i");
66  mdIn.read(fn_in);
67 
68  if (mdIn.isEmpty())
69  REPORT_ERROR(ERR_PARAM_INCORRECT, "Can't calculate histogram from empty metadata");
70 
71  if (checkParam("-o"))
72  fn_out = getParam("-o");
73 
74  StepsNo = getIntParam("--steps");
75  do_normalize = checkParam("--norm");
76  automatic_range = true;
77 
78  if (checkParam("--range"))
79  {
80  m = getDoubleParam("--range", 0);
81  M = getDoubleParam("--range", 1);
82  automatic_range = false;
83  }
84 
86  if ((apply_mask = checkParam("--mask")))
87  mask.readParams(this);
88 
89  //todo: check this
90  // if (fn_sel!="" && automatic_range)
91  // REPORT_ERROR(ERR_ARG_INCORRECT,"Only use selfile in combination with fixed range!");
92  //
93  // mask_prm.read(argc, argv);
94 
95  }
96 
97  void run()
98  {
99  auto iterId = mdIn.ids().begin();
100  image.readApplyGeo(mdIn, *iterId);
101  image().setXmippOrigin();
102  double dummy;
103 
104  // Generate mask if necessary
105  if (apply_mask)
106  {
107  mask.generate_mask(image());
108  maskArray = mask.get_binary_mask();
109  if (automatic_range)
110  computeStats_within_binary_mask(maskArray, image(), m, M, dummy, dummy);
111  }
112  else if (automatic_range)
113  image().computeDoubleMinMax(m, M);
114 
115  if (automatic_range)
116  {
117  m *= 1.5;
118  M *= 1.5;
119  }
120 
121  if (apply_mask)
122  compute_hist_within_binary_mask(maskArray, image(), histb, m, M, StepsNo);
123  else
124  compute_hist(image(), histb, m, M, StepsNo);
125 
126  const auto totalSize = mdIn.ids().end();
127  while (++iterId != totalSize)
128  {
129  image.readApplyGeo(mdIn, *iterId);
130  image().setXmippOrigin();
131  // Compute histogram ----------------------------------------------------
132  if (apply_mask)
133  compute_hist_within_binary_mask(maskArray, image(), hist, m, M, StepsNo);
134  else
135  compute_hist(image(), hist, m, M, StepsNo);
136  histb += hist;
137  }
138 
139  if (do_normalize)
140  histb *= 1.0 / (histb.sum() * histb.step_size);
141 
142  if (!fn_out.empty())
143  histb.write(fn_out);
144  else
145  std::cout << histb;
146  }
147 
148 }
149 ;//end of class ProgHistogram
150 
151 
152 void Usage();
Parameter incorrect.
Definition: xmipp_error.h:181
MetaDataVec mdIn
double getDoubleParam(const char *param, int arg=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Histogram1D hist
Definition: mask.h:360
void Usage()
MultidimArray< int > maskArray
int allowed_data_types
Definition: mask.h:495
virtual IdIteratorProxy< false > ids()
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams &params=DefaultApplyGeoParams)
void computeStats_within_binary_mask(const MultidimArray< T1 > &mask, const MultidimArray< T > &m, double &min_val, double &max_val, double &avg, double &stddev)
Definition: mask.h:799
void addSeeAlsoLine(const char *seeAlso)
static void defineParams(XmippProgram *program, int allowed_data_types=ALL_KINDS, const char *prefix=nullptr, const char *comment=nullptr, bool moreOptions=false)
Definition: mask.cpp:1203
Image< double > image
bool isEmpty() const override
const char * getParam(const char *param, int arg=0)
void compute_hist(const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
Definition: histogram.cpp:572
Histogram1D histb
void addExampleLine(const char *example, bool verbatim=true)
void readParams(XmippProgram *program)
Definition: mask.cpp:1284
double dummy
void generate_mask(bool apply_geo=false)
Definition: mask.cpp:1577
void write(const FileName &fn, MDLabel=MDL_X, MDLabel=MDL_COUNT)
Definition: histogram.cpp:129
#define INT_MASK
Definition: mask.h:385
bool checkParam(const char *param)
double step_size
Definition: histogram.h:127
void addUsageLine(const char *line, bool verbatim=false)
const MultidimArray< int > & get_binary_mask() const
Definition: mask.h:707
int getIntParam(const char *param, int arg=0)
void compute_hist_within_binary_mask(const MultidimArray< int > &mask, MultidimArray< T > &v, Histogram1D &hist, int no_steps)
Definition: mask.h:906
double sum() const
void addParamsLine(const String &line)