Xmipp  v3.23.11-Nereus
Functions
volume_segment.cpp File Reference
#include <core/args.h>
#include <data/morphology.h>
#include <data/filters.h>
#include "volume_segment.h"
Include dependency graph for volume_segment.cpp:

Go to the source code of this file.

Functions

double segment_threshold (const Image< double > *V_in, Image< double > *V_out, double threshold, bool do_prob)
 
void wang_smoothing (const Image< double > *V_in, Image< double > *V_out, int radius)
 
void probabilistic_solvent (Image< double > *V_in, Image< double > *V_out)
 

Function Documentation

◆ probabilistic_solvent()

void probabilistic_solvent ( Image< double > *  V_in,
Image< double > *  V_out 
)

Definition at line 233 of file volume_segment.cpp.

234 {
235  // Calculate mean and sigma for protein and solvent regions
236  // according to the traditional segmentation
237  double Np, sump, sum2p, Ns, sums, sum2s;
238  double avgp, sigp, avgs, sigs, aux, solv_frac, prot_frac;
239  double p_prot, p_solv;
240 
241  MultidimArray<double> &mVin=(*V_in)();
242  MultidimArray<double> &mVout=(*V_out)();
243  mVin.setXmippOrigin();
244  mVout.setXmippOrigin();
245 
246  Np = sump = sum2p = Ns = sums = sum2s = 0.;
248  {
249  aux = A3D_ELEM(mVin, k, i, j);
250  if (A3D_ELEM(mVout, k, i, j) < 0.5)
251  {
252  sums += aux;
253  sum2s += aux * aux;
254  Ns += 1.;
255  }
256  else
257  {
258  sump += aux;
259  sum2p += aux * aux;
260  Np += 1.;
261  }
262  }
263  if (Np > 0. && Ns > 0.)
264  {
265  avgs = sums / Ns;
266  sigs = sum2s / Ns - avgs * avgs;
267  avgp = sump / Np;
268  sigp = sum2p / Np - avgp * avgp;
269  prot_frac = Np / (Np + Ns);
270  solv_frac = 1. - prot_frac;
271  }
272  else
273  {
274  REPORT_ERROR(ERR_NUMERICAL, "empty solvent or protein region");
275  }
276 
277  // Terwilliger-like calculation of P(x|solv) & P(x|prot)
278  // Bayes: P(prot|x)= P(x|prot)/{P(x|prot)+P(x|solv)}
279  double isigs=-1/(2.0*sigs);
280  double isigp=-1/(2.0*sigp);
282  {
283  double voxelVal=A3D_ELEM(mVin, k, i, j);
284  aux = voxelVal - avgs;
285  p_solv = solv_frac * exp(aux * aux * isigs);
286  aux = voxelVal - avgp;
287  p_prot = prot_frac * exp(aux * aux * isigp);
288  A3D_ELEM(mVout, k, i, j) = p_prot / (p_prot + p_solv);
289  }
290 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#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 A3D_ELEM(V, k, i, j)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
Error related to numerical calculation.
Definition: xmipp_error.h:179
#define j

◆ segment_threshold()

double segment_threshold ( const Image< double > *  V_in,
Image< double > *  V_out,
double  threshold,
bool  do_prob 
)

Definition at line 128 of file volume_segment.cpp.

130 {
131  Image<double> aux;
132 
133  // Binarize input volume
134  (*V_out)() = (*V_in)();
135  (*V_out)().threshold("below", threshold, threshold);
136  (*V_out)().binarize(threshold);
137 
138 #ifdef DEBUG
139 
140  std::cout << threshold << std::endl;
141  Image<double> save;
142  save() = (*V_in)();
143  save.write("PPP0.vol");
144  save() = (*V_out)();
145  save.write("PPP1.vol");
146 #endif
147 
148  if (!do_prob)
149  {
150  // Apply morphological opening to input volume
151  aux().initZeros((*V_out)());
152  opening3D((*V_out)(), aux(), 18, 0, 1);
153  closing3D(aux(), (*V_out)(), 18, 0, 1);
154 #ifdef DEBUG
155 
156  save() = (*V_out)();
157  save.write("PPP2.vol");
158 #endif
159  }
160 
161  // Count the number of different objects
162  int no_comp = labelImage3D((*V_out)(), aux());
163  Matrix1D<double> count(no_comp + 1);
164  const MultidimArray<double> &maux=aux();
166  VEC_ELEM(count,(int)A3D_ELEM(maux,k, i, j))++;
167 
168 #ifdef DEBUG
169 
170  std::cout << count << std::endl << std::endl;
171  std::cout << "Press any key\n";
172  char c;
173  std::cin >> c;
174 #endif
175 
176  // Pick the maximum
177  count(0) = 0; // We don't want to pick the background
178  int imax=count.maxIndex();
179 
180  // Select the mask with only that piece
181  const MultidimArray<double> &mVout=(*V_out)();
183  A3D_ELEM(mVout,k, i, j) = A3D_ELEM(maux,k, i, j) == imax;
184 
185  return count(imax);
186 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
doublereal * c
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 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
void threshold(double *phi, unsigned long nvox, double limit)
Definition: lib_vwk.cpp:524
#define A3D_ELEM(V, k, i, j)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
void opening3D(const MultidimArray< double > &in, MultidimArray< double > &out, int neig, int count, int size)
Definition: morphology.cpp:440
void closing3D(const MultidimArray< double > &in, MultidimArray< double > &out, int neig, int count, int size)
Definition: morphology.cpp:422
#define j
int labelImage3D(const MultidimArray< double > &V, MultidimArray< double > &label)
Definition: filters.cpp:669

◆ wang_smoothing()

void wang_smoothing ( const Image< double > *  V_in,
Image< double > *  V_out,
int  radius 
)

Definition at line 188 of file volume_segment.cpp.

189 {
190 
191  int r2;
192  double radius2 = radius * radius;
193  double sumw, weight;
194 
195  (*V_out)().initZeros((*V_in)());
196 
197  const MultidimArray<double> &mVin=(*V_in)();
198  const MultidimArray<double> &mVout=(*V_out)();
200  {
201  sumw = 0.;
202  for (int kp = k - radius; kp < k + radius; kp++)
203  {
204  if (kp > STARTINGZ(mVin) && kp < FINISHINGZ(mVin))
205  {
206  for (int ip = i - radius; ip < i + radius; ip++)
207  {
208  if (ip > STARTINGY(mVin) && ip < FINISHINGY(mVin))
209  {
210  for (int jp = j - radius; jp < j + radius; jp++)
211  {
212  if (jp > STARTINGX(mVin) && jp < FINISHINGX(mVin))
213  {
214  r2 = (kp - k) * (kp - k) + (ip - i) * (ip - i) + (jp - j) * (jp - j);
215  if ((r2 < radius2) && (A3D_ELEM(mVin, kp, ip, jp) > 0.))
216  {
217  weight = 1. - sqrt(r2 / radius2);
218  A3D_ELEM(mVout, k, i, j) += weight * XMIPP_MAX(0., A3D_ELEM(mVin, kp, ip, jp));
219  sumw += weight;
220  }
221  }
222  }
223  }
224  }
225  }
226  }
227  if (sumw > 0.)
228  A3D_ELEM(mVout, k, i, j) /= sumw;
229  }
230 }
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define FINISHINGX(v)
void sqrt(Image< double > &op)
#define FINISHINGZ(v)
#define STARTINGX(v)
#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 STARTINGY(v)
#define A3D_ELEM(V, k, i, j)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
#define j
#define FINISHINGY(v)
float r2
#define STARTINGZ(v)
double radius
Spatial radius in Universal System units.
Definition: blobs.h:115