Xmipp  v3.23.11-Nereus
Protected Member Functions | Protected Attributes | List of all members
ProgAddNoise Class Reference
Inheritance diagram for ProgAddNoise:
Inheritance graph
[legend]
Collaboration diagram for ProgAddNoise:
Collaboration graph
[legend]

Protected Member Functions

void defineParams ()
 
void readParams ()
 
void show ()
 
template<typename T >
void limit (Image< T > &img)
 
void processImage (const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
 
- Protected Member Functions inherited from XmippMetadataProgram
virtual void initComments ()
 
virtual void preProcess ()
 
virtual void postProcess ()
 
virtual bool getImageToProcess (size_t &objId, size_t &objIndex)
 
void show () const override
 
virtual void startProcessing ()
 
virtual void finishProcessing ()
 
virtual void writeOutput ()
 
virtual void showProgress ()
 
virtual void defineLabelParam ()
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 

Protected Attributes

double param1
 
double param2
 
double df
 
double limit0
 
double limitF
 
bool do_limit0
 
bool do_limitF
 
std::string noise_type
 
- Protected Attributes inherited from XmippMetadataProgram
WriteModeMetaData mode
 Metadata writing mode: OVERWRITE, APPEND. More...
 
FileName oext
 Output extension and root. More...
 
FileName oroot
 
MDLabel image_label
 MDLabel to be used to read/write images, usually will be MDL_IMAGE. More...
 
bool produces_an_output
 Indicate that a unique final output is produced. More...
 
bool produces_a_metadata
 Indicate that the unique final output file is a Metadata. More...
 
bool each_image_produces_an_output
 Indicate that an output is produced for each image in the input. More...
 
bool allow_apply_geo
 
bool decompose_stacks
 Input Metadata will treat a stack file as a set of images instead of a unique file. More...
 
bool delete_output_stack
 Delete previous output stack file prior to process images. More...
 
bool get_image_info
 Get the input image file dimensions to further operations. More...
 
bool save_metadata_stack
 Save the associated output metadata when output file is a stack. More...
 
bool track_origin
 Include the original input image filename in the output stack. More...
 
bool keep_input_columns
 Keep input metadata columns. More...
 
bool remove_disabled
 Remove disabled images from the input selfile. More...
 
bool allow_time_bar
 Show process time bar. More...
 
bool input_is_metadata
 Input is a metadata. More...
 
bool single_image
 Input is a single image. More...
 
bool input_is_stack
 Input is a stack. More...
 
bool output_is_stack
 Output is a stack. More...
 
bool create_empty_stackfile
 
bool delete_mdIn
 
size_t time_bar_step
 Some time bar related counters. More...
 
size_t time_bar_size
 
size_t time_bar_done
 
- 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
 

Additional Inherited Members

- Public Member Functions inherited from XmippMetadataProgram
MetaDatagetInputMd ()
 
MetaDataVecgetOutputMd ()
 
 XmippMetadataProgram ()
 Empty constructor. More...
 
virtual int tryRead (int argc, const char **argv, bool reportErrors=true)
 
virtual void init ()
 
virtual void setup (MetaData *md, const FileName &o="", const FileName &oroot="", bool applyGeo=false, MDLabel label=MDL_IMAGE)
 
virtual ~XmippMetadataProgram ()
 
void setMode (WriteModeMetaData _mode)
 
void setupRowOut (const FileName &fnImgIn, const MDRow &rowIn, const FileName &fnImgOut, MDRow &rowOut) const
 Prepare rowout. More...
 
virtual void wait ()
 Wait for the distributor to finish. More...
 
virtual void checkPoint ()
 For very long programs, it may be needed to write checkpoints. More...
 
virtual void run ()
 Run over all images. More...
 
- 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)
 
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 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 inherited from XmippMetadataProgram
FileName fn_in
 Filenames of input and output Metadata. More...
 
FileName fn_out
 
FileName baseName
 
FileName pathBaseName
 
FileName oextBaseName
 
bool apply_geo
 Apply geo. More...
 
size_t ndimOut
 Output dimensions. More...
 
size_t zdimOut
 
size_t ydimOut
 
size_t xdimOut
 
DataType datatypeOut
 
size_t mdInSize
 Number of input elements. More...
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Detailed Description

Definition at line 31 of file transform_add_noise.cpp.

Member Function Documentation

◆ defineParams()

void ProgAddNoise::defineParams ( )
inlineprotectedvirtual

Function in which the param of each Program are defined.

Reimplemented from XmippMetadataProgram.

Definition at line 42 of file transform_add_noise.cpp.

43  {
46  //Usage
47  addUsageLine("Add random noise to the input images.");
48  addUsageLine("Noise can be generated using uniform, gaussian or t-student distributions.");
49  //Parameters
50  addParamsLine("--type <rand_mode> : Type of noise to add");
51  addParamsLine(" where <rand_mode>");
52  addParamsLine(" gaussian <stddev> <avg=0.> :Gaussian distribution parameters");
53  addParamsLine(" student <df> <stddev> <avg=0.> :t-student distribution parameters");
54  addParamsLine(" uniform <min> <max> :Uniform distribution parameters");
55  addParamsLine(" " + TYPE_POISSON + " <min> <max> :Poission distribution. Each pixel i of output is generated as poisson(ref-input[i])");
56  addParamsLine(" [--limit0 <float> ] :Crop noise histogram below this value ");
57  addParamsLine(" [--limitF <float> ] :Crop noise histogram above this value ");
58  //Examples
59  addExampleLine("Add noise to a single image, writing in different image:", false);
60  addExampleLine("xmipp_transform_add_noise -i cleanImage.spi --type gaussian 10 5 -o noisyGaussian.spi");
61  addExampleLine("+++Following =cleanImage.spi= at left and =noisyGaussian.spi= at right: %BR%", false);
62  addExampleLine("+++%ATTACHURL%/cleanImage.jpg %ATTACHURL%/noisyGaussian.jpg %BR%", false);
63  addExampleLine("Add uniform noise to a volume, overriding input volume:", false);
64  addExampleLine("xmipp_transform_add_noise -i g0ta.vol -uniform -0.1 0.1");
65 
66  }
void addExampleLine(const char *example, bool verbatim=true)
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)
void addParamsLine(const String &line)

◆ limit()

template<typename T >
void ProgAddNoise::limit ( Image< T > &  img)
inlineprotected

Definition at line 130 of file transform_add_noise.cpp.

130  {
131  if (do_limit0) {
132  const size_t count = img.data.nzyxdim;
133  for (size_t i = 0; i < count; ++i) {
134  img.data[i] = XMIPP_MAX(img.data[i], limit0);
135  }
136  }
137  if (do_limitF) {
138  const size_t count = img.data.nzyxdim;
139  for (size_t i = 0; i < count; ++i) {
140  img.data[i] = XMIPP_MIN(img.data[i], limitF);
141  }
142  }
143  }
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define i
MultidimArray< T > data
Definition: xmipp_image.h:55
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181

◆ processImage()

void ProgAddNoise::processImage ( const FileName fnImg,
const FileName fnImgOut,
const MDRow rowIn,
MDRow rowOut 
)
inlineprotectedvirtual

Implements XmippMetadataProgram.

Definition at line 145 of file transform_add_noise.cpp.

146  {
147 
148  if (TYPE_POISSON == noise_type) {
149  Image<float> img;
150  img.read(fnImg);
151  std::random_device rd;
152  std::mt19937 gen(rd());
153  Image<int> res(img.data.xdim, img.data.ydim, img.data.zdim, img.data.ndim);
154  const size_t count = res.data.nzyxdim;
155  const float gap = param1 - param2;
156  auto dist = std::poisson_distribution<>(0);
157  for (size_t i = 0; i < count; ++i) {
158  float mean = param1 - gap * img.data[i];
159  if (dist.mean() != mean) { // reuse distribution, if possible
160  dist = std::poisson_distribution<>(mean);
161  }
162  res.data[i] = dist(gen);
163  }
164  limit(res);
165  res.write(fnImgOut);
166  } else {
167  Image<double> img;
168  img.read(fnImg);
169  img().addNoise(param1, param2, noise_type, df);
170  limit(img);
171  img.write(fnImgOut);
172  }
173 
174  }
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 i
MultidimArray< T > data
Definition: xmipp_image.h:55
void limit(Image< T > &img)
std::string noise_type
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)

◆ readParams()

void ProgAddNoise::readParams ( )
inlineprotectedvirtual

Function in which each program will read parameters that it need. If some error occurs the usage will be printed out.

Default value of df in addNoise function

Reimplemented from XmippMetadataProgram.

Definition at line 68 of file transform_add_noise.cpp.

69  {
71  do_limit0 = checkParam("--limit0");
72  if (do_limit0)
73  limit0 = getDoubleParam("-limit0");
74  do_limitF = checkParam("--limitF");
75  if (do_limitF)
76  limitF = getDoubleParam("--limitF");
77 
79  df = 3.;
80  noise_type = getParam("--type");
81 
82  if (noise_type == "gaussian")
83  {
84  param2 = getDoubleParam("--type", 1);
85  param1 = getDoubleParam("--type", 2);
86  }
87  else if (noise_type == "student")
88  {
89  df = getDoubleParam("--type", 1);
90  param1 = getDoubleParam("--type", 2);
91  param2 = getDoubleParam("--type", 3);
92  }
93  else if (noise_type == "uniform")
94  {
95  param1 = getDoubleParam("--type", 1);
96  param2 = getDoubleParam("--type", 2);
97  }
98  else if (TYPE_POISSON == noise_type)
99  {
100  param1 = getDoubleParam("--type", 1);
101  param2 = getDoubleParam("--type", 2);
102  }
103  else
104  REPORT_ERROR(ERR_ARG_INCORRECT, "Unknown noise type");
105  }
double getDoubleParam(const char *param, int arg=0)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
const char * getParam(const char *param, int arg=0)
Incorrect argument received.
Definition: xmipp_error.h:113
std::string noise_type
bool checkParam(const char *param)

◆ show()

void ProgAddNoise::show ( )
inlineprotected

Definition at line 107 of file transform_add_noise.cpp.

108  {
110  if (noise_type == "gaussian")
111  std::cout << "Noise avg=" << param1 << std::endl
112  << "Noise stddev=" << param2 << std::endl;
113  else if (noise_type == "student")
114  std::cout << "Degrees of freedom= "<< df << std::endl
115  << "Noise avg=" << param1 << std::endl
116  << "Noise stddev=" << param2 << std::endl;
117  else if (noise_type == "uniform")
118  std::cout << "Noise min=" << param1 << std::endl
119  << "Noise max=" << param2 << std::endl;
120  else if (TYPE_POISSON == noise_type)
121  std::cout << "Mean background=" << param1 << std::endl
122  << "Mean foreground=" << param2 << std::endl;
123  if (do_limit0)
124  std::cout << "Crop noise histogram below=" << limit0 << std::endl;
125  if (do_limitF)
126  std::cout << "Crop noise histogram above=" << limitF << std::endl;
127  }
std::string noise_type
void show() const override

Member Data Documentation

◆ df

double ProgAddNoise::df
protected

Definition at line 38 of file transform_add_noise.cpp.

◆ do_limit0

bool ProgAddNoise::do_limit0
protected

Definition at line 39 of file transform_add_noise.cpp.

◆ do_limitF

bool ProgAddNoise::do_limitF
protected

Definition at line 39 of file transform_add_noise.cpp.

◆ limit0

double ProgAddNoise::limit0
protected

Definition at line 38 of file transform_add_noise.cpp.

◆ limitF

double ProgAddNoise::limitF
protected

Definition at line 38 of file transform_add_noise.cpp.

◆ noise_type

std::string ProgAddNoise::noise_type
protected

Definition at line 40 of file transform_add_noise.cpp.

◆ param1

double ProgAddNoise::param1
protected

Definition at line 37 of file transform_add_noise.cpp.

◆ param2

double ProgAddNoise::param2
protected

Definition at line 37 of file transform_add_noise.cpp.


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