37 int img_blobs(
Bimage* p,
float threshold,
float min_size,
float max_size,
float setvalue,
int sign)
39 if ( min_size > max_size ) swap(min_size, max_size);
41 if ( sign < 0 ) sign = -1;
42 else if ( sign > 0 ) sign = 1;
45 if ( threshold < p->average() ) sign = -1;
52 cout <<
"Finding blobs above " << threshold << endl;
54 cout <<
"Finding blobs below " << threshold << endl;
58 cout <<
"Size range to keep: " << min_size <<
" - " << max_size << endl;
59 cout <<
"Set value: " << setvalue << endl << endl;
63 int count = (int) pfeature->
maximum();
65 unsigned long i, f, nvox = 0, nblobs = 0;
68 int* feature_size =
new int[count+1];
70 for ( i=0; i<=count; i++ ) feature_size[i] = 0;
72 for ( i=0; i<datasize; i++ ) feature_size[(
long)(*pfeature)[i]]++;
75 cout <<
"Feature\tSize" << endl;
76 for ( i=0; i<=count; i++ ) {
77 cout << i <<
tab << feature_size[i];
78 if ( (i+1)%5 == 0 ) cout << endl;
83 if ( ( min_size > 0 ) || ( max_size < datasize ) ) {
84 for ( i=1; i<=count; i++ )
85 if ( feature_size[i] < min_size || feature_size[i] > max_size ) nblobs++;
87 for ( i=0; i<datasize; i++ ) {
88 f = (long)(*pfeature)[i];
90 if ( ( feature_size[f] < min_size ) || ( feature_size[f] > max_size ) ) {
99 cout <<
"Number of blobs deleted: " << nblobs << endl;
100 cout <<
"Number of voxels set: " << nvox << endl << endl;
103 delete[] feature_size;
131 if ( rho <= 0 ) rho =
RHO;
133 unsigned long img_start = 0, img_end = p->
images() - 1;
135 if ( img_num >= 0 ) img_start = img_end = img_num;
137 unsigned long i, j, n, nvox = 0;
140 double voxel_size = 1;
146 double volume = mol_weight/rho;
150 double min_vol = voxel_size;
154 cout <<
"Finding the threshold for image " << img_num+1 <<
":" << endl;
155 cout <<
"Protein density: " << rho <<
" Da/A3" << endl;
156 cout <<
"Molecular weight: " << mol_weight <<
" Da" << endl;
157 cout <<
"Molecular volume: " << volume <<
" A3" << endl;
158 cout <<
"Voxel size: " << voxel_size <<
" A3" << endl << endl;
162 cout <<
"Image\tThresh\tVolRes\tIntMass\tMW/mass" << endl;
163 for ( n=img_start; n<=img_end; n++ ) {
165 cout <<
" Threshold\tThreshStep\tVolume(A3)\tDifference\tMass" << endl;
171 while ( fabs(vol_diff) >= min_vol && fabs(thresh_step) > min_step ) {
174 if ( vol_diff > 0 ) {
175 if ( thresh_step < 0 ) thresh_step *= -0.5;
177 if ( thresh_step > 0 ) thresh_step *= -0.5;
179 threshold += thresh_step;
180 for ( i=n*imgsize, j=i+imgsize, nvox = 0, mass = 0; i<j; i++ ) {
181 if ( (*p)[i] >= threshold ) {
186 vol_diff = nvox*voxel_size - volume;
188 cout << threshold <<
tab << thresh_step <<
tab
189 << nvox*voxel_size <<
tab << vol_diff <<
tab << mass << endl;
190 dnvox = nvox - dnvox;
191 dmass = mass - dmass;
194 cout << n+1 <<
tab << threshold <<
tab <<
195 fabs(vol_diff)/volume <<
tab << mass <<
tab << mol_weight/mass << endl;
226 if ( rho <= 0 ) rho =
RHO;
228 unsigned long i, j, nvox;
231 for ( i=img_num*imgsize, j=i+imgsize, nvox=0; i<j; i++ )
232 if ( (*p)[i] >= threshold ) nvox++;
234 double voxel_size = 1;
239 double volume = nvox*voxel_size;
240 double mass = volume*rho;
243 cout <<
"Protein density: " << rho <<
" Da/A3" << endl;
244 cout <<
"Threshold: " << threshold << endl;
245 cout <<
"Volume: " << volume <<
" A3" << endl;
246 cout <<
"Molecular weight: " << mass <<
" Da" << endl << endl;
266 unsigned long i, j, jn, jz, jy, x, y, z, n;
267 unsigned long xx, yy, zz, xlo, xhi, ylo, yhi, zlo, zhi;
269 unsigned long iter, dvol, vol;
270 unsigned long slicesize = p->
sizeX()*p->
sizeY();
272 double voxel_size = p->
sampling().volume();
276 int nb, nbcut = 6, sign = 1;
277 if ( p->
sizeZ() < 2 ) nbcut = 2;
278 if ( threshold < p->average() ) sign = -1;
284 for ( n=0; n<p->
images(); n++ ) {
290 if ( min.
x() > 0 ) min[0]--;
291 if ( max.
x() < p->
sizeX() - 1 ) max[0]++;
292 if ( min.
y() > 0 ) min[1]--;
293 if ( max.
y() < p->
sizeY() - 1 ) max[1]++;
294 if ( min.
z() > 0 ) min[2]--;
295 if ( max.
z() < p->
sizeZ() - 1 ) max[2]++;
297 i = p->
index(0,x,y,z,n);
301 cout <<
"Image " << n+1 <<
":" << endl;
302 cout <<
"Origin: " << p->
image[n].
origin() << endl;
303 cout <<
"Iter\tVol\tdVol\tVol(A3)\tdVol(A3)" << endl;
309 for ( z=min.
z(); z<=max.
z(); z++ ) {
310 zlo = ( z > 0 )? z-1: z;
311 zhi = ( z < p->
sizeZ()-1 )? z+1: z;
312 for ( y=min.
y(); y<=max.
y(); y++ ) {
313 ylo = ( y > 0 )? y-1: y;
314 yhi = ( y < p->
sizeY()-1 )? y+1: y;
315 for ( x=min.
x(); x<=max.
x(); x++ ) {
316 xlo = ( x > 0 )? x-1: x;
317 xhi = ( x < p->
sizeX()-1 )? x+1: x;
319 i = p->
index(0,x,y,z,n);
321 if ( fom[i] < 1 && sign*(*p)[i] <= threshold ) {
322 jz = jn + zlo*slicesize;
323 for ( zz=zlo; zz<=zhi; zz++, jz += slicesize ) {
324 jy = jz + ylo*p->
sizeX();
325 for ( yy=ylo; yy<=yhi; yy++, jy += p->
sizeX() ) {
327 for ( xx=xlo; xx<=xhi; xx++, j++ ) {
328 if ( i != j && fom[j] > 1 ) nb++;
332 if ( ( nb >= vol ) || ( nb > nbcut ) ) {
342 for ( z=min2.
z(); z<=max2.
z(); z++ ) {
343 for ( y=min2.
y(); y<=max2.
y(); y++ ) {
344 for ( x=min2.
x(); x<=max2.
x(); x++ ) {
346 i = p->
index(0,x,y,z,n);
349 if ( min.
x() == x && x > 0 ) min[0]--;
350 if ( min.
y() == y && y > 0 ) min[1]--;
351 if ( min.
z() == z && z > 0 ) min[2]--;
352 if ( max.
x() == x && x < p->
sizeX() - 1 ) max[0]++;
353 if ( max.
y() == y && y < p->
sizeY() - 1 ) max[1]++;
354 if ( max.
z() == z && z < p->
sizeZ() - 1 ) max[2]++;
361 cout << iter <<
tab << vol <<
tab << dvol <<
tab
362 << vol*voxel_size <<
tab << dvol*voxel_size << endl;
363 if ( mask_out_freq > 0 && iter%mask_out_freq == 0 ) {
364 if ( p->
images() == 1 ) filename =
Bstring(iter,
"v%04ld.map");
365 else filename =
Bstring(n,
"v%02ld") +
Bstring(iter,
"_%04ld.map");
383 float threshold_last,
float threshold_step)
386 double threshold, a(0);
389 if ( threshold_step == 0 ) {
390 if ( threshold_last == threshold_first )
393 threshold_step = threshold_last - threshold_first;
395 if ( threshold_step > 0 && threshold_first > threshold_last )
396 threshold_step = -threshold_step;
397 if ( threshold_step < 0 && threshold_first < threshold_last )
398 threshold_step = -threshold_step;
401 if ( threshold_step < 0 ) sign = -1;
402 threshold_last += threshold_step/2;
404 cout <<
"Finding the number of regions at each threshold:" << endl;
405 cout <<
"Thresh\tRegions\tAvgSize" << endl;
406 for ( threshold = threshold_first; sign*threshold <= sign*threshold_last;
407 threshold+=threshold_step ) {
410 cout << threshold <<
tab << pfeat->
maximum() <<
tab << a << endl;
440 float threshold_lo,
float threshold_step,
int fill_borders)
443 error_show(
"No feature mask in img_flood_fill", __FILE__, __LINE__);
451 if ( threshold_hi < p->average() ) sign = -1;
452 if ( fabs(threshold_hi-p->
average()) < fabs(threshold_lo-p->
average()) )
453 swap(threshold_hi, threshold_lo);
455 if ( threshold_step == 0 )
456 threshold_step = (threshold_lo - threshold_hi)/100;
457 else if ( (threshold_lo - threshold_hi)/threshold_step < 0 )
458 threshold_step *= -1;
459 double abs_threshold = sign*threshold_hi;
460 double abs_threshold_lo = sign*threshold_lo;
463 cout <<
"Segmenting an image by flooding from a threshold or feature set:" << endl;
465 cout <<
"Feature set: " << pfeature->
file_name() << endl;
466 cout <<
"Starting threshold: " << threshold_hi << endl;
467 cout <<
"Finishing threshold: " << threshold_lo << endl;
468 cout <<
"Threshold step: " << threshold_step << endl << endl;
477 error_show(
"Error in img_flood_fill", __FILE__, __LINE__);
478 cerr <<
"The feature mask must have the same dimensions as the image!" << endl;
484 cerr <<
"Warning: The feature mask must of integer type!" << endl;
489 cout <<
"Regions: " << pfeature->
maximum() << endl;
497 for ( threshold=threshold_hi; abs_threshold>abs_threshold_lo; threshold+=threshold_step ) {
498 abs_threshold = sign*threshold;
500 cout <<
"Processing threshold: " << threshold <<
" (" << 100*(threshold - threshold_hi)/(threshold_lo - threshold_hi) <<
" %) \r" << flush;
503 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t it){
504 if ( sign*(*p)[it] > abs_threshold )
510 #pragma omp parallel for
511 for (
long it=0; it<datasize; it++ ) {
512 if ( sign*(*p)[it] > abs_threshold )
518 for ( i=0; i<datasize; i++ ) pfeature->
set(i, (*pmask)[i]);
524 unsigned long nborder = 0;
527 for ( i=0; i<datasize; i++ )
if ( (*pmask)[i] < 0 ) nborder++;
529 cout <<
"Border voxels: " << nborder << endl;
532 if ( fill_borders ) {
534 cout <<
"Assigning borders:" << endl;
536 while ( maxtries-- > 0 && nborder > 0 ) {
540 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t it){
541 if ( (*pmask)[it] < 0 ) {
543 pmask->
set(it, (*pmask)[j]);
544 if ( (*pmask)[it] < 0 ) {
546 pmask->
set(it, (*pmask)[j]);
551 #pragma omp parallel for
552 for (
long it=0; it<datasize; it++ ) {
553 if ( (*pmask)[it] < 0 ) {
555 pmask->
set(it, (*pmask)[j]);
556 if ( (*pmask)[it] < 0 ) {
558 pmask->
set(it, (*pmask)[j]);
563 for (
long it=0; it<datasize; it++ )
if ( (*pmask)[it] < 0 ) nborder++;
568 cerr <<
"Warning: " << nborder <<
" border voxels were not assigned!" << endl << endl;
571 unsigned long nseg = (
unsigned long)(pfeature->
maximum() + 1.9);
576 for ( i=0; i<nseg; i++ ) vol[i] = 0;
577 for ( i=0; i<datasize; i++ )
578 if ( (*pmask)[i] > -1 ) vol[(
long)(*pmask)[i]]++;
579 cout <<
"Region\tVoxels\tVolume\tMass" << endl;
580 for ( i=1; i<nseg; i++ )
581 cout << i <<
tab << vol[i] <<
tab << vol[i]*p->
sampling().volume()
590 cout <<
"DEBUG img_flood_fill: Finished with segmentation" << endl;
614 if ( kernel_size < 3 ) kernel_size = 3;
618 unsigned long i, j, imax, x, y, z, n, xx, yy, zz, dn;
619 unsigned long hk = kernel_size/2, xlo, xhi, ylo, yhi, zlo, zhi, npeak = 0;
624 cout <<
"Segmenting an image by flooding from peaks:" << endl;
625 cout <<
"Kernel size: " << 2*hk+1 << endl;
626 cout <<
"Threshold: " << threshold << endl;
633 int* pnt =
new int[datasize];
635 for ( i=n=0; n<p->
images(); n++ ) {
636 for ( z=0; z<p->
sizeZ(); z++ ) {
637 zlo = (z>hk)? z-hk: 0;
639 for ( y=0; y<p->
sizeY(); y++ ) {
640 ylo = (y>hk)? y-hk: 0;
642 for ( x=0; x<p->
sizeX(); x++, i++ ) {
643 xlo = (x>hk)? x-hk: 0;
646 if ( (*p)[i] >= threshold ) {
649 for ( zz=zlo; zz<=zhi; zz++ ) {
650 for ( yy=ylo; yy<=yhi; yy++ ) {
651 for ( xx=xlo; xx<=xhi; xx++ ) {
653 j = p->
index(0,xx,yy,zz,n);
654 if ( dmax < (*p)[j] ) {
662 if ( pnt[i] == i ) pmask->
set(i, ++npeak);
673 for ( i=0; i<datasize; i ++ ) if ( (*p)[i] >= threshold && (*pmask)[i] < 1 ) {
674 pmask->
set(i, (*pmask)[pnt[i]]);
676 for ( i=n=0; i<datasize; i ++ ) if ( (*pmask)[i] > 0 ) n++;
685 cout <<
"Number of peaks: " << npeak << endl << endl;
707 cerr <<
"Warning: The mask must of integer type!" << endl;
711 unsigned long i, mval = (
unsigned long) (pmask->
maximum() + 1.5);
714 for ( i=0; i<datasize; i++ )
715 if ( (*pmask)[i] < 1 && (*p)[i] >= threshold ) pmask->
set(i, mval);
738 if ( (*p)[index] != 0 )
return (
int)(*p)[index];
740 unsigned long x, y, z, n;
745 long zlo, zhi, ylo, yhi, xlo, xhi;
750 if ( zlo < 0 ) zlo = 0;
752 if ( zhi >= p->
sizeZ() ) zhi = p->
sizeZ() - 1;
754 if ( ylo < 0 ) ylo = 0;
756 if ( yhi >= p->
sizeY() ) yhi = p->
sizeY() - 1;
758 if ( xlo < 0 ) xlo = 0;
760 if ( xhi >= p->
sizeX() ) xhi = p->
sizeX() - 1;
763 for ( z=zlo; z<=zhi; z++ ) {
764 for ( y=ylo; y<=yhi; y++ ) {
765 for ( x=xlo; x<=xhi; x++ ) {
767 i = p->
index(0,x,y,z,n);
772 else if ( nv != (*p)[i] )
781 cout <<
"DEBUG img_check_neighbors: Ending with feature " << (int)(*p)[index] << endl;
798 unsigned long x, y, z, n;
801 unsigned long i, ihi(index);
802 long zlo, zhi, ylo, yhi, xlo, xhi;
803 double max_val(-1e30);
807 if ( zlo < 0 ) zlo = 0;
809 if ( zhi >= p->
sizeZ() ) zhi = p->
sizeZ() - 1;
811 if ( ylo < 0 ) ylo = 0;
813 if ( yhi >= p->
sizeY() ) yhi = p->
sizeY() - 1;
815 if ( xlo < 0 ) xlo = 0;
817 if ( xhi >= p->
sizeX() ) xhi = p->
sizeX() - 1;
820 for ( z=zlo; z<=zhi; z++ ) {
821 for ( y=ylo; y<=yhi; y++ ) {
822 for ( x=xlo; x<=xhi; x++ ) {
823 i = p->
index(0,x,y,z,n);
824 if ( max_val < (*p)[i] ) {
852 unsigned long nmark =
count_list((
char *) markers);
854 if ( nmark < 1 )
return NULL;
858 unsigned long i, j, k, ivoxel, n;
859 long* index =
new long[nmark];
863 cout <<
"Excising 1 feature:" << endl;
865 cout <<
"Excising " << nmark <<
" features:" << endl;
869 cout <<
"x\ty\tz\tn\tIndex\tValue" << endl;
873 for ( j=0, mark=markers; mark; mark=mark->
next, j++ ) {
876 index[j] = (long)(*pfeature)[ivoxel];
880 index[j] <<
tab << (*p)[ivoxel] << endl;
884 unsigned long max_ind(0), nvox(0);
887 double voxel_size = 1;
895 for ( i=n=0; i<p->
images(); n++ ) {
896 for ( k=0; k<imgsize; k++, i++ ) {
897 if ( (*pfeature)[i] > 0 ) {
898 for ( j=0; j<nmark && (*pfeature)[i] != index[j]; j++ ) ;
899 if ( j < nmark && (*pfeature)[i] == index[j] ) {
900 pex->
set(i, (*p)[i]);
901 if ( max < (*p)[i] ) {
915 unsigned long x, y, z;
919 cout <<
"Voxels: " << nvox << endl;
920 cout <<
"Maximum: " << max << endl;
921 cout <<
"Maximum voxel: " << x <<
" "
922 << y <<
" " << z <<
" " << n << endl;
923 cout <<
"Volume: " << nvox*voxel_size <<
" A3" << endl;
924 cout <<
"Molecular weight: " << nvox*voxel_size*
RHO <<
" Da" << endl << endl;
946 unsigned long i, j, k, c;
954 cout <<
"Excising %g features" << pfeature->
maximum() << endl << endl;
956 for ( i=0; i<imagesize; i++ ) if ( (*pfeature)[i] > 0 ) {
957 j = (((
unsigned long)(*pfeature)[i] - 1)*imagesize + i)*pex->
channels();
void change_type(char letter)
Get the data type indicated by a single letter code.
Bimage * img_flood_fill(Bimage *p, Bimage *pfeature, float threshold_hi, float threshold_lo, float threshold_step, int fill_borders)
Creates and expands a feature map from a starting to ending threshold value.
Bimage * img_flood_peaks(Bimage *p, int kernel_size, float threshold)
Generates a segmented image from peaks above a threshold value.
int img_set_second_mask(Bimage *p, Bimage *pmask, float threshold)
A second level mask is added to a primary mask based on a threshold.
Bimage * img_find_features(Bimage *p, float threshold, int sign, int wrap)
Segments an image into contiguous regions (features).
Header file for general utilities.
CompoundType compound_type()
Header file for image segmenting functions.
long count_list(char *list)
Counts the number of items in a linked list.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Bimage * img_excise_feature(Bimage *p, Bimage *pfeature, Bmarker *markers)
Finds the density around a voxel associated with a particular feature.
size_t index(size_t nx, size_t ny)
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
size_t statistics()
Calculates the statistics for an image.
double img_average_feature_size(Bimage *p)
Calculates the average size of regions (features) in a level mask.
int img_internal_volume(Bimage *p, float threshold, int mask_out_freq)
Finds the density threshold associated with a particular molecular weight.
General image parameter class.
unsigned char * data_alloc()
Header file for generalized linked list functions.
float * FOM_initialize()
Allocates and initializes the FOM block.
void coordinates(size_t i, size_t &nx, size_t &ny, size_t &nz)
Header file for combining two images in various ways.
float img_mass_at_threshold(Bimage *p, int img_num, float threshold, float rho)
Calculates the mass from the density threshold.
float img_find_threshold(Bimage *p, int img_num, double mol_weight, float rho)
Finds the density threshold associated with a particular molecular weight.
void file_name(Bstring s)
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
double standard_deviation()
int write_fom_as_image(Bstring &filename, Bimage *p)
Converts a FOM block to an image for output.
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Bimage * img_excise_all_features(Bimage *p, Bimage *pfeature)
Extracts all the features associated with a multi-level mask.
Header file for feature extraction tools.
int img_blobs(Bimage *p, float threshold, float min_size, float max_size, float setvalue, int sign)
Identifies contiguous regions above a given threshold (blobs) and eliminate those less than a minimum...
int img_segment_threshold_series(Bimage *p, float threshold_first, float threshold_last, float threshold_step)
Segments a map through a series of thresholds and reports results.
bool check_if_same_size(Bimage *p)
Check if two images are the same size.
Header file for 2D and 3D image I/O.
int img_check_neighbors(Bimage *p, unsigned long index)
Check neighbors in feature map and assign a value if a neighbor is assigned.
ostream & tab(ostream &out)
long img_find_highest_in_kernel(Bimage *p, unsigned long index, int size)
Finds the highest value in a kernel excluding the central voxel.