Xmipp  v3.23.11-Nereus
Classes | Public Member Functions | Public Attributes | List of all members
Micrograph Class Reference

#include <micrograph.h>

Collaboration diagram for Micrograph:
Collaboration graph
[legend]

Classes

struct  Point
 

Public Member Functions

void clear ()
 
int getDatatype () const
 
int getDatatypeDetph () const
 
void open_micrograph (const FileName &fn_micrograph)
 
void close_micrograph ()
 
const FileNamemicrograph_name ()
 
void set_micrograph_name (const FileName &fn)
 
void set_ctfparams (const MDRowVec &ctf)
 
const MDRowget_ctfparams ()
 
void setStdevFilter (double d)
 
void write_coordinates (int label, double minCost, const FileName &fn_coords="")
 
void read_coordinates (int label, const FileName &fn_coords)
 
void transform_coordinates (const Matrix2D< double > &M)
 
void scale_coordinates (const double &c)
 
int ParticleNo () const
 
std::vector< Particle_coords > & Particles ()
 
void get_Particles (std::vector< Particle_coords > &_coords)
 
void set_window_size (int _X_window_size, int _Y_window_size)
 
void set_transmitance_flag (bool flag_value)
 
void setDataType (DataType _datatype)
 
bool read_transmitance_flag (void)
 
void set_inverse_flag (bool flag_value)
 
bool read_inverse_flag (void)
 
template<typename T >
int templateScissor (const Image< T > &I, const Particle_coords &P, MultidimArray< double > &result, double Dmin, double Dmax, double scaleX, double scaleY, bool only_check, bool fillBorders)
 
int scissor (const Particle_coords &P, MultidimArray< double > &result, double Dmin, double Dmax, double scaleX=1, double scaleY=1, bool only_check=false, bool fillBorders=false)
 
double operator() (size_t y, size_t x) const
 
void computeDoubleMinMax (double &Dmin, double &Dmax) const
 
void set_val (int y, int x, double new_val)
 
void produce_all_images (int label, double minCost, const FileName &fn_root, const FileName &fn_image="", double ang=0, bool rmStack=false, bool fillBorders=false, bool extractNoise=false, int Nnoise=-1)
 
int search_coord_near (int x, int y, int prec=3) const
 
void invalidate_coord (int n)
 
int add_coord (int x, int y, int label, double cost)
 
void move_last_coord_to (int x, int y)
 
Particle_coordscoord (int n)
 
void get_coord (int n, Particle_coords &_coords)
 
int add_label (const std::string &label)
 
int LabelNo ()
 
std::string & get_label (int n)
 
void get_label (int n, std::string &_label)
 
void size (int &_Xdim, int &_Ydim) const
 
void resize (int Xdim, int Ydim, const FileName &filename="")
 Set micrograph size (when you do not read the file from disk) More...
 
void write (const FileName &fileName, CastWriteMode castMode=CW_CAST)
 

Public Attributes

size_t Xdim
 
size_t Ydim
 
size_t Zdim
 
size_t Ndim
 
Point point1
 
Point point2
 
std::vector< Particle_coordscoords
 

Detailed Description

Micrography class. This class manages a large micrograph on disk. The image is not loaded into memory, that should avoid memory problems

Definition at line 78 of file micrograph.h.

Member Function Documentation

◆ add_coord()

int Micrograph::add_coord ( int  x,
int  y,
int  label,
double  cost 
)

Add coordinate. It returns the index of the particle added within the coordinate list.

Definition at line 491 of file micrograph.cpp.

492 {
493  struct Particle_coords aux;
494  aux.valid = true;
495  aux.X = x;
496  aux.Y = y;
497  aux.label = label;
498  aux.cost = cost;
499  aux.scoreVar = -1;
500  aux.scoreGini = -1;
501  coords.push_back(aux);
502  return coords.size() - 1;
503 }
static double * y
std::vector< Particle_coords > coords
Definition: micrograph.h:118
bool valid
Valid.
Definition: micrograph.h:64
doublereal * x
int label
Label.
Definition: micrograph.h:58
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66

◆ add_label()

int Micrograph::add_label ( const std::string &  label)
inline

Add label. The index assigned to the label is returned

Definition at line 501 of file micrograph.h.

502  {
503  labels.push_back(label);
504  return labels.size() - 1;
505  }
int label
Label.
Definition: micrograph.h:58

◆ clear()

void Micrograph::clear ( )

Clear

Definition at line 41 of file micrograph.cpp.

42 {
43  single_particle.clear();
44  coords.clear();
45  fn_coords = fn_micrograph = "";
46  X_window_size = Y_window_size = -1;
47  fh_micrograph = -1;
48  Xdim = Ydim = -1;
49  datatype = -1;
50  compute_transmitance = false;
51  compute_inverse = false;
52  IUChar.clear();
53  IShort.clear();
54  IUShort.clear();
55  IInt.clear();
56  IUInt.clear();
57  IFloat.clear();
58 }
std::vector< Particle_coords > coords
Definition: micrograph.h:118
size_t Xdim
Definition: micrograph.h:112
size_t Ydim
Definition: micrograph.h:113
void clear()
Definition: xmipp_image.h:144

◆ close_micrograph()

void Micrograph::close_micrograph ( )

Close micrograpgh. After working with the file, you must close it.

Definition at line 135 of file micrograph.cpp.

136 {
137  switch (datatype)
138  {
139  case DT_UHalfByte:
140  case DT_UChar:
141  IUChar.clear();
142  break;
143  case DT_UShort:
144  IUShort.clear();
145  break;
146  case DT_Short:
147  IShort.clear();
148  break;
149  case DT_Int:
150  IInt.clear();
151  break;
152  case DT_UInt:
153  IUInt.clear();
154  break;
155  case DT_Float:
156  IFloat.clear();
157  break;
158  default:
159  std::cerr << "Micrograph::close_micrograph: Unknown datatype "
160  << datatype << std::endl;
162  break;
163  }
164 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect type received.
Definition: xmipp_error.h:190
void clear()
Definition: xmipp_image.h:144

◆ computeDoubleMinMax()

void Micrograph::computeDoubleMinMax ( double &  Dmin,
double &  Dmax 
) const
inline

Micrograph max min

Definition at line 388 of file micrograph.h.

389  {
390  if (datatype == DT_UChar || datatype == DT_UHalfByte)
391  {
392  return IUChar().computeDoubleMinMax(Dmin,Dmax);
393  }
394  else if (datatype == DT_UShort)
395  {
396  return IUShort().computeDoubleMinMax(Dmin,Dmax);
397  }
398  else if (datatype == DT_Short)
399  {
400  return IShort().computeDoubleMinMax(Dmin,Dmax);
401  }
402  else if (datatype == DT_UInt)
403  {
404  return IUInt().computeDoubleMinMax(Dmin,Dmax);
405  }
406  else if (datatype == DT_Int)
407  {
408  return IInt().computeDoubleMinMax(Dmin,Dmax);
409  }
410  else if (datatype == DT_Float)
411  {
412  return IFloat().computeDoubleMinMax(Dmin,Dmax);
413  }
414 
415  else
416  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::computeDoubleMinMax::(): unknown datatype");
417  }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect type received.
Definition: xmipp_error.h:190

◆ coord()

Particle_coords& Micrograph::coord ( int  n)
inline

Access to coordinate structure. If the index is out of range then an exception is thrown.

Definition at line 486 of file micrograph.h.

487  {
488  if (n < 0 || n > ParticleNo())
489  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "Micrograph::coord(): index out of range");
490  return coords[n];
491  }
Index out of bounds.
Definition: xmipp_error.h:132
int ParticleNo() const
Definition: micrograph.h:188
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int * n

◆ get_coord()

void Micrograph::get_coord ( int  n,
Particle_coords _coords 
)
inline

Another function for accessing the coordinate structure.

Definition at line 494 of file micrograph.h.

495  {
496  _coords = coord(n);
497  }
Particle_coords & coord(int n)
Definition: micrograph.h:486
int * n

◆ get_ctfparams()

const MDRow& Micrograph::get_ctfparams ( )
inline

Return the row with CTF params.

Definition at line 160 of file micrograph.h.

161  {
162  return ctfRow;
163  }

◆ get_label() [1/2]

std::string& Micrograph::get_label ( int  n)
inline

Get a label. An exception is thrown if the index is greater than the number of labels

Definition at line 516 of file micrograph.h.

517  {
518  if (n < 0 || n > LabelNo())
519  REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, "Micrograph::get_label(): index out of range");
520  return labels[n];
521  }
Index out of bounds.
Definition: xmipp_error.h:132
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
int LabelNo()
Definition: micrograph.h:508
int * n

◆ get_label() [2/2]

void Micrograph::get_label ( int  n,
std::string &  _label 
)
inline

Another function for get a lebel.

Definition at line 524 of file micrograph.h.

525  {
526  _label = get_label(n);
527  }
std::string & get_label(int n)
Definition: micrograph.h:516
int * n

◆ get_Particles()

void Micrograph::get_Particles ( std::vector< Particle_coords > &  _coords)
inline

Another function for getting the particles.

Definition at line 201 of file micrograph.h.

202  {
203  _coords = coords;
204  }
std::vector< Particle_coords > coords
Definition: micrograph.h:118

◆ getDatatype()

int Micrograph::getDatatype ( ) const
inline

Get micrograph datatype.

Definition at line 124 of file micrograph.h.

125  {
126  return datatype;
127  }

◆ getDatatypeDetph()

int Micrograph::getDatatypeDetph ( ) const

Get micrograph datatype.

Definition at line 166 of file micrograph.cpp.

167 {
168  switch (datatype)
169  {
170  case DT_UHalfByte:
171  case DT_UChar:
172  return (8 * sizeof(unsigned char));
173  case DT_UShort:
174  return (8 * sizeof(unsigned short));
175  case DT_Short:
176  return (8 * sizeof(short));
177  case DT_UInt:
178  return (8 * sizeof(unsigned int));
179  case DT_Int:
180  return (8 * sizeof(int));
181  case DT_Float:
182  return (8 * sizeof(float));
183  default:
184  std::cerr << "Micrograph::getDatatypeDetph: Unknown datatype "
185  << datatype << std::endl;
187  break;
188  }
189 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect type received.
Definition: xmipp_error.h:190

◆ invalidate_coord()

void Micrograph::invalidate_coord ( int  n)

Remove a coordinate from the coordinate list. An exception is thrown if the index is out of range within the coordinate list

Definition at line 482 of file micrograph.cpp.

483 {
484  if (n < 0 || n >= ParticleNo())
486  "Micrograph::invalidate_coord: Index out of range");
487  coords[n].valid = false;
488 }
Index out of bounds.
Definition: xmipp_error.h:132
int ParticleNo() const
Definition: micrograph.h:188
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int * n

◆ LabelNo()

int Micrograph::LabelNo ( )
inline

Number of labels.

Definition at line 508 of file micrograph.h.

509  {
510  return labels.size();
511  }

◆ micrograph_name()

const FileName& Micrograph::micrograph_name ( )
inline

Get micrograph filename.

Definition at line 140 of file micrograph.h.

141  {
142  return fn_micrograph;
143  }

◆ move_last_coord_to()

void Micrograph::move_last_coord_to ( int  x,
int  y 
)

Move last coordinate to this position.

Definition at line 506 of file micrograph.cpp.

507 {
508  if (coords.size() > 0)
509  {
510  coords.back().X = x;
511  coords.back().Y = y;
512  }
513 }
static double * y
std::vector< Particle_coords > coords
Definition: micrograph.h:118
doublereal * x

◆ open_micrograph()

void Micrograph::open_micrograph ( const FileName fn_micrograph)

Open micrograph. An exception is thrown if the file is not valid.

Definition at line 61 of file micrograph.cpp.

62 {
63  clear();
64  // Micrograph name
65  fn_micrograph = _fn_micrograph;
66  // Look for micrograph dimensions
67  Image<char> auxI = {};
68  auxI.read(fn_micrograph, HEADER);
69 
70  auxI.getDimensions(Xdim, Ydim, Zdim, Ndim);
71  if ((Zdim > 1) || (Ndim > 1))
74  "Micrograph::open_micrograph: Only files with a single micrograph may be processed. Error reading " + fn_micrograph);
75  auxI.MDMainHeader.getValue(MDL_DATATYPE, datatype);
76  __offset = 0;
77  auxI.clear();
78  //#define DEBUG
79 #ifdef DEBUG
80 
81  std::cerr << "x,y,z,n, datatype : "
82  << Xdim << " "
83  << Ydim << " "
84  << Zdim << " "
85  << Ndim << " "
86  << datatype << " "
87  <<std::endl;
88 #endif
89 #undef DEBUG
90  //3,4,6
91  // Open micrograph and map
92  int result;
93  switch (datatype)
94  {
95  case DT_UHalfByte:
96  case DT_UChar:
97  result = IUChar.readMapped(fn_micrograph, FIRST_IMAGE);
98  pixelDesvFilter(IUChar.data, stdevFilter);
99  break;
100  case DT_UShort:
101  result = IUShort.readMapped(fn_micrograph, FIRST_IMAGE);
102  pixelDesvFilter(IUShort.data, stdevFilter);
103  break;
104  case DT_Short:
105  result = IShort.readMapped(fn_micrograph, FIRST_IMAGE);
106  pixelDesvFilter(IShort.data, stdevFilter);
107  break;
108  case DT_Int:
109  result = IInt.readMapped(fn_micrograph, FIRST_IMAGE);
110  pixelDesvFilter(IInt.data, stdevFilter);
111  break;
112  case DT_UInt:
113  result = IUInt.readMapped(fn_micrograph, FIRST_IMAGE);
114  pixelDesvFilter(IUChar.data, stdevFilter);
115  break;
116  case DT_Float:
117  result = IFloat.readMapped(fn_micrograph, FIRST_IMAGE);
118  pixelDesvFilter(IFloat.data, stdevFilter);
119  break;
120  default:
121  std::cerr << "Micrograph::open_micrograph: Unknown datatype "
122  << datatype << std::endl;
124  break;
125  }
126  //fh_micrograph = open(fn_micrograph.c_str(), O_RDWR, S_IREAD | S_IWRITE);
127  if (result < 0)
128  REPORT_ERROR(
130  (std::string)"Micrograph::open_micrograph: There is a "
131  "problem opening " + fn_micrograph + "\nCheck that the file has write permission");
132 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
if read from file original image datatype, this is an struct defined in image
T & getValue(MDLabel label)
MultidimArray< T > data
Definition: xmipp_image.h:55
void clear()
Definition: micrograph.cpp:41
size_t Zdim
Definition: micrograph.h:114
File or directory does not exist.
Definition: xmipp_error.h:136
size_t Xdim
Definition: micrograph.h:112
void pixelDesvFilter(MultidimArray< T > &V, double thresFactor)
Definition: filters.h:1378
MDRowVec MDMainHeader
size_t Ndim
Definition: micrograph.h:115
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
#define FIRST_IMAGE
Incorrect MultidimArray dimensions.
Definition: xmipp_error.h:173
size_t Ydim
Definition: micrograph.h:113
Incorrect type received.
Definition: xmipp_error.h:190
int readMapped(const FileName &name, size_t select_img=ALL_IMAGES, int mode=WRITE_READONLY)
void clear()
Definition: xmipp_image.h:144
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const

◆ operator()()

double Micrograph::operator() ( size_t  y,
size_t  x 
) const
inline

Pixel access for reading. These coordinates follow the physical Xmipp convention {../../../Extra_Docs/Conventions.html} for coordinates

Definition at line 354 of file micrograph.h.

355  {
356  if (y < 0 || y >= Ydim || x < 0 || x >= Xdim)
357  // COSS: REPORT_ERROR(1, "Micrograph::(): index out of range");
358  return 0;
359  if (datatype == DT_UChar || datatype == DT_UHalfByte)
360  {
361  return IMGPIXEL(IUChar,y,x);
362  }
363  else if (datatype == DT_UShort)
364  {
365  return IMGPIXEL(IUShort,y,x);
366  }
367  else if (datatype == DT_Short)
368  {
369  return IMGPIXEL(IShort,y,x);
370  }
371  else if (datatype == DT_UInt)
372  {
373  return IMGPIXEL(IUInt,y,x);
374  }
375  else if (datatype == DT_Int)
376  {
377  return IMGPIXEL(IInt,y,x);
378  }
379  else if (datatype == DT_Float)
380  {
381  return IMGPIXEL(IFloat,y,x);
382  }
383  else
384  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::(): unknown datatype");
385  }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static double * y
doublereal * x
size_t Xdim
Definition: micrograph.h:112
size_t Ydim
Definition: micrograph.h:113
Incorrect type received.
Definition: xmipp_error.h:190
#define IMGPIXEL(I, i, j)

◆ ParticleNo()

int Micrograph::ParticleNo ( ) const
inline

Particle number. Number of particles in the coordinate list

Definition at line 188 of file micrograph.h.

189  {
190  return coords.size();
191  }
std::vector< Particle_coords > coords
Definition: micrograph.h:118

◆ Particles()

std::vector<Particle_coords>& Micrograph::Particles ( )
inline

Particle. Return the list of particles.

Definition at line 195 of file micrograph.h.

196  {
197  return coords;
198  }
std::vector< Particle_coords > coords
Definition: micrograph.h:118

◆ produce_all_images()

void Micrograph::produce_all_images ( int  label,
double  minCost,
const FileName fn_root,
const FileName fn_image = "",
double  ang = 0,
bool  rmStack = false,
bool  fillBorders = false,
bool  extractNoise = false,
int  Nnoise = -1 
)

Produce all single particle images. The file fn_micrograph+".sel" is also generated. The angle is the angle from the Y axis to the tilt axis, angles are positive clockwise. Images are rotated by -ang. If this angle is 0 no rotation is applied.

Definition at line 317 of file micrograph.cpp.

321 {
322  MetaDataVec SF;
323  Image<double> I;
324  Micrograph *M;
325 
326  // Set Source image
327  if (fn_image == "")
328  M = this;
329  else
330  {
331  M = new Micrograph;
332  M->open_micrograph(fn_image/*, swapbyte*/);
333  M->set_window_size(X_window_size, Y_window_size);
334  M->set_transmitance_flag(compute_transmitance);
335  M->set_inverse_flag(compute_inverse);
336  }
337 
338  // Set scale for particles
339  int MXdim;
340  int MYdim;
341  int thisXdim;
342  int thisYdim;
343  M->size(MXdim, MYdim);
344  this->size(thisXdim, thisYdim);
345  double scaleX = (double) MXdim / thisXdim;
346  double scaleY = (double) MYdim / thisYdim;
347 
348  // Compute max and minimum if compute_transmitance
349  // or compute_inverse flags are ON
350  double Dmax=0.;
351  double Dmin=0.;
352  if (compute_transmitance || compute_inverse)
353  {
354  (*this).computeDoubleMinMax(Dmin, Dmax);
355 
356  if (compute_transmitance)
357  {
358  if (Dmin > 1)
359  Dmin = log10(Dmin);
360  if (Dmax > 1)
361  Dmax = log10(Dmax);
362  }
363  }
364  // Scissor all particles
365  if (ang != 0)
366  std::cout << "Angle from Y axis to tilt axis " << ang << std::endl
367  << " applying appropriate rotation\n";
368  int nmax = ParticleNo();
369  int nparticles = nmax;
370  if (extractNoise && Nnoise>0)
371  nmax=Nnoise;
372  FileName fn_aux;
373  FileName _ext = fn_rootIn.getFileFormat();
374  FileName fn_out;
375  FileName fn_root = fn_rootIn.removeFileFormat().removeLastExtension();
376  if (fn_rootIn.hasStackExtension())
377  fn_out=fn_root.addExtension(_ext);
378  else
379  fn_out=fn_rootIn.addExtension("stk");
380 
381  if (rmStack)
382  fn_out.deleteFile();
383  size_t ii = 0;
384  size_t id;
385 
386  Particle_coords Pnoise;
387  Pnoise.valid = true;
388  Pnoise.label = 0;
389  Pnoise.cost = 1;
390  Pnoise.scoreVar = -1;
391  Pnoise.scoreGini = -1;
392 
393  int minNoiseDistance=Y_window_size/2;
394  std::vector<Particle_coords> noiseCoords;
395 
396  for (int n = 0; n < nmax; n++)
397  {
398  if (coords[n].valid && coords[n].cost > minCost && coords[n].label == label)
399  {
400  fn_aux.compose(++ii, fn_out);
401  id = SF.addObject();
402  // If the ctfRow was set, copy the info to images metadata
403  if (ctfRow.containsLabel(MDL_CTF_DEFOCUSU))
404  SF.setRow(ctfRow, id);
405  SF.setValue(MDL_IMAGE, fn_aux, id);
406  SF.setValue(MDL_MICROGRAPH, M->fn_micrograph, id);
407  SF.setValue(MDL_XCOOR, coords[n].X, id);
408  SF.setValue(MDL_YCOOR, coords[n].Y, id);
409  if (coords[n].scoreVar>0)
410  {
413  }
414  bool t=false;
415  if (extractNoise)
416  {
417  // Look for coordinate that is away from the rest of coordinates
418  bool found=false;
419  while (!found)
420  {
421  Pnoise.X=int(rnd_unif(X_window_size,thisXdim-X_window_size));
422  Pnoise.Y=int(rnd_unif(Y_window_size,thisYdim-Y_window_size));
423  found=true;
424  for (int nn=0; nn<nparticles; nn++)
425  {
426  if (std::abs(Pnoise.X-coords[nn].X)<minNoiseDistance && std::abs(Pnoise.Y-coords[nn].Y)<minNoiseDistance)
427  {
428  found=false;
429  break;
430  }
431  }
432  }
433  noiseCoords.push_back(Pnoise);
434  t = M->scissor(Pnoise, I(), Dmin, Dmax, scaleX, scaleY, false, fillBorders);
435  }
436  else
437  t = M->scissor(coords[n], I(), Dmin, Dmax, scaleX, scaleY, false, fillBorders);
438  if (!t)
439  {
440  std::cout << "Particle " << fn_aux
441  << " is very near the border, "
442  << "corresponding image is set to blank\n";
443  SF.setValue(MDL_ENABLED, -1, id);
444  }
445  else
446  SF.setValue(MDL_ENABLED, 1, id);
447  // if (ang!=0) I().rotate(-ang);
448  I.write(fn_out, ii, true, WRITE_APPEND);
449  }
450  }
451  SF.write(fn_out.withoutExtension() + ".xmd");
452 
453 
454  // Free source image??
455  if (fn_image != "")
456  {
457  M->close_micrograph();
458  delete M;
459  }
460 
461  if (extractNoise)
462  {
463  coords.clear();
464  coords=noiseCoords;
465  }
466 }
int ParticleNo() const
Definition: micrograph.h:188
int * nmax
int Y
Y position.
Definition: micrograph.h:62
Defocus U (Angstroms)
int scissor(const Particle_coords &P, MultidimArray< double > &result, double Dmin, double Dmax, double scaleX=1, double scaleY=1, bool only_check=false, bool fillBorders=false)
Definition: micrograph.cpp:285
FileName removeLastExtension() const
FileName removeFileFormat() const
FileName addExtension(const String &ext) const
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)
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
void compose(const String &str, const size_t no, const String &ext="")
bool valid
Valid.
Definition: micrograph.h:64
void abs(Image< double > &op)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
bool containsLabel(MDLabel label) const override
void set_window_size(int _X_window_size, int _Y_window_size)
Definition: micrograph.h:209
double scoreVar
Score by var and Gini.
Definition: micrograph.h:68
Is this image enabled? (int [-1 or 1])
void open_micrograph(const FileName &fn_micrograph)
Definition: micrograph.cpp:61
double rnd_unif()
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
< Particle variance (double)
X component (int)
double scoreGini
Score by Gini.
Definition: micrograph.h:70
void log10(Image< double > &op)
Name of a micrograph (std::string)
void deleteFile() const
int label
Label.
Definition: micrograph.h:58
void close_micrograph()
Definition: micrograph.cpp:135
FileName withoutExtension() const
void set_transmitance_flag(bool flag_value)
Definition: micrograph.h:220
String getFileFormat() const
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
Y component (int)
void set_inverse_flag(bool flag_value)
Definition: micrograph.h:244
void size(int &_Xdim, int &_Ydim) const
Definition: micrograph.h:530
bool setRow(const MDRow &row, size_t id)
int * n
Name of an image (std::string)

◆ read_coordinates()

void Micrograph::read_coordinates ( int  label,
const FileName fn_coords 
)

Read coordinates from disk. Coordinates are read into the selected family, the rest of families are untouched as well as the coordinates already belonging to this family

Definition at line 216 of file micrograph.cpp.

217 {
218  std::ifstream fh;
219  int line_no = 0;
220  std::string line;
221 
222  fn_coords = _fn_coords;
223 
224  MetaDataVec MD;
225  MD.read(fn_coords);
226  line_no = MD.size();
227 
228  // Resize coordinate list and read
229  coords.reserve(line_no);
230  struct Particle_coords aux;
231  aux.valid = true;
232  aux.label = label;
233  aux.cost = 1;
234  aux.scoreVar = -1;
235  aux.scoreGini = -1;
236 
237  for (size_t objId : MD.ids())
238  {
239  MD.getValue(MDL_XCOOR, aux.X, objId); //aux.X=x;
240  MD.getValue(MDL_YCOOR, aux.Y, objId); //aux.Y=y;
242  {
243  MD.getValue(MDL_SCORE_BY_VAR, aux.scoreVar, objId);
244  MD.getValue(MDL_SCORE_BY_GINI, aux.scoreGini, objId);
245  }
246  coords.push_back(aux);
247  }
248 }
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
std::vector< Particle_coords > coords
Definition: micrograph.h:118
bool valid
Valid.
Definition: micrograph.h:64
virtual IdIteratorProxy< false > ids()
size_t size() const override
< Particle variance (double)
X component (int)
int label
Label.
Definition: micrograph.h:58
bool getValue(MDObject &mdValueOut, size_t id) const override
Y component (int)
bool containsLabel(const MDLabel label) const override

◆ read_inverse_flag()

bool Micrograph::read_inverse_flag ( void  )
inline

Get Log flag. When cutting images, the contrast is inverted if inverse flag=true. This function reads it

Definition at line 254 of file micrograph.h.

255  {
256  return compute_inverse;
257  }

◆ read_transmitance_flag()

bool Micrograph::read_transmitance_flag ( void  )
inline

Get Transmitance flag. When cutting images, 1/log10 is computed over the pixel values if transmitance_flag=true. This function reads it Note: if pixel_value=0, no log is computed

Definition at line 235 of file micrograph.h.

236  {
237  return compute_transmitance;
238  }

◆ resize()

void Micrograph::resize ( int  Xdim,
int  Ydim,
const FileName filename = "" 
)

Set micrograph size (when you do not read the file from disk)

Definition at line 515 of file micrograph.cpp.

516 {
517  this->Xdim = Xdim;
518  this->Ydim = Ydim;
519  this->Zdim = 1;
520  this->Ndim = 1;
521  if (datatype == DT_UChar || datatype == DT_UHalfByte)
522  {
523  IUChar.data.setMmap(true);
524  IUChar.data.resize(1, 1, Ydim, Xdim);
525  }
526  else if (datatype == DT_UShort)
527  {
528  IUShort.data.setMmap(true);
529  IUShort.data.resize(1, 1, Ydim, Xdim);
530  }
531  else if (datatype == DT_Short)
532  {
533  IShort.data.setMmap(true);
534  IShort.data.resize(1, 1, Ydim, Xdim);
535  }
536  else if (datatype == DT_UInt)
537  {
538  IUInt.data.setMmap(true);
539  IUInt.data.resize(1, 1, Ydim, Xdim);
540  }
541  else if (datatype == DT_Int)
542  {
543  IInt.data.setMmap(true);
544  IInt.data.resize(1, 1, Ydim, Xdim);
545  }
546  else if (datatype == DT_Float)
547  {
548  IFloat.data.setMmap(true);
549  IFloat.data.resize(1, 1, Ydim, Xdim);
550  }
551  else
552  REPORT_ERROR(ERR_TYPE_INCORRECT, "Unknown datatype");
553 
554 }
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
MultidimArray< T > data
Definition: xmipp_image.h:55
void setMmap(bool mmap)
size_t Zdim
Definition: micrograph.h:114
size_t Xdim
Definition: micrograph.h:112
size_t Ndim
Definition: micrograph.h:115
size_t Ydim
Definition: micrograph.h:113
Incorrect type received.
Definition: xmipp_error.h:190

◆ scale_coordinates()

void Micrograph::scale_coordinates ( const double &  c)

Multiply coordinates by a constant

Definition at line 270 of file micrograph.cpp.

271 {
272  int imax = coords.size();
273  for (int i = 0; i < imax; i++)
274  {
275  if (coords[i].valid)
276  {
277  coords[i].X = (int) (coords[i].X * c);
278  coords[i].Y = (int) (coords[i].Y * c);
279  }
280  }
281 
282 }
int Y
Y position.
Definition: micrograph.h:62
doublereal * c
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
bool valid
Valid.
Definition: micrograph.h:64
#define i

◆ scissor()

int Micrograph::scissor ( const Particle_coords P,
MultidimArray< double > &  result,
double  Dmin,
double  Dmax,
double  scaleX = 1,
double  scaleY = 1,
bool  only_check = false,
bool  fillBorders = false 
)

Scissor. The single particle is selected by an index within the particle coordinate list. If the index is beyond the number of particles ParticleNo , or the selfWindow size is not set (set_window_size ) an exception is thrown.

Make sure that index n represents a valid particle before cutting it

The scale affects the particle position, such that the position cut is pos*scale, but not the selfWindow size.

If only check is true then the particle is not scissored, but the routine only checks if it can be done.

Dmax and Dmin are used to invert the image and or compute the trnasmitance

Returns 0 if an error occurred and 1 if everything is all right

Definition at line 285 of file micrograph.cpp.

288 {
289  if (X_window_size == -1 || Y_window_size == -1)
291  "Micrograph::scissor: window size not set");
292  if (datatype == DT_UChar || datatype == DT_UHalfByte)
293  return templateScissor(IUChar, P, result, Dmin, Dmax, scaleX, scaleY,
294  only_check, fillBorders);
295  else if (datatype == DT_UShort)
296  return templateScissor(IUShort, P, result, Dmin, Dmax, scaleX, scaleY,
297  only_check, fillBorders);
298  else if (datatype == DT_Short)
299  return templateScissor(IShort, P, result, Dmin, Dmax, scaleX, scaleY,
300  only_check, fillBorders);
301  else if (datatype == DT_UInt)
302  return templateScissor(IUInt, P, result, Dmin, Dmax, scaleX, scaleY,
303  only_check, fillBorders);
304  else if (datatype == DT_Int)
305  return templateScissor(IInt, P, result, Dmin, Dmax, scaleX, scaleY,
306  only_check, fillBorders);
307  else if (datatype == DT_Float)
308  return templateScissor(IFloat, P, result, Dmin, Dmax, scaleX, scaleY,
309  only_check, fillBorders);
310  else
312  "Micrograph::scissor: unknown datatype");
313 
314 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect MultidimArray size.
Definition: xmipp_error.h:174
int templateScissor(const Image< T > &I, const Particle_coords &P, MultidimArray< double > &result, double Dmin, double Dmax, double scaleX, double scaleY, bool only_check, bool fillBorders)
Definition: micrograph.h:263
Incorrect type received.
Definition: xmipp_error.h:190

◆ search_coord_near()

int Micrograph::search_coord_near ( int  x,
int  y,
int  prec = 3 
) const

Search coordinate near a position. By default the precission is set to 3 pixels. The index of the coordinate within the list is returned. Returns -1 if none.

Definition at line 469 of file micrograph.cpp.

470 {
471  int imax = coords.size();
472  int prec2 = prec * prec;
473  for (int i = 0; i < imax; i++)
474  if ((coords[i].X - x) * (coords[i].X - x)
475  + (coords[i].Y - y) * (coords[i].Y - y) < prec2
476  && coords[i].valid)
477  return i;
478  return -1;
479 }
int Y
Y position.
Definition: micrograph.h:62
static double * y
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
bool valid
Valid.
Definition: micrograph.h:64
doublereal * x
#define i

◆ set_ctfparams()

void Micrograph::set_ctfparams ( const MDRowVec ctf)
inline

Set the CTF parameters

Definition at line 153 of file micrograph.h.

154  {
155  ctfRow = ctf;
156  ctfRow.detach();
157  }
void detach() override

◆ set_inverse_flag()

void Micrograph::set_inverse_flag ( bool  flag_value)
inline

Set Log flag. When cutting images, the contrast is inverted if inverse flag is true.

Definition at line 244 of file micrograph.h.

245  {
246  compute_inverse =
247  flag_value;
248  }

◆ set_micrograph_name()

void Micrograph::set_micrograph_name ( const FileName fn)
inline

Set micrograph filename.

Definition at line 146 of file micrograph.h.

147  {
148  fn_micrograph = fn;
149  }

◆ set_transmitance_flag()

void Micrograph::set_transmitance_flag ( bool  flag_value)
inline

Set Transmitance flag. When cutting images, 1/log10 is computed over the pixel values if this transmitance_flag=true. This function sets it Note: if pixel_value=0, no log is computed

Definition at line 220 of file micrograph.h.

221  {
222  compute_transmitance =
223  flag_value;
224  }

◆ set_val()

void Micrograph::set_val ( int  y,
int  x,
double  new_val 
)
inline

Pixel access for writing.

Definition at line 422 of file micrograph.h.

423  {
424  if (datatype == DT_UChar || datatype == DT_UHalfByte)
425  {
426  IMGPIXEL(IUChar,y,x) = (unsigned char) new_val;
427  }
428  else if (datatype == DT_UShort)
429  {
430  IMGPIXEL(IUShort,y,x) = (unsigned short) new_val;
431  }
432  else if (datatype == DT_Short)
433  {
434  IMGPIXEL(IShort,y,x) = (short) new_val;
435  }
436  else if (datatype == DT_UInt)
437  {
438  IMGPIXEL(IUInt,y,x) = (unsigned int) new_val;
439  }
440  else if (datatype == DT_Int)
441  {
442  IMGPIXEL(IInt,y,x) = (int) new_val;
443  }
444  else if (datatype == DT_Float)
445  {
446  IMGPIXEL(IFloat,y,x) = (float) new_val;
447  }
448 
449  else
450  REPORT_ERROR(ERR_TYPE_INCORRECT, "Micrograph::set_val::(): unknown datatype");
451 
452  }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static double * y
doublereal * x
Incorrect type received.
Definition: xmipp_error.h:190
#define IMGPIXEL(I, i, j)

◆ set_window_size()

void Micrograph::set_window_size ( int  _X_window_size,
int  _Y_window_size 
)
inline

Set selfWindow size. This selfWindow is set upon each coordinate and is used to cut all images.

Definition at line 209 of file micrograph.h.

210  {
211  X_window_size = _X_window_size;
212  Y_window_size = _Y_window_size;
213  }

◆ setDataType()

void Micrograph::setDataType ( DataType  _datatype)
inline

Definition at line 226 of file micrograph.h.

227  {
228  datatype = _datatype;
229  }

◆ setStdevFilter()

void Micrograph::setStdevFilter ( double  d)
inline

Definition at line 165 of file micrograph.h.

166  {
167  stdevFilter=d;
168  }
doublereal * d

◆ size()

void Micrograph::size ( int &  _Xdim,
int &  _Ydim 
) const
inline

Return micrograph size

Definition at line 530 of file micrograph.h.

531  {
532  _Xdim = Xdim;
533  _Ydim = Ydim;
534  }
size_t Xdim
Definition: micrograph.h:112
size_t Ydim
Definition: micrograph.h:113

◆ templateScissor()

template<typename T >
int Micrograph::templateScissor ( const Image< T > &  I,
const Particle_coords P,
MultidimArray< double > &  result,
double  Dmin,
double  Dmax,
double  scaleX,
double  scaleY,
bool  only_check,
bool  fillBorders 
)
inline

Templated scissor function. This is the one actually doing the work

Definition at line 263 of file micrograph.h.

267  {
268  result.initZeros(Y_window_size, X_window_size);
269  int _i0 = ROUND(scaleY * P.Y) + FIRST_XMIPP_INDEX(Y_window_size);
270  int _iF = ROUND(scaleY * P.Y) + LAST_XMIPP_INDEX(Y_window_size);
271  int _j0 = ROUND(scaleX * P.X) + FIRST_XMIPP_INDEX(X_window_size);
272  int _jF = ROUND(scaleX * P.X) + LAST_XMIPP_INDEX(X_window_size);
273  int retval = 1;
274  double irange=1.0/(Dmax - Dmin);
275 
276  if (!fillBorders && (_i0 < 0 || _iF >= Ydim || _j0 < 0 || _jF >= Xdim))
277  retval = 0;
278  else
279  if (!only_check)
280  {
281 
282  for (int i = _i0, i_i0=0; i <= _iF; i++, i_i0++)
283  {
284  int ifrom=i;
285  if (fillBorders)
286  {
287  if (ifrom<0)
288  ifrom=0;
289  else if (ifrom>=Ydim)
290  ifrom=Ydim-1;
291  }
292  for (int j=_j0, j_j0=0; j<=_jF; j++, j_j0++)
293  {
294  int jfrom=j;
295  if (fillBorders)
296  {
297  if (jfrom<0)
298  jfrom=0;
299  else if (jfrom>=Xdim)
300  jfrom=Xdim-1;
301  }
302  double val=IMGPIXEL(I,ifrom,jfrom);
303  if (compute_transmitance)
304  {
305  double temp;
306  if (val < 1)
307  temp = val;
308  else
309  temp = log10(val);
310  if (compute_inverse)
311  A2D_ELEM(result,i_i0, j_j0) = (Dmax - temp) * irange;
312  else
313  A2D_ELEM(result,i_i0, j_j0) = (temp - Dmin) * irange;
314  }
315  else
316  {
317  if (compute_inverse)
318  A2D_ELEM(result, i_i0, j_j0) = (Dmax - val) * irange;
319  else
320  A2D_ELEM(result, i_i0, j_j0) = val;
321  }
322  }
323  }
324  }
325  return retval;
326  }
int Y
Y position.
Definition: micrograph.h:62
#define A2D_ELEM(v, i, j)
int X
X position.
Definition: micrograph.h:60
#define i
#define ROUND(x)
Definition: xmipp_macros.h:210
size_t Xdim
Definition: micrograph.h:112
void log10(Image< double > &op)
#define j
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
size_t Ydim
Definition: micrograph.h:113
void initZeros(const MultidimArray< T1 > &op)
#define LAST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:448
#define IMGPIXEL(I, i, j)

◆ transform_coordinates()

void Micrograph::transform_coordinates ( const Matrix2D< double > &  M)

Transform all coordinates according to a 3x3 transformation matrix

Definition at line 251 of file micrograph.cpp.

252 {
253  Matrix1D<double> m(3);
255 
256  int imax = coords.size();
257  for (int i = 0; i < imax; i++)
258  {
259  if (coords[i].valid)
260  {
261  VECTOR_R3(m, coords[i].X, coords[i].Y, 1);
262  M3x3_BY_V3x1(m, M, m);
263  coords[i].X = (int) XX(m);
264  coords[i].Y = (int) YY(m);
265  }
266  }
267 }
int Y
Y position.
Definition: micrograph.h:62
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
bool valid
Valid.
Definition: micrograph.h:64
#define i
#define XX(v)
Definition: matrix1d.h:85
#define M3x3_BY_V3x1(a, M, b)
Definition: matrix2d.h:170
#define YY(v)
Definition: matrix1d.h:93
int m
#define VECTOR_R3(v, x, y, z)
Definition: matrix1d.h:124
#define SPEED_UP_temps012
Definition: xmipp_macros.h:403

◆ write()

void Micrograph::write ( const FileName fileName,
CastWriteMode  castMode = CW_CAST 
)

Write micrograph. Set adjust to true if the values should be scaled within the minimum and maximum of the output range. Ex: m.write(fn8bits+"%uint8");

Definition at line 555 of file micrograph.cpp.

556 {
557  if (datatype == DT_UChar)
558  {
559  IUChar.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
560  }
561  else if (datatype == DT_UHalfByte)
562  {
564  "Micrograph::set_val::(): non supported datatype UHalfByte");
565  }
566  else if (datatype == DT_UShort)
567  {
568  IUShort.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
569  }
570  else if (datatype == DT_Short)
571  {
572  IShort.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
573  }
574  else if (datatype == DT_UInt)
575  {
576  IUInt.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
577  }
578  else if (datatype == DT_Int)
579  {
580  IInt.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
581  }
582  else if (datatype == DT_Float)
583  {
584  IFloat.write(fileName, FIRST_IMAGE, false, WRITE_OVERWRITE, castMode);
585  }
586  else
588  "Micrograph::set_val::(): unknown datatype");
589 
590 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
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 FIRST_IMAGE
Incorrect type received.
Definition: xmipp_error.h:190

◆ write_coordinates()

void Micrograph::write_coordinates ( int  label,
double  minCost,
const FileName fn_coords = "" 
)

Save coordinates to disk.

Definition at line 192 of file micrograph.cpp.

194 {
195  std::ofstream fh;
196  if (_fn_coords != "")
197  fn_coords = _fn_coords;
198 
199  MetaDataVec MD;
200  MD.setComment((std::string) "Selected Coordinates for file " + fn_coords);
201  int imax = coords.size();
202  for (int i = 0; i < imax; i++)
203  {
204  if (coords[i].valid && coords[i].cost > minCost
205  && coords[i].label == label)
206  {
207  size_t id = MD.addObject();
208  MD.setValue(MDL_XCOOR, coords[i].X, id);
209  MD.setValue(MDL_YCOOR, coords[i].Y, id);
210  }
211  }
212  MD.write(fn_coords);
213 }
int Y
Y position.
Definition: micrograph.h:62
std::vector< Particle_coords > coords
Definition: micrograph.h:118
int X
X position.
Definition: micrograph.h:60
bool valid
Valid.
Definition: micrograph.h:64
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
#define i
virtual void setComment(const String &newComment="No comment")
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
X component (int)
int label
Label.
Definition: micrograph.h:58
double cost
Cost, scaled between 0 and 1.
Definition: micrograph.h:66
Y component (int)

Member Data Documentation

◆ coords

std::vector<Particle_coords> Micrograph::coords

Definition at line 118 of file micrograph.h.

◆ Ndim

size_t Micrograph::Ndim

Definition at line 115 of file micrograph.h.

◆ point1

Point Micrograph::point1

Definition at line 116 of file micrograph.h.

◆ point2

Point Micrograph::point2

Definition at line 117 of file micrograph.h.

◆ Xdim

size_t Micrograph::Xdim

Definition at line 112 of file micrograph.h.

◆ Ydim

size_t Micrograph::Ydim

Definition at line 113 of file micrograph.h.

◆ Zdim

size_t Micrograph::Zdim

Definition at line 114 of file micrograph.h.


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