Xmipp  v3.23.11-Nereus
tomo_remove_fluctuations.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es (2009)
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 
28 #include "core/xmipp_fftw.h"
29 
30 // Read from command line --------------------------------------------------
32 {
33  fnIn = getParam("-i");
34  fnOut = getParam("-o");
35  maxFreq = getDoubleParam("--lpf");
36 }
37 
38 // Usage -------------------------------------------------------------------
40 {
41  addUsageLine("Remove the flickering in a tilt series. This is a phenomenon rather ");
42  addUsageLine("common in X-ray microscopy. For doing so, a low-pass filter of a given ");
43  addUsageLine("frequency (normalized to 0.5) is applied across the time series, i.e., a ");
44  addUsageLine("line is formed with all the lines at the same position along the tilt series. ");
45  addUsageLine("This time series is the filtered and put back into the tilt series. ");
46  addParamsLine(" -i <file> : Input images (selfile or stack)");
47  addParamsLine(" -o <outputStack> : Output stack");
48  addParamsLine(" [--lpf <f=0.25>] : Low pass filter");
49  addParamsLine(" : Frequency is normalized to 0.5");
50 }
51 
52 // Produce side info -------------------------------------------------------
54 {
55  // Read the selfile into a volume
56  SF.read(fnIn);
57  size_t Zdim, dummy, Ydim, Xdim, Ndim;
58  Zdim=SF.size();
59  getImageSize(SF,Xdim,Ydim,dummy,Ndim);
60  V().setMmap(true);
61  V().initZeros(Zdim,Ydim,Xdim);
62  int k=0;
63  Image<double> I;
64  FileName fnImg;
65  for (size_t objId : SF.ids())
66  {
67  SF.getValue(MDL_IMAGE,fnImg,objId);
68  I.read(fnImg);
69  V().setSlice(k,I());
70  k++;
71  }
72 }
73 
74 // Show --------------------------------------------------------------------
76 {
77  if (verbose==0)
78  return;
79  std::cout << "Removing fluctuations from a tilt series\n";
80  std::cout << "Input series: " << fnIn << std::endl
81  << "Output root: " << fnOut << std::endl
82  << "Max freq (LPF): " << maxFreq << std::endl
83  ;
84 }
85 
86 // Denoise image -----------------------------------------------------------
88 {
90 
91  // Filter each line in the series
93  int maxPixel=CEIL(ZSIZE(mV)*maxFreq);
94  FourierTransformer transformer;
95  if (verbose>=1)
99  std::complex<double> zero=0;
100  line.initZeros(ZSIZE(mV));
101  for (size_t i=0; i<YSIZE(mV); i++)
102  {
103  for (size_t j=0; j<XSIZE(mV); j++)
104  {
105  // Get line
106  for (size_t k=0; k<ZSIZE(mV); k++)
107  DIRECT_A1D_ELEM(line,k)=DIRECT_A3D_ELEM(mV,k,i,j);
108 
109  // Fourier transform
110  transformer.setReal(line);
111  transformer.FourierTransform();
112 
113  // Filter
114  transformer.getFourierAlias(lineFourier);
115  for (size_t k=maxPixel; k<XSIZE(lineFourier); k++)
116  DIRECT_A1D_ELEM(lineFourier,k)=zero;
117 
118  // Inverse Fourier transform and back to the volume
119  transformer.inverseFourierTransform();
120  for (size_t k=0; k<ZSIZE(mV); k++)
121  DIRECT_A3D_ELEM(mV,k,i,j)=DIRECT_A1D_ELEM(line,k);
122  }
123  if (verbose>=1)
124  progress_bar(i+1);
125  }
126  if (verbose>=1)
127  progress_bar(YSIZE(mV));
128 
129  // Write the results
130  mV.setDimensions(XSIZE(mV),YSIZE(mV),1,ZSIZE(mV));
131  V.write(fnOut,ALL_IMAGES,true);
132 }
void init_progress_bar(long total)
#define YSIZE(v)
double getDoubleParam(const char *param, int arg=0)
double maxFreq
Cutoff frequency of the lowpass filter (<0.5)
void inverseFourierTransform()
Definition: xmipp_fftw.cpp:329
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
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 getImageSize(const MetaData &md, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, MDLabel image_label)
void readParams()
Read parameters from command line.
virtual IdIteratorProxy< false > ids()
FileName fnOut
Rootname for the output.
size_t size() const override
#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
#define DIRECT_A1D_ELEM(v, i)
const char * getParam(const char *param, int arg=0)
void show() const
Show parameters.
void setReal(MultidimArray< double > &img)
Definition: xmipp_fftw.cpp:129
#define CEIL(x)
Definition: xmipp_macros.h:225
void produceSideInfo()
Produce side info.
#define XSIZE(v)
void progress_bar(long rlen)
#define ZSIZE(v)
int verbose
Verbosity level.
double dummy
#define DIRECT_A3D_ELEM(v, k, i, j)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)
#define j
bool getValue(MDObject &mdValueOut, size_t id) const override
void getFourierAlias(T &V)
Definition: xmipp_fftw.h:207
#define ALL_IMAGES
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
Name of an image (std::string)
void addParamsLine(const String &line)
ql0001_ & zero(ctemp+1),(cvec+1),(a+1),(b+1),(bl+1),(bu+1),(x+1),(w+1), &iout, ifail, &zero,(w+3), &lwar2,(iw+1), &leniw, &glob_grd.epsmac