25 long i, j, k, cc, ds(x*y*z*n);
30 for ( i=0; i<n; i++ ) b[i] = 0;
32 for ( j=0; j<m; j++ ) {
33 for ( i=0; i<ds; i++ )
34 for ( cc=0, k=i*c; cc<c; cc++, k++ )
55 long imgsize(c*x*y*z);
57 for ( i=n=0; i<m; i++ )
if ( p[i] ) n += p[i]->n;
60 error_show(
"Error: No images to concatenate!", __FILE__, __LINE__);
65 compoundtype = p[0]->compoundtype;
76 cout <<
"Catenating " << n <<
" images" << endl;
78 for ( i=j=0; i<m; i++ )
if ( p[i] ) {
80 for ( k=j*imgsize, l=0; l<ds; k++, l++ )
set(k, (*p[i])[l]);
81 for ( k=0; k<p[i]->n; k++, j++ )
109 error_show(
"Bimage::blend", __FILE__, __LINE__);
113 if ( number < 3 ) number = 3;
116 cout <<
"Blending two images to create a series of " << number <<
" sub-images" << endl;
119 double fraction1, fraction2;
132 cout <<
"DEBUG img_morph_blend: datasize=" << datasize << endl;
134 for ( i=nn=0; nn<number; nn++ ) {
135 fraction2 = nn*1.0/(number - 1.0);
136 fraction1 = 1 - fraction2;
138 cout <<
"Calculating image " << nn+1 <<
": dnew = " << fraction1
139 <<
" x image1 + " << fraction2 <<
" x image2" << endl;
140 for ( j=0; j<datasize; j++, i++ )
141 pnu->
set(i, fraction1*(*
this)[j] + fraction2*(*p)[j]);
177 double radius,
double scale,
double shift,
int operation)
179 if ( radius <= 0 ) radius = (x > y)? x: y;
181 long i, si, xx, yy, zz;
183 double sx2, sy2, sz2, r2(radius*radius), v;
193 cout <<
"Placing a small image into a larger image:" << endl;
194 cout <<
"Small image size: " << p->
size() << endl;
195 cout <<
"Location: " << loc << endl;
196 cout <<
"Low limits: " << lo << endl;
197 cout <<
"High limits: " << hi << endl << endl;
200 for ( zz=lo[2]; zz<hi[2]; zz++ ) {
201 sz = (long) zz - start[2];
204 for ( yy=lo[1]; yy<hi[1]; yy++ ) {
205 sy = (long) yy - start[1];
208 for ( xx=lo[0]; xx<hi[0]; xx++ ) {
209 sx = (long) xx - start[0];
212 if ( sx2 + sy2 + sz2 <= r2 ) {
213 i =
index(xx, yy, zz, nn);
214 si = p->
index(sx, sy, sz);
215 v = (*p)[si] * scale +
shift;
216 switch ( operation ) {
217 case 1:
if ( v < (*
this)[i] )
set(i, v);
break;
218 case 2:
if ( v > (*
this)[i] )
set(i, v);
break;
244 long i, j, cc, tx, ty, tz, xx, yy, zz;
248 cout <<
"Adding tile:" << endl;
249 cout <<
"Location: " << p->
image[nn].
origin() << endl;
253 for ( tz=0; tz<p->z && zz<z; tz++, zz++ ) {
255 for ( ty=0; ty<p->y && yy<y; ty++, yy++ ) {
257 for ( tx=0; tx<p->x && xx<x; tx++, xx++ ) {
258 i = p->
index(0, tx, ty, tz, nn);
259 j =
index(0, xx, yy, zz, 0);
260 for ( cc=0; cc<c; cc++, i++, j++ ) {
284 long i, j, cc, tx, ty, tz, xx, yy, zz;
289 for ( i=0; i<p->n; i++ )
if ( i != nn ) {
292 if ( d[0] > p->x/2 ) imin[0] = p->x - d[0];
293 if ( d[0] < -p->x/2 ) imax[0] = -d[0];
294 if ( d[1] > p->y/2 ) imin[1] = p->y - d[1];
295 if ( d[1] < -p->y/2 ) imax[1] = -d[1];
296 if ( d[2] > p->z/2 ) imin[2] = p->z - d[2];
297 if ( d[2] < -p->z/2 ) imax[2] = -d[2];
306 for ( i=0; i<3; ++i ) {
307 if ( imin[i] ) wmin[i] = 1.0L/imin[i];
308 if ( imax[i] ) wmax[i] = 1.0L/(p->
size()[i] - imax[i]);
312 cout <<
"Placing tile:" << endl;
313 cout <<
"Location: " << p->
image[nn].
origin() << endl;
314 cout <<
"Tile size: " << p->
size() << endl;
315 cout <<
"Overlap minima: " << imin << endl;
316 cout <<
"Overlap maxima: " << imax << endl << endl;
320 for ( tz=0; tz<p->z; tz++, zz++ ) {
322 if ( tz < imin[2] ) w[2] = tz*wmin[2];
323 else if ( tz > imax[2] ) w[2] = (p->z - tz)*wmax[2];
325 for ( ty=0; ty<p->y; ty++, yy++ ) {
327 if ( ty < imin[1] ) w[1] = ty*wmin[1];
328 else if ( ty > imax[1] ) w[1] = (p->y - ty)*wmax[1];
330 for ( tx=0; tx<p->x; tx++, xx++ ) {
332 if ( tx < imin[0] ) w[0] = tx*wmin[0];
333 else if ( tx > imax[0] ) w[0] = (p->x - tx)*wmax[0];
334 i = p->
index(0, tx, ty, tz, nn);
335 j =
index(0, xx, yy, zz, 0);
336 wvol = w[0]*w[1]*w[2];
338 for ( cc=0; cc<c; cc++, i++, j++ )
add(j, (*p)[i] * wvol);
358 long i, j, cc, tx, ty, tz, xx, yy, zz, w;
363 for ( i=0; i<p->n; i++ )
if ( i != nn ) {
365 for ( j=0; j<3; ++j ) {
366 if ( d[j] && fabs(d[j]) <= p->
size()[j] ) {
367 if ( d[j] > 0 ) imin[j] = (long)(p->
size()[j] - d[j])/2;
368 else imax[j] = (long)(p->
size()[j] - d[j])/2;
374 for ( i=0; i<3; ++i )
379 cout <<
"Placing tile:" << endl;
380 cout <<
"Location: " << p->
image[nn].
origin() << endl;
381 cout <<
"Tile size: " << p->
size() << endl;
382 cout <<
"Overlap minima: " << imin << endl;
383 cout <<
"Overlap maxima: " << imax << endl << endl;
389 for ( tz=0; tz<p->z; tz++, zz++ ) {
391 if ( tz < imin[2] ) w = 0;
392 else if ( tz > imax[2] ) w = 0;
395 for ( ty=0; ty<p->y; ty++, yy++ ) {
397 if ( ty < imin[1] ) w = 0;
398 else if ( ty > imax[1] ) w = 0;
401 for ( tx=0; tx<p->x; tx++, xx++ ) {
403 if ( tx < imin[0] ) w = 0;
404 else if ( tx > imax[0] ) w = 0;
406 i = p->
index(tx, ty, tz, nn);
407 j =
index(xx, yy, zz);
408 for ( cc=0; cc<c; cc++, i++, j++ )
set(j, (*p)[i]);
437 for (
long nn=0; nn<pt->n; nn++ ) {
440 }
else if ( flag == 2 ) {
447 if ( flag == 0 &&
next ) {
449 if ( (*
next)[i] > 1 )
set(i, (*
this)[i]/(*next)[i]);
480 error_show(
"Bimage::linear_fit", __FILE__, __LINE__);
484 if ( max_exclude >= 1 ) max_exclude /= 100;
485 if ( max_exclude > 0.5 ) max_exclude = 0.5;
487 long i, j, nn, nm, nfit, best_excl(0);
488 long imgsize(x*y*z*c), num(imgsize);
490 double sx, sx2, sy, sxy, sd, dy, denominator, a, b, R, bestR(1e30), best_a(0), best_b(0);
491 double excl_voxels, diff, maxdiff, diff_cutoff;
494 for ( i=0; i<1024; i++ ) hist[i] = 0;
495 int* inc_mask =
new int[imgsize];
496 for ( i=0; i<imgsize; i++) inc_mask[i] = 1;
501 cout <<
"Linear least squares fit: image2 = intercept + slope x image1" << endl;
502 cout <<
"Maximum voxels to exclude: " << 100.0*max_exclude <<
" %" << endl;
506 cout <<
" %Set\t Voxels \t%Excluded\tIntercept\tSlope\t\tR" << endl;
508 cout <<
"Intercept\t Slope\t R-factor\tExcluded\t%" << endl;
510 for ( nn=0; nn<n; nn++) {
516 if ( pmask->
images() < n ) nm = 0;
517 for ( num=j=0, i=nm*imgsize; j<imgsize; j++,i++ ) {
521 }
else inc_mask[j] = -1;
524 for ( excl_voxels = 0; excl_voxels < max_exclude+0.01; excl_voxels += 0.01 ) {
526 a = b = sx = sx2 = sy = sxy = sd = dy = maxdiff = 0;
527 for ( j=0, i=nn*imgsize; j<imgsize; j++, i++ )
if ( inc_mask[j] > 0 ) {
530 sx2 += (*this)[i]*(*this)[i];
531 sxy += (*this)[i]*(*p)[i];
534 denominator = nfit*sx2 - sx*sx;
536 a = (sx2*sy - sx*sxy)/denominator;
537 b = (nfit*sxy - sx*sy) /denominator;
541 for ( j=0, i=nn*imgsize; j<imgsize; j++, i++ )
if ( inc_mask[j] >= 0 ) {
542 diff = a + b*(*this)[i] - (*p)[i];
545 sd += ((*p)[i]-sy)*((*p)[i]-sy);
548 if ( maxdiff < diff ) maxdiff = diff;
551 if ( dy < 1e-30 ) R = 0;
552 else if ( sd ) R = sqrt(dy/sd);
557 best_excl = num - nfit;
561 cout<< 100*excl_voxels <<
tab << nfit <<
tab
562 << 100.0-nfit*100.0/num <<
tab << a <<
tab << b <<
tab << R << endl;
565 cout <<
"DEBUG img_linear_fit: maxdiff=" << maxdiff << endl;
566 if ( maxdiff < 1e-30 ) maxdiff = 1;
567 for ( i=0; i<1024; i++ ) hist[i] = 0;
568 for ( j=0, i=nn*imgsize; j<imgsize; j++, i++ )
if ( inc_mask[j] >= 0 ) {
569 diff = fabs(a + b*(*
this)[i] - (*p)[i]);
570 bin = (int) (1000*diff/maxdiff);
571 if ( bin > 999 ) bin = 999;
577 while ( i > 1 && j < num*excl_voxels ) {
581 diff_cutoff = i*maxdiff/1000.0;
583 for ( j=0, i=nn*imgsize; j<imgsize; j++, i++ )
if ( inc_mask[j] >= 0 ) {
584 diff = fabs(a + b*(*
this)[i] - (*p)[i]);
586 if ( diff > diff_cutoff ) inc_mask[j] = 0;
590 for ( j=0, i=nn*imgsize; j<imgsize; j++, i++ )
591 set(i, best_a + best_b*(*
this)[i] - (*p)[i]);
596 cout << best_a <<
tab << best_b <<
tab << bestR <<
tab << best_excl <<
tab << best_excl*100.0/num <<
" %" << endl;
618 error_show(
"Bimage::histomatch", __FILE__, __LINE__);
624 double denom, fraction;
625 int* hist1 =
new int[bins];
626 int* hist2 =
new int[bins];
627 int* map =
new int[bins];
628 double* dens =
new double[bins];
630 for ( i=0; i<bins; i++ )
631 hist1[i] = hist2[i] = 0;
634 cout <<
"Matching histograms in " << bins <<
" bins:" << endl << endl;
636 for ( i=0; i<datasize; i++ ) {
637 h1 = (long) (bins*((*
this)[i] - min)/(max - min));
639 if ( h1 >= bins ) h1 = bins - 1;
640 if ( h2 >= bins ) h2 = bins - 1;
646 cout <<
"Histograms done" << endl;
648 for ( i=bins-1; i>0; i-- ) {
649 hist1[i-1] += hist1[i];
650 hist2[i-1] += hist2[i];
654 cout <<
"Integration done" << endl;
656 for ( h2=0; h2<bins; h2++ ) {
658 while ( ( h1 < bins ) && ( hist1[h1] > hist2[h2] ) ) h1++;
661 denom = hist1[h1+1] - hist1[h1] - hist2[h2+1] + hist2[h2];
663 denom = hist1[h1] - hist1[h1-1] - hist2[h2] + hist2[h2-1];
665 fraction = (hist2[h2] - hist1[h1])/denom;
669 dens[h2] = (h2 + fraction)*(max - min)/bins + min;
673 cout <<
"Mapping calculated" << endl;
674 cout <<
"Bin\tHist1\tHist2\tMapping\tDensity" << endl;
675 for ( i=0; i<bins; i++ )
676 cout << i <<
tab << hist1[i] <<
tab << hist2[i] <<
tab
677 << map[i] <<
tab << dens[i] << endl;
680 for ( i=0; i<datasize; i++ ) {
682 if ( h2 >= bins ) h2 = bins - 1;
683 set(i, dens[map[h2]]);
689 cout <<
"Histogram matching done" << endl;