68 cerr <<
"Error: Size of subimage is less than one!" << endl;
77 unsigned long i, j, iy, iz, jy, jz;
78 unsigned long x, y, z, n, c, new_nimg;
79 long oldn, oldx, oldy, oldz;
81 if ( img_number < 0 ) {
86 if ( img_number >= p->
images() ) img_number = p->
images() - 1;
98 cout <<
"Extracting subimage: " << img_number << endl;
100 cout <<
"Extracting subimages: 0 - " << pex->
images() - 1 << endl;
101 cout <<
"Size: " << pex->
size() << endl;
102 cout <<
"Start: " << start << endl;
108 for ( n=0; n<new_nimg; n++ ) {
109 oldn = n + img_number;
112 for ( z=0; z<pex->
sizeZ(); z++ ) {
114 if ( oldz >= 0 && oldz < p->sizeZ() ) {
117 for ( y=0; y<pex->
sizeY(); y++ ) {
119 if ( oldy >= 0 && oldy < p->sizeY() ) {
120 iy = (iz + y)*pex->
sizeX();
121 jy = (jz + oldy)*p->
sizeX();
122 for ( x=0; x<pex->
sizeX(); x++ ) {
124 if ( oldx >= 0 && oldx < p->sizeX() ) {
126 pex->
set(i, (*p)[j]);
159 error_show(
"img_extract_bitmap", __FILE__, __LINE__);
160 cerr <<
"Error: Cannot extract a bitmap from a non-bitmap image!" << endl;
164 if ( size.
volume() < 1 ) {
165 error_show(
"img_extract_bitmap", __FILE__, __LINE__);
166 cerr <<
"Error: Size of subimage is less than one!" << endl;
170 unsigned long i, j, y, z, n, new_nimg;
171 long oldn, oldx, oldy, oldz;
173 if ( img_number < 0 ) {
178 if ( img_number >= p->
images() ) img_number = p->
images() - 1;
187 cout <<
"Extracting a bitmap subimage:" << endl;
189 cout <<
"Image number: " << img_number << endl;
191 cout <<
"Image numbers: 0 - " << pex->
images() - 1 << endl;
192 cout <<
"Size: " << pex->
size() << endl;
193 cout <<
"Start: " << start << endl;
196 cout <<
"Extracting a bitmap subimage" << endl << endl;
198 unsigned long linesize = pex->
page_size().
x()/8;
202 for ( n=0; n<new_nimg; n++ ) {
204 oldn = n + img_number;
205 for ( z=0; z<pex->
sizeZ(); z++ ) {
207 for ( y=0; y<pex->
sizeY(); y++ ) {
210 i = ((n*pex->
sizeZ() + z)*pex->
sizeY() + y)*linesize;
211 j = (((oldn*p->
sizeZ() + oldz)*p->
sizeY() + oldy)*p->
sizeX() + oldx)/8;
212 memcpy(nudata+i, data+j, linesize);
242 unsigned long x, y, z, n, c, new_nimg;
243 double rx2, ry2, rz2, r2;
245 if ( minrad > maxrad ) swap(minrad, maxrad);
246 if ( minrad < 0 ) minrad = 0;
247 if ( maxrad > p->
sizeX() ) maxrad = p->
sizeX() - 1;
249 double minrad2 = minrad*minrad;
250 double maxrad2 = maxrad*maxrad;
252 if ( img_number < 0 ) {
257 if ( img_number >= p->
images() ) img_number = p->
images() - 1;
265 cout <<
"Extracting subimage: " << img_number << endl;
267 cout <<
"Extracting subimages: 0 - " << pex->
images() - 1 << endl;
268 cout <<
"Shell: " << minrad <<
" " << maxrad << endl;
274 for ( n=0; n<new_nimg; n++ ) {
275 for ( z=0; z<pex->
sizeZ(); z++ ) {
276 if ( p->
sizeZ() > 1 ) {
280 for ( y=0; y<pex->
sizeY(); y++ ) {
283 for ( x=0; x<pex->
sizeX(); x++ ) {
286 r2 = rx2 + ry2 + rz2;
287 if ( r2 >= minrad2 && r2 <= maxrad2 ) {
289 for ( c=0; c<pex->
channels(); c++, i++ ) pex->
set(i, (*p)[i]);
303 unsigned long i, j, x, y, z, c;
304 long oldx, oldy, oldz;
306 for ( z=0; z<pex->
sizeZ(); z++ ) {
307 oldz = z + coords.
z();
308 for ( y=0; y<pex->
sizeY(); y++ ) {
309 oldy = y + coords.
y();
310 for ( x=0; x<pex->
sizeX(); x++ ) {
311 oldx = x + coords.
x();
313 i = pex->
index(0, x, y, z, n);
314 j = p->
index(0, oldx, oldy, oldz, img_number);
315 for ( c=0; c<pex->
channels(); c++, i++, j++ )
316 pex->
set(i, (*p)[j]);
352 if ( tile_size.
length() < 1 ) {
353 error_show(
"img_extract_tiles", __FILE__, __LINE__);
354 cerr <<
"Error: Size of subimage is less than one!" << endl;
358 if ( img_number < 0 ) img_number = 0;
359 if ( img_number >= p->
images() ) img_number = p->
images() - 1;
361 unsigned long x, y, z, new_nimg(0);
364 if ( size.
length() < 2 ) {
365 size[0] = p->
sizeX();
366 size[1] = p->
sizeY();
367 size[2] = p->
sizeZ();
370 if ( start.
x() > p->
sizeX() ) start[0] = 0;
371 if ( start.
y() > p->
sizeY() ) start[1] = 0;
372 if ( start.
z() > p->
sizeZ() ) start[2] = 0;
374 if ( size.
x() > p->
sizeX() - start.
x() ) size[0] = p->
sizeX() - start.
x();
375 if ( size.
y() > p->
sizeY() - start.
y() ) size[1] = p->
sizeY() - start.
y();
376 if ( size.
z() > p->
sizeZ() - start.
z() ) size[2] = p->
sizeZ() - start.
z();
378 tile_size = tile_size.
min(size);
380 if ( !exceed ) size -= tile_size - 1;
385 for ( z=0; z<size.
z(); z+=tile_size.
z() )
386 for ( y=0; y<size.
y(); y+=tile_size.
y() )
387 for ( x=0; x<size.
x(); x+=tile_size.
x() )
392 pex->
size(tile_size);
398 cout <<
"Extracting " << pex->
images() <<
" tiles:" << endl;
399 cout <<
"Start: " << start << endl;
400 cout <<
"Tile size: " << pex->
size() << endl;
403 cout <<
"Extracting tiles" << endl << endl;
407 for ( n=z=0; z<size.
z(); z+=pex->
sizeZ() )
408 for ( y=0; y<size.
y(); y+=pex->
sizeY() )
409 for ( x=0; x<size.
x(); x+=pex->
sizeX(), n++ )
413 dispatch_apply(pex->
images(), dispatch_get_global_queue(0, 0), ^(
size_t nn){
417 #pragma omp parallel for
418 for ( n=0; n<pex->
images(); n++ )
450 if ( tile_size.
length() < 1 ) {
451 error_show(
"img_extract_tiles_at_coords", __FILE__, __LINE__);
452 cerr <<
"Error: Size of subimage is less than one!" << endl;
456 if ( tile_size.
x() > p->
sizeX() ) tile_size[0] = p->
sizeX();
457 if ( tile_size.
y() > p->
sizeY() ) tile_size[1] = p->
sizeY();
458 if ( tile_size.
z() > p->
sizeZ() ) tile_size[2] = p->
sizeZ();
460 if ( img_number < 0 ) img_number = 0;
464 pex->
size(tile_size);
470 cout <<
"Extracting " << pex->
images() <<
" tiles using coordinates:" << endl;
471 cout <<
"Tile size: " << pex->
size() << endl;
474 cout <<
"Extracting tiles using coordinates" << endl << endl;
479 dispatch_apply(pex->
images(), dispatch_get_global_queue(0, 0), ^(
size_t n){
483 #pragma omp parallel for
484 for (
long n=0; n<pex->
images(); n++ )
510 if ( voxel.
x() < 0 || voxel.
x() >= p->
sizeX() || voxel.
y() < 0 ||
511 voxel.
y() >= p->
sizeY() || voxel.
z() < 0 || voxel.
z() >= p->
sizeZ() )
return NULL;
516 unsigned long i, j, iy, x, y, z, c;
518 unsigned long h, nh = 1;
519 if ( p->
sizeZ() > 1 ) nh = 3;
520 unsigned long nx = (
unsigned long) (size.
x());
521 unsigned long ny = (
unsigned long) (size.
y());
528 cout <<
"Extracting orthogonal views:" << endl;
529 cout <<
"Start: " << start << endl;
530 cout <<
"End: " << end << endl;
534 cout <<
"Extracting orthogonal views" << endl << endl;
540 for ( h=0; h<nh; h++ ) {
542 if ( h%2 == 0 ) origin[0] = p->
image->
origin().x() - start.
x();
544 if ( h < 2 ) origin[1] = p->
image->
origin().y() - start.
y();
547 if ( h == 0 ) porth->
image[h].
view(0,0,1,0);
548 else if ( h == 1 ) porth->
image[h].
view(1,0,0,0);
553 for ( y=0; y<ny; y++ ) {
556 yo = (long) (y + start.
y());
557 if ( yo < 0 ) yf = 0;
558 else if ( yo >= p->
sizeY() ) yf = 0;
561 zo = (long) (z + start.
z());
562 if ( zo < 0 ) yf = 0;
563 else if ( zo >= p->
sizeZ() ) yf = 0;
566 for ( x=0; x<nx; x++ ) {
569 xo = (long) (x + start.
x());
570 if ( xo < 0 ) xf = 0;
571 else if ( xo >= p->
sizeX() ) xf = 0;
574 zo = (long) (z + start.
z());
575 if ( zo < 0 ) xf = 0;
576 else if ( zo >= p->
sizeZ() ) xf = 0;
581 for ( c=0; c<porth->
channels(); c++, i++, j++ ) porth->
set(i, (*p)[j]);
600 if ( scale >= 1 ) aflag = 0;
605 unsigned long ic, j, jz, x, y, c, m;
608 double iscale = 1.0/scale, shift = (scale < 1)? (iscale - 1)/2: 0;
610 double* value =
new double[p->
channels()];
613 unsigned long datasize = pshow->
sizeX()*pshow->
sizeY();
618 x = i%pshow->
sizeX();
619 y = i/pshow->
sizeX();
623 if ( m > datasize ) m = datasize;
624 for ( ; i<m; i++, x++ ) {
625 if ( x >= pshow->
sizeX() ) {
630 yf = (pshow->
sizeY() - y - 1)*iscale;
631 if ( xf >= 0 || xf <= p->sizeX()-1 || yf >=0 || yf <= p->
sizeY()-1 ) {
632 yo = (long) (yf + shift);
633 xo = (long) (xf + shift);
636 pshow->
set(i, 255 * (*p)[j]);
637 if ( dscale < 0 ) pshow->
set(i, 255 - (*pshow)[i]);
641 for ( c=0; c<p->
channels(); c++ ) value[c] = p->
average(c, xf, yf, z, n, iscale);
643 for ( c=0; c<p->
channels(); c++, j++ ) value[c] = (*p)[j];
645 dispval[0] = value[0]*value[0] + value[1]*value[1];
650 for ( c=0; c<3; c++ ) dispval[c] = 255*rgb[c];
652 for ( c=0; c<pshow->
channels(); c++ ) dispval[c] = value[c];
655 for ( c=0; c<pshow->
channels(); c++ ) {
660 for ( c=0; c<pshow->
channels(); c++ ) {
661 dispval[c] = floor(dscale*(dispval[c]-p->
show_minimum())+0.5);
662 if ( dispval[c] > 255 ) dispval[c] = 255;
663 else if ( dispval[c] < 0 ) dispval[c] = 0;
666 for ( c=0, ic = i*pshow->
channels(); c<pshow->
channels(); c++, ic++ ) pshow->
set(ic, dispval[c]);
715 unsigned long datasize = pshow->
sizeX()*pshow->
sizeY();
719 dispatch_apply((datasize - 1)/chunk_size + 1, dispatch_get_global_queue(0, 0), ^(
size_t i){
723 #pragma omp parallel for
724 for (
long i=0; i<datasize; i+=chunk_size )
757 if ( center.
x() < 0 || center.
x() >= p->
sizeX() || center.
y() < 0 ||
758 center.
y() >= p->
sizeY() || center.
z() < 0 || center.
z() >= p->
sizeZ() )
return NULL;
760 int threshold = 0, xf, yf;
763 unsigned long i, j, iy, jz, x, y, z, c;
765 unsigned long h, nh = 1;
766 if ( p->
sizeZ() > 1 ) nh = 3;
767 unsigned long nx = (
unsigned long) (scale*size.
x());
768 unsigned long ny = (
unsigned long) (scale*size.
y());
769 double iscalex = 1.0/scale;
770 double iscaley = 1.0/scale;
771 double iscalez = 1.0/scale;
779 cout <<
"DEBUG img_extract_magnifying: start=" << start << endl;
780 cout <<
"DEBUG img_extract_magnifying: end=" << end << endl;
781 cout <<
"DEBUG img_extract_magnifying: size: nx=" << nx <<
" ny=" << ny <<
" c=" << p->
channels() << endl;
800 double* value =
new double[p->
channels()];
806 for ( h=0; h<nh; h++ ) {
810 for ( y=0; y<ny; y++ ) {
813 yo = (long) (y*iscaley + start.
y());
814 if ( yo < 0 ) yf = 0;
815 else if ( yo >= p->
sizeY() ) yf = 0;
818 zo = (long) (z*iscalez + start.
z());
819 if ( zo < 0 ) yf = 0;
820 else if ( zo >= p->
sizeZ() ) yf = 0;
822 iy = (h*ny + ny - 1 - y)*nx;
823 for ( x=0; x<nx; x++ ) {
826 xo = (long) (x*iscalex + start.
x());
827 if ( xo < 0 ) xf = 0;
828 else if ( xo >= p->
sizeX() ) xf = 0;
831 zo = (long) (z*iscalez + start.
z());
832 if ( zo < 0 ) xf = 0;
833 else if ( zo >= p->
sizeZ() ) xf = 0;
840 pmag->
set(i, 255 * (*p)[j]);
841 if ( dscale < 0 ) pmag->
set(i, 255 - (*pmag)[i]);
844 for ( c=0; c<p->
channels(); c++, j++ ) value[c] = (*p)[j];
846 dispval[0] = value[0]*value[0] + value[1]*value[1];
851 for ( c=0; c<3; c++ ) dispval[c] = 255*rgb[c];
853 for ( c=0; c<pmag->
channels(); c++ ) dispval[c] = value[c];
856 for ( c=0; c<pmag->
channels(); c++ ) {
861 for ( c=0; c<pmag->
channels(); c++ ) {
862 dispval[c] = floor(dscale*(dispval[c]-p->
show_minimum())+0.5);
863 if ( dispval[c] > 255 ) dispval[c] = 255;
864 else if ( dispval[c] < 0 ) dispval[c] = 0;
867 for ( c=0; c<pmag->
channels(); c++, i++ ) pmag->
set(i, dispval[c]);
890 int ncoord,
int nbad,
float* coords)
893 if ( ncoord < 1 )
return NULL;
895 unsigned long i, j, n, x, y, z, c;
896 long oldx, oldy, oldz, oldn;
900 if ( nbad && bad_radius > 1 ) {
902 for ( i=ncoord; i<ncoord+nbad; i++ ) {
903 center[0] = coords[4*i];
904 center[1] = coords[4*i+1];
905 center[2] = coords[4*i+2];
910 Vector3<int> box((
int) (2.0*radius), (
int) (2.0*radius), (
int) (2.0*radius));
918 float* fom = part->FOM_initialize();
920 for ( n=0; n<part->images(); n++ ) {
921 oldn = (int) coords[4*n+3];
922 part->image[n].origin(boxrad);
923 for ( z=0; z<part->sizeZ(); z++ ) {
924 oldz = z - boxrad.
z() + (int) coords[4*n+2];
925 for ( y=0; y<part->sizeY(); y++ ) {
926 oldy = y - boxrad.y() + (int) coords[4*n+1];
927 for ( x=0; x<part->sizeX(); x++ ) {
928 oldx = x - boxrad.x() + (int) coords[4*n];
929 if ( oldx >= 0 && oldx < p->sizeX() && oldy >= 0 && oldy < p->
sizeY() && oldz >= 0 && oldz < p->
sizeZ()) {
930 i = ((n*part->sizeZ() + z)*part->sizeY() + y)*part->sizeX() + x;
932 if ( !pmask || (pmask && (*pmask)[j] < 1) ) {
936 for ( c=0; c<p->
channels(); c++, i++, j++ ) part->set(i, (*p)[j]);
946 part->correct_background();
970 if ( img_num < 0 )
return NULL;
971 if ( nspline < 1 )
return NULL;
973 if ( axis < 1 || axis > 3 ) {
974 if ( p->
sizeZ() > 1 ) axis = 3;
979 size[axis-1] = nspline;
980 if ( p->
sizeX() < width ) size[0] = p->
sizeX();
981 if ( p->
sizeY() < width ) size[1] = p->
sizeY();
982 if ( p->
sizeZ() < width ) size[2] = p->
sizeZ();
988 unsigned long i, x, y, z, s;
989 double w, halfwidth(width/2);
996 cout <<
"DEBUG img_extract_one_filament: img_num=" << img_num <<
" width=" << width <<
" nspline=" << nspline << endl;
997 cout <<
"DEBUG img_extract_one_filament: axis=" << axis <<
" size=" << size << endl;
1000 for ( i=z=0; z<size.
z(); z++ ) {
1001 if ( axis == 3 ) s = z;
1002 else if ( pfil->
sizeZ() > 1 ) vf[2] = z - halfwidth;
1003 for ( y=0; y<size.
y(); y++ ) {
1004 if ( axis == 2 ) s = y;
1005 else if ( pfil->
sizeY() > 1 ) vf[1] = y - halfwidth;
1006 for ( x=0; x<size.
x(); x++, i++ ) {
1007 if ( axis == 1 ) s = x;
1008 else if ( pfil->
sizeX() > 1 ) vf[0] = x - halfwidth;
1009 if ( s > 0 ) v = spline[s] - spline[s-1];
1010 else v = spline[1] - spline[0];
void view(double x, double y, double z, double a)
Header file for spline interpolation.
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
CompoundType compound_type()
int img_delete_within_radius(Bimage *p, Vector3< float > center, double radius, double width, double fill, int fill_type)
Fills a sphere within an image with a uniform value.
bool within_boundaries(Vector3< double > loc)
Class for complex numbers.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
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.
double interpolate(size_t cc, double xx, double yy, double zz, size_t nn, double fill) const
Interpolates using a given location.
size_t statistics()
Calculates the statistics for an image.
3x3 Matrix manipulation functions
General image parameter class.
unsigned char * data_alloc()
CompoundType
Compound data type specifier.
void origin(double x, double y, double z)
void add(size_t j, double v)
void set(size_t j, double v)
Sets a single value at the given index.
Header file for functions for editing image contents.
unsigned char * data_pointer()
Classes for color objects.
Header file for 2D and 3D image I/O.
Vector3< int > page_size()
long get_chunk_size(size_t datasize)
Returns the chunk size per thread.