Xmipp  v3.23.11-Nereus
image_header.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Joaquin Oton (joton@cnb.csic.es)
4  * J.M.de la Rosa Trevin (jmdelarosa@cnb.csic.es)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FO A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
28 #include "core/xmipp_hdf5.h"
30 
32 
34 {
35 protected:
40  double sampling;
41 
42  void defineParams()
43  {
44  produces_an_output = true;
45  get_image_info = false;
46 
48  addUsageLine("Operate with image files headers. By default in Xmipp, geometrical transformations");
49  addUsageLine("coming in images files headers are ignored. Instead this information is read from");
50  addUsageLine("the images metadata if exist. With this program geometrical transformations can be");
51  addUsageLine("extracted to a metadata or assigned to header, also allows print or reset image file headers.");
52  addParamsLine("[ --print <decompose=0>] : Print the geometrical transformations in image file headers.");
53  addParamsLine(" : if input is stack and decompose=1 print header of each individual image.");
54  addParamsLine(" alias -p;");
55  addParamsLine("or --extract : The output is a selfile with geometrical transformations read from image file headers.");
56  addParamsLine(" alias -e;");
57  addParamsLine(" requires -o;");
58  addParamsLine("or --assign : Write the geometrical transformations from selfile to the image file headers.");
59  addParamsLine(" alias -a;");
60  addParamsLine("or --reset : Reset the geometrical transformations in image file headers.");
61  addParamsLine(" alias -r;");
62  addParamsLine("or --tree : Print the tree scheme from file containers as hdf5 files.");
63  addParamsLine(" alias -t;");
64  addParamsLine("or --sampling_rate <Ts=-1> : Change the sampling rate (in Angstrom units) in the image file header.");
65  addParamsLine(" : If no value is passed then current value in header is print.");
66  addParamsLine(" alias -s;");
67  addParamsLine(" [--round_shifts] :Round shifts to integers");
68  addExampleLine("Print the header of the images in metadata: ", false);
69  addExampleLine("xmipp_image_header -i images.sel");
70  addExampleLine("Extract geometrical transformations from image file headers: ", false);
71  addExampleLine("xmipp_image_header -i smallStack.stk --extract -o header.doc");
72  addExampleLine("Assign the geometrical transformations from the metadata to header: ", false);
73  addExampleLine("xmipp_image_header -i header.doc --assign");
74  addSeeAlsoLine("transform_geometry");
75  addKeywords("header, geometric, transformation, print");
76  }
77 
78  void readParams()
79  {
80  if (checkParam("--extract"))
81  {
82  operation = HEADER_EXTRACT;
83  produces_a_metadata = true;
84  }
85  else if (checkParam("--assign"))
86  operation = HEADER_ASSIGN;
87  else if (checkParam("--reset"))
88  operation = HEADER_RESET;
89  else if (checkParam("--sampling_rate"))
90  {
91  operation = HEADER_SAMPLINGRATE;
92  sampling = getDoubleParam("--sampling_rate");
93  allow_time_bar = false;
94  }
95  else if (checkParam("--tree"))
96  {
97  operation = HEADER_TREE;
98  decompose_stacks = false;
99  }
100  else
101  {
102  operation = HEADER_PRINT;
103  allow_time_bar = false;
104  decompose_stacks = getIntParam("--print") == 1;
105  }
107  round_shifts = checkParam("--round_shifts");
108  if (operation != HEADER_EXTRACT && checkParam("-o"))
109  REPORT_ERROR(ERR_PARAM_INCORRECT, "Argument -o is not valid for this operation");
110  params.datamode = HEADER;
111  }
112 
113  void show()
114  {
115  if (verbose == 0)
116  return;
117 
118  String msg;
119  switch (operation)
120  {
121  case HEADER_PRINT:
122  msg = "Printing headers...";
123  break;
124  case HEADER_EXTRACT:
125  msg = "Extracting image(s) geometrical transformations from header to metadata...";
126  break;
127  case HEADER_ASSIGN:
128  msg = "Assigning image(s) geometrical transformations from metadata to header...";
129  break;
130  case HEADER_RESET:
131  msg = "Reseting geometrical transformations from headers...";
132  break;
133  case HEADER_SAMPLINGRATE:
134  if (sampling > 0)
135  msg = "Setting sampling rate into headers...";
136  else
137  msg = "Showing sampling rate from headers...";
138  break;
139  case HEADER_TREE:
140  msg = "Printing tree structure...";
141  break;
142  }
143  std::cout << msg << std::endl << "Input: " << fn_in << std::endl;
144 
145  if (checkParam("-o"))
146  std::cout << "Output: " << fn_out << std::endl;
147 
148  }
149 
150  void roundShifts(MDRow &row)
151  {
152  double aux = 0.;
153  if (row.getValue(MDL_SHIFT_X, aux))
154  {
155  aux = (double)ROUND(aux);
156  row.setValue(MDL_SHIFT_X, aux);
157  }
158  if (row.getValue(MDL_SHIFT_Y, aux))
159  {
160  aux = (double)ROUND(aux);
161  row.setValue(MDL_SHIFT_Y, aux);
162  }
163  }
164 
165  void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
166  {
167 
168  ImageGeneric img;
169 
170  switch (operation)
171  {
172  case HEADER_PRINT:
173  img.read(fnImg, _HEADER_ALL);
174  img.print();
175  break;
176  case HEADER_EXTRACT:
177  img.read(fnImg, _HEADER_ALL);
178  rowOut = img.getGeometry();
179  if (round_shifts)
180  roundShifts(rowOut);
181  rowOut.setValue(MDL_IMAGE, fnImgOut);
182  break;
183  case HEADER_ASSIGN:
184  rowOut = rowIn;
185  if (round_shifts)
186  roundShifts(rowOut);
187  img.readApplyGeo(fnImg, rowOut, params);
189  img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
190  break;
191  case HEADER_RESET:
192  img.read(fnImg, _HEADER_ALL);
193  img.initGeometry();
194  img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
195  break;
196  case HEADER_SAMPLINGRATE:
197  {
198  img.read(fnImg, _HEADER_ALL);
199  if (sampling < 0)
200  {
201  double samplingRead;
202  img.image->MDMainHeader.getValue(MDL_SAMPLINGRATE_X, samplingRead);
203  std::cout << samplingRead << std::endl;
204  }
205  else
206  {
210  img.write(fnImg, ALL_IMAGES, fnImg.isInStack(), WRITE_REPLACE);
211  std::cout << "New sampling rate (Angstrom) = " << sampling << std::endl;
212  }
213  }
214  break;
215  case HEADER_TREE:
216  {
217  XmippH5File H5File;
218  FileName filename = fnImg.removeAllPrefixes().removeFileFormat();
219 
220  if (H5File.isHdf5(filename.c_str()))
221  {
222  H5File.openFile(fnImg.removeAllPrefixes().removeFileFormat(), H5F_ACC_RDONLY);
223  H5File.showTree();
224  }
225  else
226  REPORT_ERROR(ERR_IMG_UNKNOWN, "Unknown file format to display its data structure.");
227  }
228  break;
229  }
230  }
231 
233  {
234  if (operation ==HEADER_EXTRACT)
235  single_image = false;
237  }
238 }
239 ;// end of class ProgHeader
void setDataMode(DataMode mode)
MDRowVec row
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)
Parameter incorrect.
Definition: xmipp_error.h:181
sampling rate in A/pixel (double)
MDRow & getGeometry(const size_t n=0)
double getDoubleParam(const char *param, int arg=0)
void showTree(std::ostream &out=std::cout)
Definition: xmipp_hdf5.cpp:191
sampling rate in A/pixel (double)
sampling rate in A/pixel (double)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
FileName removeFileFormat() const
void openFile(const H5std_string &name, unsigned int flags, const H5::FileAccPropList &access_plist=H5::FileAccPropList::DEFAULT)
Definition: xmipp_hdf5.cpp:112
void defineParams()
void setValue(const MDObject &object) override
Shift for the image in the X axis (double)
void finishProcessing()
Unknown image type.
Definition: xmipp_error.h:130
bool single_image
Input is a single image.
void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut)
bool get_image_info
Get the input image file dimensions to further operations.
ApplyGeoParams params
void initGeometry(const size_t n=0)
void addSeeAlsoLine(const char *seeAlso)
T & getValue(MDLabel label)
void addKeywords(const char *keywords)
FileName removeAllPrefixes() const
T & getValue(MDLabel label)
FileName fn_in
Filenames of input and output Metadata.
bool round_shifts
void roundShifts(MDRow &row)
HeaderOperation operation
HeaderOperation
bool produces_an_output
Indicate that a unique final output is produced.
void addExampleLine(const char *example, bool verbatim=true)
#define ROUND(x)
Definition: xmipp_macros.h:210
int verbose
Verbosity level.
ImageBase * image
bool allow_time_bar
Show process time bar.
MDRowVec MDMainHeader
void setValue(MDLabel label, const T &d, bool addLabel=true)
void readParams()
bool decompose_stacks
Input Metadata will treat a stack file as a set of images instead of a unique file.
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams &params=DefaultApplyGeoParams)
std::string String
Definition: xmipp_strings.h:34
#define ALL_IMAGES
bool checkParam(const char *param)
Shift for the image in the Y axis (double)
void addUsageLine(const char *line, bool verbatim=false)
int getIntParam(const char *param, int arg=0)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
Name of an image (std::string)
bool produces_a_metadata
Indicate that the unique final output file is a Metadata.
void addParamsLine(const String &line)
bool isInStack() const
double sampling