Xmipp  v3.23.11-Nereus
image_odd_even.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Jose Luis Vilas, jlvilas@cnb.csic.es
4  * Carlos Oscar S. Sorzano coss@cnb.csic.es (2019)
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 FOR 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 
27 #include "image_odd_even.h"
29 //#define DEBUG
30 //#define DEBUG_MASK
31 
32 void ProgOddEven::readParams()
33 {
34  fnImg = getParam("--img");
35  splitType = getParam("--type");
36  sumFrames = checkParam("--sum_frames");
37  fnOut_odd = getParam("-o");
38  fnOut_even = getParam("-e");
39 }
40 
41 
42 void ProgOddEven::defineParams()
43 {
44  addUsageLine("This function splits a set of images or frames in two subsets named odd and even");
45  addParamsLine(" --img <img_file=\"\"> : File of input images (movie or images tilt series)");
46  addParamsLine(" --type <split_type> : Type of splitting");
47  addParamsLine(" :+ frames - If the frames will be split)");
48  addParamsLine(" :+ images - If the images will be split)");
49  addParamsLine("[ --sum_frames] : Sum the set of split frames");
50  addParamsLine(" -o <img_file=\"\"> : File of odd images/frames)");
51  addParamsLine(" -e <img_file=\"\"> : File of even images/frames)");
52 }
53 
54 
55 void ProgOddEven::fromimageToMd(FileName fnImg, MetaData &movienew, size_t &Xdim, size_t &Ydim)
56 {
57  ImageGeneric movieStack;
58  movieStack.read(fnImg, HEADER);
59  size_t Zdim, Ndim;
60  movieStack.getDimensions(Xdim, Ydim, Zdim, Ndim);
61  if (fnImg.getExtension() == "mrc" and Ndim == 1)
62  {
63  Ndim = Zdim;
64  }
65  size_t id;
66  FileName fn;
67  for (size_t i = 0; i < Ndim; i++)
68  {
69  id = movienew.addObject();
70  fn.compose(i + FIRST_IMAGE, fnImg);
71  movienew.setValue(MDL_IMAGE, fn, id);
72  }
73 }
74 
75 
76 void ProgOddEven::run()
77 {
78  std::cout << "Starting..." << std::endl;
79  size_t Xdim = 0, Ydim = 0;
80  if ((splitType != "frames") and (splitType != "images"))
81  {
82  std::cout << "ERROR: Please specify the type of splitting in frames or images" << std::endl;
83  std::cout << " --type frames for splitting the set of frames or --type images for splitting"
84  "the set of images" << std::endl;
85  exit(0);
86  }
87 
88  MetaDataVec movie, movienew;
89 
90  if (splitType == "frames")
91  {
92  if (fnImg.isMetaData())
93  {
94  movie.read(fnImg);
95  }
96  else
97  {
98  fromimageToMd(fnImg, movienew, Xdim, Ydim);
99  }
100  }
101 
102  if (splitType == "images")
103  {
104  fromimageToMd(fnImg, movienew, Xdim, Ydim);
105  }
106 
107  long n = 1;
108  MetaDataVec movieOdd, movieEven;
109 
110  FileName fnFrame;
111  size_t objId_odd, objId_even;
112  Image<double> frame, imgOdd, imgEven;
113 
114  MultidimArray<double> &ptrEven = imgEven();
115  MultidimArray<double> &ptrOdd = imgOdd();
116  MultidimArray<double> &ptrframe = frame();
117 
118  ptrEven.initZeros(Ydim, Xdim);
119  ptrOdd.initZeros(Ydim, Xdim);
120 
121  for (size_t objId : movienew.ids())
122  {
123  movienew.getValue(MDL_IMAGE, fnFrame, objId);
124  if (sumFrames)
125  {
126  frame.read(fnFrame);
127  }
128 
129  if (objId%2 == 0)
130  {
131  objId_even = movieEven.addObject();
132  movieEven.setValue(MDL_IMAGE, fnFrame, objId_even);
133  if (sumFrames)
134  {
135  for (size_t i =0; i<Ydim;++i)
136  {
137  for (size_t j =0; j<Xdim;++j)
138  {
139  DIRECT_A2D_ELEM(ptrEven, i, j) += DIRECT_A2D_ELEM(ptrframe, i, j);
140  }
141  }
142  }
143  }
144  else
145  {
146  objId_odd = movieOdd.addObject();
147  movieOdd.setValue(MDL_IMAGE, fnFrame, objId_odd);
148  if (sumFrames)
149  {
150  for (size_t i =0; i<Ydim;++i)
151  {
152  for (size_t j =0; j<Xdim;++j)
153  {
154  DIRECT_A2D_ELEM(ptrOdd, i, j) += DIRECT_A2D_ELEM(ptrframe, i, j);
155  }
156  }
157  }
158  }
159  }
160 
161  movieOdd.write(fnOut_odd);
162  movieEven.write(fnOut_even);
163 
164  if (sumFrames)
165  {
166  imgOdd.write(fnOut_odd.withoutExtension()+"_aligned.mrc");
167  imgEven.write(fnOut_even.withoutExtension()+"_aligned.mrc");
168  }
169 }
170 
virtual size_t addObject()=0
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define DIRECT_A2D_ELEM(v, i, j)
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 compose(const String &str, const size_t no, const String &ext="")
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
virtual IdIteratorProxy< false > ids()
#define i
String getExtension() const
const char * getParam(const char *param, int arg=0)
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
#define j
bool getValue(MDObject &mdValueOut, size_t id) const override
bool setValue(const MDLabel label, const T &valueIn, size_t id)
bool isMetaData(bool failIfNotExists=true) const
FileName withoutExtension() const
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)
#define FIRST_IMAGE
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
int * n
Name of an image (std::string)
void addParamsLine(const String &line)