Xmipp  v3.23.11-Nereus
image_eliminate_empty_particles.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Author: Tomas Majtner (tmajtner@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 
28 #include "core/metadata_vec.h"
29 #include <data/filters.h>
30 #include <fstream>
31 
32 // Read arguments ==========================================================
34 {
35  fnIn = getParam("-i");
36  fnOut = getParam("-o");
37  fnElim = getParam("-e");
38  threshold = getDoubleParam("-t");
39  addFeatures = checkParam("--addFeatures");
40  useDenoising = checkParam("--useDenoising");
41  denoise = getIntParam("-d");
42 }
43 
44 // Show ====================================================================
46 {
47  if (verbose==0)
48  return;
49  std::cerr
50  << "Input selfile: " << fnIn << std::endl
51  << "Output selfile: " << fnOut << std::endl
52  << "Eliminated selfile: " << fnElim << std::endl
53  << "Threshold: " << threshold << std::endl
54  << "Add features: " << addFeatures << std::endl
55  << "Turn on denoising: " << useDenoising << std::endl
56  << "Denosing parameter: " << denoise << std::endl
57  ;
58 }
59 
60 // Usage ===================================================================
62 {
63  addUsageLine("Eliminates empty particles (false positives from picking)");
64  addParamsLine(" -i <selfile> : Selfile containing set of input particles");
65  addParamsLine(" [-o <selfile=\"output.xmd\">] : Output selfile");
66  addParamsLine(" [-e <selfile=\"eliminated.xmd\">] : Eliminated particles selfile");
67  addParamsLine(" [-t <float=-1>] : Threshold used by algorithm. Set to -1 for no elimination.");
68  addParamsLine(" [--addFeatures] : Add the emptiness features to the input particles");
69  addParamsLine(" [--useDenoising] : Option for turning on denoising method while computing emptiness feature");
70  addParamsLine(" [-d <int=50>] : Parameter for denoising, higher value means stronger denoising and slower computation");
71 }
72 
74 {
75  MetaDataVec SF;
76  SF.read(fnIn);
77  FileName fnImg;
78  Image<double> I;
79  CorrelationAux aux;
80  MDRowVec row;
81  MetaDataVec MDclass, MDclassEl, MDclassT, MDclassElT;
83  int countItems = 0;
84  std::vector<double> fv;
85 
86  init_progress_bar(SF.size());
87  std::size_t extraPath = fnOut.find_last_of("/");
88  // these files are for streaming and will be later removed in Scipion
89  FileName fnDone = fnOut.substr(0, extraPath+1) + "outTemp.xmd";
90  FileName fnElDone = fnOut.substr(0, extraPath+1) + "elimTemp.xmd";
91 
92  std::ifstream ifile1(fnOut.c_str());
93  if (ifile1) MDclass.read(fnOut);
94  std::ifstream ifile2(fnElim.c_str());
95  if (ifile2) MDclassEl.read(fnElim);
96 
97  for (size_t objId : SF.ids())
98  {
99  countItems++;
100  SF.getValue(MDL_IMAGE, fnImg, objId);
101  SF.getRow(row, countItems);
102 
103  I.read(fnImg);
104  I().setXmippOrigin();
105  centerImageTranslationally(I(), aux);
106 
107  if (useDenoising)
109  bandpassFilter(I(), 0, 0.1, 0.02);
110 
111  ef.extractVariance(I(), fv);
112 
113  double ratio = fv.back();
114  row.setValue(MDL_SCORE_BY_EMPTINESS, ratio);
115  if (addFeatures)
117  if (threshold<0 || ratio > threshold)
118  {
119  MDclass.addRow(row);
120  MDclassT.addRow(row);
121  }
122  else
123  {
124  MDclassEl.addRow(row);
125  MDclassElT.addRow(row);
126  }
127 
128  fv.clear();
129  if (countItems%100==0)
130  progress_bar(countItems);
131  }
132  progress_bar(SF.size());
133 
134  if (MDclass.size()>0)
135  MDclass.write(formatString("@%s", fnOut.c_str()), MD_APPEND);
136  if (MDclassEl.size()>0)
137  MDclassEl.write(formatString("@%s", fnElim.c_str()), MD_APPEND);
138  if (MDclassT.size()>0)
139  MDclassT.write(formatString("@%s", fnDone.c_str()), MD_APPEND);
140  if (MDclassElT.size()>0)
141  MDclassElT.write(formatString("@%s", fnElDone.c_str()), MD_APPEND);
142 }
void init_progress_bar(long total)
void centerImageTranslationally(MultidimArray< double > &I, CorrelationAux &aux)
Definition: filters.cpp:3212
double getDoubleParam(const char *param, int arg=0)
void defineParams()
Define input parameters.
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void setValue(const MDObject &object) override
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
FileName fnElim
Name of the eliminated particle metadata.
virtual IdIteratorProxy< false > ids()
std::unique_ptr< MDRow > getRow(size_t id) override
size_t size() const override
size_t addRow(const MDRow &row) override
const char * getParam(const char *param, int arg=0)
void progress_bar(long rlen)
int verbose
Verbosity level.
FileName fnIn
Name of the input metadata.
void bandpassFilter(MultidimArray< double > &img, double w1, double w2, double raised_w)
bool getValue(MDObject &mdValueOut, size_t id) const override
void readParams()
Read input parameters.
void realGaussianFilter(MultidimArray< double > &img, double sigma)
Small values represent worse particles. Much larger than 1 for good particles.
String formatString(const char *format,...)
bool checkParam(const char *param)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
float threshold
Threshold for variance of variance.
void extractVariance(const MultidimArray< double > &I, std::vector< double > &fv)
Extracting variance features.
void addUsageLine(const char *line, bool verbatim=false)
FileName fnOut
Name of the output metadata.
int getIntParam(const char *param, int arg=0)
Name of an image (std::string)
void addParamsLine(const String &line)
Feature vectors used to classify particles (vector double)