59 if ( sign < 0 ) sign = -1;
60 else if ( sign > 0 ) sign = 1;
63 if ( threshold < avg ) sign = -1;
66 long i, count(0), rsize;
67 double abs_threshold(sign*threshold);
70 cout <<
"Finding regions:" << endl;
71 cout <<
"Threshold: " << threshold <<
" (" << sign <<
")" << endl;
77 for ( i=0; i<n; i++ ) pmask->
image[i] =
image[i];
80 for ( i=0; i<datasize; i++ ) {
81 if ( (*pmask)[i] == 0 ) {
82 if ( sign*(*
this)[i] > abs_threshold ) {
86 cout <<
"DEBUG Bimage::regions: i=" << i <<
" count=" <<
87 count <<
" rsize=" << rsize << endl;
95 cout <<
"Regions: " << count << endl << endl;
120 long region_number,
double threshold,
int sign)
123 cout <<
"DEBUG Bimage::region_assign: Starting with region " <<
124 region_number <<
" idx=" << idx << endl;
126 if ( sign < 0 ) sign = -1;
127 else if ( sign > 0 ) sign = 1;
130 if ( threshold < avg ) sign = -1;
133 double abs_threshold(sign*threshold);
134 long i, j, nn, kx, ky, kz;
140 long number, new_number, count(1);
141 long list_size = 2*(x + y)*z;
142 if ( z > 1 ) list_size += 2*x*y;
143 int* list =
new int[list_size];
144 int* new_list =
new int[list_size];
147 pmask->
set(idx, region_number);
153 while ( number > 0 ) {
155 cout <<
"DEBUG Bimage::region_assign: region=" << region_number <<
" list size=" << number << endl;
157 for ( i=0; i<number; i++ ) {
162 for ( kz=lo[2]; kz<=hi[2]; kz++ ) {
163 for ( ky=lo[1]; ky<=hi[1]; ky++ ) {
164 for ( kx=lo[0]; kx<=hi[0]; kx++ ) {
165 j =
index(kx, ky, kz, nn);
166 if ( ((*pmask)[j] == 0) && (sign*(*
this)[j] > abs_threshold) ) {
167 pmask->
set(j, region_number);
169 new_list[new_number] = j;
171 if ( new_number >= list_size ) {
172 cerr <<
"Error: The list size exceeded its limit ("
173 << list_size <<
")" << endl;
181 for ( i=0; i<number; i++ ) list[i] = new_list[i];
188 cout <<
"DEBUG Bimage::region_assign: Ending with region " << region_number << endl;
201 double threshold_last,
double threshold_step)
204 double threshold, a(0);
207 if ( threshold_step == 0 ) {
208 if ( threshold_last == threshold_first )
211 threshold_step = threshold_last - threshold_first;
213 if ( threshold_step > 0 && threshold_first > threshold_last )
214 threshold_step = -threshold_step;
215 if ( threshold_step < 0 && threshold_first < threshold_last )
216 threshold_step = -threshold_step;
219 if ( threshold_step < 0 ) sign = -1;
220 threshold_last += threshold_step/2;
222 cout <<
"Finding the number of regions at each threshold:" << endl;
223 cout <<
"Thresh\tRegions\tAvgSize" << endl;
224 for ( threshold = threshold_first; sign*threshold <= sign*threshold_last;
225 threshold+=threshold_step ) {
228 cout << threshold <<
tab << pmask->
maximum() <<
tab << a << endl;
257 double threshold_lo,
double threshold_step,
int fill_borders)
260 error_show(
"No region mask in Bimage::region_flood", __FILE__, __LINE__);
265 double threshold, dt;
267 if ( threshold_hi < threshold_lo )
268 swap(threshold_hi, threshold_lo);
269 if ( threshold_step == 0 )
270 threshold_step = (threshold_lo - threshold_hi)/100;
271 else if ( (threshold_lo - threshold_hi)/threshold_step < 0 )
272 threshold_step *= -1;
275 cout <<
"Segmenting an image by flooding from a threshold or region set:" << endl;
277 cout <<
"Region set: " << pmask->
file_name() << endl;
278 cout <<
"Starting threshold: " << threshold_hi << endl;
279 cout <<
"Finishing threshold: " << threshold_lo << endl;
280 cout <<
"Threshold step: " << threshold_step << endl;
284 error_show(
"Error in Bimage::region_flood", __FILE__, __LINE__);
285 cerr <<
"The region mask must have the same dimensions as the image!" << endl;
290 cout <<
"Starting regions: " << pmask->
maximum() << endl;
296 for ( threshold=threshold_hi; threshold>threshold_lo; threshold+=threshold_step ) {
297 dt = threshold_hi - threshold_lo;
298 if ( dt > 0 ) dt = 100*(threshold_hi - threshold)/dt;
301 cout <<
"Processing threshold: " << threshold <<
" (" << dt <<
" %) \r" << flush;
304 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t it){
305 if ( (*
this)[it] > threshold )
311 #pragma omp parallel for
312 for (
long it=0; it<datasize; it++ ) {
313 if ( (*
this)[it] > threshold )
319 for ( i=0; i<datasize; i++ ) pmask->
set(i, (*pt)[i]);
325 long nborder(0), dnb(1), maxtries(20);
327 for ( i=0; i<datasize; i++ )
if ( (*pmask)[i] < 0 ) nborder++;
329 cout <<
"Border voxels: " << nborder << endl;
332 if ( fill_borders ) {
334 cout <<
"Assigning borders:" << endl;
336 while ( maxtries-- > 0 && nborder > 0 && dnb > 0 ) {
338 cout <<
"Processing border voxels: " << nborder <<
" \r" << flush;
343 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t it){
344 if ( (*pmask)[it] < 0 ) {
346 pmask->
set(it, (*pmask)[j]);
347 if ( (*pmask)[it] < 0 ) {
349 pmask->
set(it, (*pmask)[j]);
354 #pragma omp parallel for
355 for (
long it=0; it<datasize; it++ ) {
356 if ( (*pmask)[it] < 0 ) {
358 pmask->
set(it, (*pmask)[j]);
359 if ( (*pmask)[it] < 0 ) {
361 pmask->
set(it, (*pmask)[j]);
366 for (
long it=0; it<datasize; it++ )
if ( (*pmask)[it] < 0 ) nborder++;
372 cout <<
"Processing border voxels: " << nborder << endl;
374 cerr <<
"Warning: " << nborder <<
" border voxels were not assigned!" << endl;
377 long nseg = (long)(pmask->
maximum() + 1.9);
382 for ( i=0; i<nseg; i++ ) vol[i] = 0;
383 for ( i=0; i<datasize; i++ )
384 if ( (*pmask)[i] > -1 ) vol[(
long)(*pmask)[i]]++;
385 cout <<
"Region\tVoxels\tVolume\tMass" << endl;
386 for ( i=1; i<nseg; i++ )
396 cout <<
"DEBUG Bimage::region_flood: Finished with segmentation" << endl;
408 if ( (*
this)[idx] != 0 )
return (
long)(*this)[idx];
410 long i, xx, yy, zz, nn(0), nv(0);
417 for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
418 for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
419 for ( xx=lo[0]; xx<=hi[0]; xx++ ) {
420 i =
index(0,xx,yy,zz,nn);
422 if ( (*
this)[i] > 0 ) {
424 nv = (long)(*
this)[i];
425 else if ( nv != (*
this)[i] )
434 cout <<
"DEBUG Bimage::check_neighbors: Ending with region " << (int)(*
this)[idx] << endl;
459 if ( kernel_size < 3 ) kernel_size = 3;
462 long hk(kernel_size/2), npeak(0);
465 cout <<
"Segmenting an image by flooding from peaks:" << endl;
466 cout <<
"Kernel size: " << 2*hk+1 << endl;
467 cout <<
"Threshold: " << threshold << endl;
468 if ( wrap ) cout <<
"With wrapping" << endl;
472 for ( nn=0; nn<n; nn++ ) pmask->
image[nn] =
image[nn];
474 int* pnt =
new int[datasize];
475 for ( i=0; i<datasize; i++ ) pnt[i] = -1;
478 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t i){
479 if ( (*
this)[i] >= threshold ) {
485 #pragma omp parallel for
486 for ( i=0; i<datasize; ++i ) {
487 if ( (*
this)[i] >= threshold ) {
494 for ( i=npeak=0; i<datasize; i++ )
495 if ( pnt[i] == i ) pmask->
set(i, ++npeak);
502 for ( i=0; i<datasize; i++ )
503 if ( (*
this)[i] >= threshold && (*pmask)[i] < 1 )
504 pmask->
set(i, (*pmask)[pnt[i]]);
505 for ( i=nn=0; i<datasize; i++ )
if ( (*pmask)[i] > 0 ) nn++;
515 cout <<
"Number of peaks: " << npeak << endl << endl;
538 double setvalue,
int sign)
540 if ( min_size > max_size ) swap(min_size, max_size);
542 if ( sign < 0 ) sign = -1;
543 else if ( sign > 0 ) sign = 1;
546 if ( threshold < avg ) sign = -1;
551 cout <<
"Finding blobs above " << threshold << endl;
553 cout <<
"Finding blobs below " << threshold << endl;
557 cout <<
"Size range to keep: " << min_size <<
" - " << max_size << endl;
558 cout <<
"Set value: " << setvalue << endl << endl;
562 long count = (long) pmask->
maximum();
564 long i, f, nvox(0), nblobs(0);
566 int* region_size =
new int[count+1];
568 for ( i=0; i<=count; i++ ) region_size[i] = 0;
570 for ( i=0; i<datasize; i++ ) region_size[(
long)(*pmask)[i]]++;
573 cout <<
"Region\tSize";
574 for ( i=0; i<=count; i++ ) {
575 if ( i%5 == 0 ) cout << endl << i;
576 cout <<
tab << region_size[i];
578 cout << endl << endl;
581 if ( ( min_size > 0 ) || ( max_size < datasize ) ) {
582 for ( i=1; i<=count; i++ )
583 if ( region_size[i] < min_size || region_size[i] > max_size ) nblobs++;
585 for ( i=0; i<datasize; i++ ) {
586 f = (long)(*pmask)[i];
588 if ( ( region_size[f] < min_size ) || ( region_size[f] > max_size ) ) {
597 cout <<
"Number of blobs deleted: " << nblobs << endl;
598 cout <<
"Number of voxels set: " << nvox << endl << endl;
601 delete[] region_size;
624 double threshold = avg + 3*std;
625 if ( threshold >= max ) threshold = (max - avg)*0.75 + avg;
627 blobs(threshold, 100, 1e30, avg, 0);
629 threshold = avg - 3*std;
630 if ( threshold <= min ) threshold = (min - avg)*0.75 + avg;
632 blobs(threshold, 1000, 1e30, avg, 0);
683 cout <<
"Replacing maxima above: " << threshold << endl;
684 cout <<
"Number of maxima: " << pmask->
maximum() << endl << endl;
692 long i, j, k, m, nn, xx, yy, zz, iter, nvr(0);
693 long kx, ky, kz, nval, tx, ty, tz;
694 long nr((
long) (pmask->
maximum() + 1.9)), rs;
698 int* num =
new int[nr];
699 int* nimg =
new int[nr];
705 for ( j=1; j<nr; j++ ) rmin[j] =
size();
707 for ( i=nn=0; nn<n; nn++ ) {
708 for ( zz=0; zz<z; zz++ ) {
709 for ( yy=0; yy<y; yy++ ) {
710 for ( xx=0; xx<x; xx++, i++ ) {
711 j = (long) (*pmask)[i];
718 if ( rmin[j][0] > xx ) rmin[j][0] = xx;
719 if ( rmin[j][1] > yy ) rmin[j][1] = yy;
720 if ( rmin[j][2] > zz ) rmin[j][2] = zz;
721 if ( rmax[j][0] < xx ) rmax[j][0] = xx;
722 if ( rmax[j][1] < yy ) rmax[j][1] = yy;
723 if ( rmax[j][2] < zz ) rmax[j][2] = zz;
731 cout <<
"Regions:\n#\tImage\tx\ty\tz\tSize" << endl;
732 for ( j=1; j<nr; j++ ) {
734 if ( num[j] ) region[j] /= num[j];
736 cout << j <<
tab << nn <<
tab << region[j] <<
tab << num[j] << endl;
737 rmin[j] = rmin[j].
max(0);
738 rmax[j] = rmax[j].
min(
size()-1);
739 region_size = rmax[j] - rmin[j] + 1;
740 rs = (long) region_size.
volume();
741 temp =
new float[rs];
744 while ( nval < num[j] ) {
745 for ( k=0; k<rs; k++ )
747 for ( zz=rmin[j][2], tz=0; zz<=rmax[j][2]; zz++, tz++ ) {
748 for ( yy=rmin[j][1], ty=0; yy<=rmax[j][1]; yy++, ty++ ) {
749 for ( xx=rmin[j][0], tx=0; xx<=rmax[j][0]; xx++, tx++ ) {
750 i =
index(0,xx,yy,zz,nn);
753 if ( (*pmask)[i] == j ) {
756 for ( kz=lo[2]; kz<=hi[2]; kz++ ) {
757 for ( ky=lo[1]; ky<=hi[1]; ky++ ) {
758 for ( kx=lo[0]; kx<=hi[0]; kx++ ) {
759 k =
index(0,kx,ky,kz,nn);
760 if ( (*pmask)[k] < 1 ) {
768 k = (tz*region_size[1] + ty)*region_size[0] + tx;
775 for ( zz=rmin[j][2], k=tz=0; zz<=rmax[j][2]; zz++, tz++ ) {
776 for ( yy=rmin[j][1], ty=0; yy<=rmax[j][1]; yy++, ty++ ) {
777 for ( xx=rmin[j][0], tx=0; xx<=rmax[j][0]; xx++, tx++, k++ ) {
778 if ( temp[k] > min ) {
779 i =
index(0,xx,yy,zz,nn);
781 if ( (*pmask)[i] == j ) pmask->
set(i, 0);
789 cout << iter <<
":\t" << nval << endl;
822 if ( rho <= 0 ) rho =
RHO;
824 long i, j, nn, nvox(0);
829 double volume(mol_weight/rho);
834 double min_step(1e-6*std);
837 cout <<
"Finding the threshold for image " << img_num+1 <<
":" << endl;
838 cout <<
"Protein density: " << rho <<
" Da/A3" << endl;
839 cout <<
"Molecular weight: " << mol_weight <<
" Da" << endl;
840 cout <<
"Molecular volume: " <<
volume <<
" A3" << endl;
841 cout <<
"Voxel size: " << vs <<
" A3" << endl << endl;
845 cout <<
"Image\tThresh\tVolRes\tIntMass\tMW/mass" << endl;
846 for ( nn=0; nn<n; nn++ ) {
848 cout <<
" Threshold\tThreshStep\tVolume(A3)\tDifference\tMass" << endl;
854 while ( fabs(vol_diff) >= min_vol && fabs(thresh_step) > min_step ) {
857 if ( vol_diff > 0 ) {
858 if ( thresh_step < 0 ) thresh_step *= -0.5;
860 if ( thresh_step > 0 ) thresh_step *= -0.5;
862 threshold += thresh_step;
863 for ( i=nn*imgsize, j=i+imgsize, nvox = 0, mass = 0; i<j; i++ ) {
864 if ( (*
this)[i] >= threshold ) {
869 vol_diff = nvox*vs -
volume;
871 cout << threshold <<
tab << thresh_step <<
tab
872 << nvox*vs <<
tab << vol_diff <<
tab << mass << endl;
873 dnvox = nvox - dnvox;
874 dmass = mass - dmass;
877 cout << nn+1 <<
tab << threshold <<
tab <<
878 fabs(vol_diff)/
volume <<
tab << mass <<
tab << mol_weight/mass << endl;
906 if ( rho <= 0 ) rho =
RHO;
911 for ( i=img_num*imgsize, j=i+imgsize, nvox=0; i<j; i++ )
912 if ( (*
this)[i] >= threshold ) nvox++;
919 cout <<
"Protein density: " << rho <<
" Da/A3" << endl;
920 cout <<
"Threshold: " << threshold << endl;
921 cout <<
"Volume: " <<
volume <<
" A3" << endl;
922 cout <<
"Molecular weight: " << mass <<
" Da" << endl << endl;
936 long i, j, jn, jz, jy, nn;
938 long slicesize(x*y), imgsize(x*y*z);
944 if ( threshold < avg ) sign = -1;
949 for ( i=nn=0; nn<n; nn++ ) {
951 for ( zz=0; zz<z; ++zz ) {
952 lo[2] = (zz>nk[2])? zz-nk[2]: 0;
953 hi[2] = (zz<z-nk[2])? zz+nk[2]: zz;
954 cout <<
"z = " << zz << endl;
955 for ( yy=0; yy<y; ++yy ) {
956 lo[1] = (yy>nk[1])? yy-nk[1]: 0;
957 hi[1] = (yy<y-nk[1])? yy+nk[1]: yy;
958 for ( xx=0; xx<x; ++xx, ++i ) {
959 lo[0] = (xx>nk[0])? xx-nk[0]: 0;
960 hi[0] = (xx<x-nk[0])? xx+nk[0]: xx;
962 if ( sign*(*
this)[i] >= threshold ) {
967 jz = jn + lo[2]*slicesize;
969 for ( k[2]=lo[2]; k[2]<=hi[2]; ++k[2], jz += slicesize ) {
971 for ( k[1]=lo[1]; k[1]<=hi[1]; ++k[1], jy += x ) {
973 for ( k[0]=lo[0]; k[0]<=hi[0]; ++k[0], ++j ) {
976 if ( sign*(*
this)[j] < threshold )
978 if ( dk < d0 && sign*(*
this)[j] < threshold )
988 if ( dd > 12 ) pv->
set(i, 1);
1006 long i, j, jn, jz, jy, nn;
1007 long xx, yy, zz, kx, ky, kz;
1008 long iter, dvol, vol(0);
1009 long slicesize(x*y), imgsize(x*y*z);
1016 int nb, nbcut(6), sign(1);
1017 if ( z < 2 ) nbcut = 2;
1018 if ( threshold < avg ) sign = -1;
1023 for ( nn=0; nn<n; nn++ ) {
1034 cout <<
"Image " << nn+1 <<
":" << endl;
1035 cout <<
"Origin: " << lo2 << endl;
1036 cout <<
"Iter\tVol\tdVol\tVol(A3)\tdVol(A3)" << endl;
1039 while ( dvol > 0 ) {
1042 for ( zz=lo[2]; zz<=hi[2]; zz++ ) {
1043 lo2[2] = (zz)? zz-1: 0;
1044 hi2[2] = (zz<z-1)? zz+1: zz;
1045 for ( yy=lo[1]; yy<=hi[1]; yy++ ) {
1046 lo2[1] = (yy)? yy-1: 0;
1047 hi2[1] = (yy<y-1)? yy+1: yy;
1048 for ( xx=lo[0]; xx<=hi[0]; xx++ ) {
1049 lo2[0] = (xx)? xx-1: 0;
1050 hi2[0] = (xx<x-1)? xx+1: xx;
1051 i =
index(0,xx,yy,zz,nn);
1053 if ( (*pv)[i] < 1 && sign*(*
this)[i] <= threshold ) {
1054 jz = jn + lo2[2]*slicesize;
1055 for ( kz=lo2[2]; kz<=hi2[2]; kz++, jz += slicesize ) {
1057 for ( ky=lo2[1]; ky<=hi2[1]; ky++, jy += x ) {
1059 for ( kx=lo2[0]; kx<=hi2[0]; kx++, j++ ) {
1060 if ( i != j && (*pv)[j] > 1 ) nb++;
1064 if ( ( nb >= vol ) || ( nb > nbcut ) ) {
1067 }
else pv->
set(i, 0);
1074 for ( zz=lo2[2]; zz<=hi2[2]; zz++ ) {
1075 for ( yy=lo2[1]; yy<=hi2[1]; yy++ ) {
1076 for ( xx=lo2[0]; xx<=hi2[0]; xx++ ) {
1077 i =
index(0,xx,yy,zz,nn);
1078 if ( (*pv)[i] == 1 ) {
1080 if ( lo[0] == xx && xx > 0 ) lo[0]--;
1081 if ( lo[1] == yy && yy > 0 ) lo[1]--;
1082 if ( lo[2] == zz && zz > 0 ) lo[2]--;
1083 if ( hi[0] == xx && xx < x - 1 ) hi[0]++;
1084 if ( hi[1] == yy && yy < y - 1 ) hi[1]++;
1085 if ( hi[2] == zz && zz < z - 1 ) hi[2]++;
1092 cout << iter <<
tab << vol <<
tab << dvol <<
tab
1093 << vol*vs <<
tab << dvol*vs << endl;
1094 if ( ( mask_out_freq > 0 && iter%mask_out_freq == 0 ) ||
1096 if ( n == 1 ) fn =
Bstring(iter,
"v%04ld.mrc");
1129 long i, j, j4, h, k, m;
1130 double v, dv, mdv, da(1e30);
1131 double r(ratio/std);
1135 long* nm =
new long[nregion];
1136 double* a =
new double[4*nregion];
1137 double* an =
new double[4*nregion];
1144 cout <<
"Segmentation using K-means:" << endl;
1145 cout <<
"Number of regions: " << nregion << endl;
1146 cout <<
"Density:distance ratio: " << ratio << endl << endl;
1150 for ( j=j4=0; j<nregion; j++, j4+=4 ) {
1152 for ( h=0; h<3; h++ ) a[j4+h] = vec[h];
1153 a[j4+3] =
get(0, vec);
1155 cout << j+1 <<
tab << vec <<
tab << a[j4+3] << endl;
1160 cout <<
"#\tChange" << endl;
1161 for ( i=0; i<max_iter && da > 0.001; i++ ) {
1162 for ( j=0; j<nregion; j++ ) nm[j] = 0;
1163 for ( j=0; j<4*nregion; j++ ) an[j] = 0;
1165 for ( k=0; k<datasize; k++ ) {
1168 for ( j=j4=m=0, mdv=1e30; j<nregion; j++, j4+=4 ) {
1169 if ( i ) dv = r * fabs(a[j4+3] - v);
1171 for ( h=0; h<3; h++ ) dv += fabs(a[j4+h] - coor[h])/
size()[h];
1178 for ( h=0; h<3; h++ ) an[4*m+h] += coor[h];
1183 for ( j=j4=0, da=0; j<nregion; j++, j4+=4 ) {
1186 for ( h=0; h<3; h++ ) an[j4+h] = vec[h];
1187 an[j4+3] =
get(0, vec);
1190 cout << j+1 <<
tab << nm[j];
1191 for ( h=0; h<4; h++ ) {
1192 if ( nm[j] ) an[j4+h] /= nm[j];
1193 da += fabs(a[j4+h] - an[j4+h]);
1196 cout <<
tab << a[j4+h];
1203 cout << i+1 <<
tab << da << endl;
1220 if( r1->
w < r2->
w )
return -1;
1227 while ( j != region[j].p ) j = region[j].
p;
1234 if ( i == j )
return 0;
1236 int s(region[i].s + region[j].s);
1237 double a((region[i].s*region[i].a + region[j].s*region[j].a)/s);
1239 if ( region[i].r > region[j].r ) {
1241 region[i].
s += region[j].
s;
1245 region[j].
s += region[i].
s;
1247 if ( region[i].r == region[j].r ) region[j].
r++;
1262 if ( pmask ) pmask->
to_mask();
1264 for ( i=0; i<imagesize; i++ ) {
1268 if ( !pmask || ( pmask && (*pmask)[i] ) ) {
1270 region[i].
a = (*p)[i];
1272 region[i].
a = p->
rgb(i).average();
1274 else region[i].
a = 0;
1286 long i, xx, yy, zz, x1, y1, z1;
1288 long dim = (z > 1)? 3: 2;
1293 for ( nn=i=0; i<imagesize; i++ )
if ( (*pmask)[i] ) nn++;
1296 if ( connect_type < 1 ) me = dim*nn;
1297 else me = (z > 1)? 13*nn: 4*nn;
1301 for ( ne=i=zz=0; zz<z; zz++ ) {
1302 for ( yy=0; yy<y; yy++ ) {
1303 for ( xx=0; xx<x; xx++, i++ )
if ( !pmask || ( pmask && (*pmask)[i] ) ) {
1306 if ( connect_type ) {
1307 for ( z1=(zz>0)?zz-1:zz; z1<zz+1 && z1<z; z1++ ) {
1308 for ( y1=(yy>0)?yy-1:yy; y1<yy+1 && y1<y; y1++ ) {
1310 edge[ne++].
v = (z1*y + y1)*x + x1;
1315 edge[ne++].
v = (zz*y + yy)*x + x1;
1320 if ( connect_type ) {
1321 for ( z1=(zz>0)?zz-1:zz; z1<zz+1 && z1<z; z1++ ) {
1323 edge[ne++].
v = (z1*y + y1)*x + xx;
1327 edge[ne++].
v = (zz*y + y1)*x + xx;
1333 edge[ne++].
v = (z1*y + yy)*x + xx;
1340 for ( i=0; i<ne; i++ ) edge[i].w = fabs((*p)[edge[i].
u] - (*p)[edge[i].
v]);
1343 for ( i=0; i<ne; i++ ) edge[i].w = fabs((p->
rgb(edge[i].
u) - p->
rgb(edge[i].v)).rms());
1355 for ( i=0; i<ne; i++ ) {
1360 if ( e.
w <= region[u].
a && e.
w <= region[v].
a ) {
1363 region[u].
a = e.
w + threshold/region[u].
s;
1377 double d2, ln1, ln2, s1, s2;
1378 double lnd(2*log(6*nr));
1379 long i, u, v, nrc(nr);
1382 for ( i=0; i<ne; i++ ) {
1387 d2 = region[u].
a - region[v].
a; d2 *= d2;
1388 s1 = region[u].
s; s2 = region[v].
s;
1391 ln1 = lnd + log(1.0 + s1);
1392 ln2 = lnd + log(1.0 + s2);
1393 if ( d2 < fac * (ln1/s1 + ln2/s2) )
1403 if ( min_size < 1 )
return nrc;
1409 for ( i=0; i<ne; i++ ) {
1413 if ( ( u != v ) && ( region[u].s < min_size || region[v].s < min_size ) ) {
1444 double complexity,
long min_size,
int return_type)
1450 long slicesize(x*y);
1451 long imagesize(slicesize*z);
1452 long dim = (z > 1)? 3: 2;
1454 double threshold(std/complexity);
1456 if ( pmask ) pmask->
to_mask();
1459 cout <<
"Segmenting image:" << endl;
1460 cout <<
"Algorithm: ";
1462 cout <<
"Simple" << endl;
1463 cout <<
"Threshold: " << threshold << endl;
1465 cout <<
"Statistical region merging" << endl;
1466 cout <<
"Complexity: " << complexity << endl;
1468 cout <<
"Connectivity: ";
1469 if ( connect_type < 1 ) cout << 2*dim << endl;
1470 else if ( dim < 3 ) cout << 8 << endl;
1471 else cout << 26 << endl;
1472 cout <<
"Minimum size: " << min_size << endl;
1483 cout <<
"Number of edges: " << ne << endl;
1485 long nr(imagesize), nrc(imagesize);
1489 for ( i=0; i<imagesize; i++ ) region[i].a = threshold;
1496 if ( min_size > 0 ) {
1498 cout <<
"Regions before merging: " << nrc << endl;
1505 cout <<
"Number of regions: " << nrc << endl << endl;
1509 if ( return_type < 1 ) {
1512 for ( nrc=i=0; i<imagesize; i++ )
1513 if ( region[i].p == i ) region[i].
r = ++nrc;
1515 for ( i=0; i<imagesize; i++ ) {
1517 pseg->
set(i, region[j].r);
1522 for ( i=0; i<imagesize; i++ )
1523 region[i].a = region[i].s = 0;
1525 for ( i=0; i<imagesize; i++ ) {
1527 region[j].
a += (*this)[i];
1531 for ( i=0; i<imagesize; i++ )
1532 if ( region[j].s ) region[i].
a /= region[i].
s;
1534 for ( i=0; i<imagesize; i++ ) {
1536 pseg->
set(i, region[j].a);
1556 double colorweight, vector<Bsuperpixel>& seg)
1559 double d, icw(1/colorweight);
1561 vector<double> dvec = seg[i].coordinates();
1562 vector<long> vec(dvec.begin(), dvec.end());
1564 vector<long>
kmin = vec - vstep;
1571 vector<long>
kmax = vec + vstep;
1580 for ( zz=
kmin[2]; zz<
kmax[2]; zz++ ) {
1581 for ( yy=
kmin[1]; yy<
kmax[1]; yy++ ) {
1582 for ( xx=
kmin[0]; xx<
kmax[0]; xx++ ) {
1583 j = p->
index(xx, yy, zz);
1584 if ( i == (*pmask)[j]) {
1585 seg[i].add(xx, yy, zz, p->
values(j));
1593 for ( zz=
kmin[2]; zz<
kmax[2]; zz++ ) {
1594 for ( yy=
kmin[1]; yy<
kmax[1]; yy++ ) {
1595 for ( xx=
kmin[0]; xx<
kmax[0]; xx++ ) {
1596 j = p->
index(xx, yy, zz);
1597 if ( i == (*pmask)[j]) {
1598 d = seg[i].difference2(p->
values(j));
1600 if ( seg[i].weight() < d ) seg[i].weight(d);
1610 double colorweight, vector<Bsuperpixel>& seg)
1612 long nseg(seg.size());
1615 dispatch_apply(nseg, dispatch_get_global_queue(0, 0), ^(
size_t i){
1619 #pragma omp parallel for
1620 for (
long i=0; i<nseg; i++ )
1636 vector<Bsuperpixel>& seg)
1638 long j((*pmask)[i]), k, m;
1639 double dd, dc, d, fom(1e30);
1642 vector<double> scoor;
1644 vector<double> val = p->
values(i);
1649 scoor = seg[m].coordinates() - coor3;
1651 dc = seg[m].difference2(val);
1652 d = dd*wd + dc/seg[m].weight();
1657 if ( ++k ==
NNEIGHBOR || ( m = seg[j].neighbor(k) ) < 0 )
break;
1664 vector<Bsuperpixel>& seg)
1667 double dr(
volume(vstep)), wd(1.0L/dr);
1672 dispatch_apply(p->
image_size(), dispatch_get_global_queue(0, 0), ^(
size_t i){
1676 #pragma omp parallel for
1681 for ( i=0; i<pmask->
image_size(); i++ )
if ( (*pmask)[i] - (*pmc)[i] ) nc++;
1693 for ( cc=0; cc<p->
channels(); cc++, j++ )
1694 p->
set(j, seg[(
long)(*pmask)[i]].channel(cc));
1702 double min(seg[i].channel(cc));
1703 for ( j=0; j<
NNEIGHBOR && ( k = seg[i].neighbor(j) ) >= 0; ++j ) {
1704 if ( seg[k].channel(cc) < min ) {
1705 min = seg[k].channel(cc);
1717 for ( cc=0; cc<p->
channels(); cc++, j++ ) {
1719 p->
set(j, seg[is].channel(cc));
1728 double max(seg[i].channel(cc));
1729 for ( j=0; j<
NNEIGHBOR && ( k = seg[i].neighbor(j) ) >= 0; ++j ) {
1730 if ( seg[k].channel(cc) > max ) {
1731 max = seg[k].channel(cc);
1743 for ( cc=0; cc<p->
channels(); cc++, j++ ) {
1745 p->
set(j, seg[is].channel(cc));
1757 for ( cc=0; cc<p->
channels(); cc++, j++ ) {
1759 v = seg[(long)(*pmask)[i]].channel(cc);
1760 p->
set(j, seg[is].channel(cc) - v);
1768 long nseg(seg.size());
1772 for ( j=0; j<nseg; j++ )
if ( j != i ) {
1773 d =
distance(seg[i].coordinates(), seg[j].coordinates());
1775 seg[i].add_neighbor(j);
1785 long nseg(seg.size());
1787 double dmax(1.9*step);
1789 for ( i=0; i<nseg; i++ ) seg[i].clear_neighbors();
1792 dispatch_apply(nseg, dispatch_get_global_queue(0, 0), ^(
size_t i){
1796 #pragma omp parallel for
1797 for ( i=0; i<nseg; i++ )
1801 for ( i=0; i<nseg; i++ )
1802 for ( j=0; j<
NNEIGHBOR && seg[i].neighbor(j) >= 0; ++j ) nn++;
1805 for ( i=0; i<nseg; i++ ) {
1806 cout << seg[i].index() <<
tab << seg[i].coordinates();
1807 for ( j=0; j<
NNEIGHBOR && seg[i].neighbor(j) >= 0; ++j ) cout <<
tab << seg[i].neighbor(j);
1823 long nseg = (long) (max + 1.9);
1826 cout <<
"Setting up " << nseg <<
" segments" << endl;
1828 vector<Bsuperpixel> seg(nseg);
1832 vector<double> v(cc, 0);
1834 for ( i=0; i<nseg; i++ ) {
1836 seg[i].channels(cc);
1842 seg[j].add(coor3[0], coor3[1], coor3[2], v);
1845 for ( i=0; i<nseg; i++ )
1851 cout <<
"Total neigbors: " << nn << endl << endl;
1879 cout <<
"SLIC superpixel segmentation:" << endl;
1880 cout <<
"Step size: " << step << endl;
1881 cout <<
"Color weight: " << colorweight << endl;
1882 cout <<
"Maximum number of iterations: " << iterations << endl;
1883 cout <<
"Stopping condition: " << stop <<
" % (" << nstop <<
")" << endl << endl;
1888 vector<long> vstep = {step,step,step};
1889 for ( i=0; i<3; ++i ) vstep[i] *= 2;
1891 if ( vstep[2] > z ) vstep[2] = z;
1897 time_t t = time(NULL);
1900 cout <<
"Iter\tChange\t∆\tTime" << endl;
1902 for ( i=0; i<iterations && nc > nstop; i++ ) {
1905 cout << i+1 <<
tab << nc <<
tab << nc-ncp <<
tab << (long)(time(NULL) - t) << endl;
1919 if ( !p->
next )
return 0;
1925 for ( pb = p; pb->
next; p = pb, pb = pb->
next ) ;
1929 j = pb->index(c, 0);
1930 p->
set(i, (*pb)[j]);
1939 vector<Bsuperpixel>
Bimage::superpixels(
long step,
double colorweight,
long iterations,
long bin_level,
double stop)
1944 cout <<
"SLIC superpixel segmentation:" << endl;
1945 cout <<
"Step size: " << step << endl;
1946 cout <<
"Bin levels: " << bin_level << endl;
1947 cout <<
"Color weight: " << colorweight << endl;
1948 cout <<
"Maximum number of iterations: " << iterations << endl;
1949 cout <<
"Stopping condition: " << stop <<
" %" << endl << endl;
1955 vector<long> vstep = {step,step,step}, vbstep(3);
1956 for ( i=0; i<3; ++i ) vstep[i] *= 2;
1957 if ( vstep[2] > z ) vstep[2] = z;
1961 time_t t = time(NULL);
1965 for ( i=1; i<bin_level; ++i ) {
1974 long nc, ncp(0), nstop(pb->
image_size()*stop/100);
1975 long bstep = step/bin;
1976 for ( i=0; i<3; ++i ) vbstep[i] = vstep[i]/bin;
1977 if ( vbstep[2] < 1 ) vbstep[2] = 1;
1978 for (
auto it = seg.begin(); it != seg.end(); ++it )
1988 cout <<
"Binning: " << bin << endl;
1989 cout <<
"Iter\tChange\t∆\tTime" << endl;
1992 for ( i=0; i<iterations && nc > nstop; i++ ) {
1995 cout << i+1 <<
tab << nc <<
tab << nc-ncp <<
tab << (long)(time(NULL) - t) << endl;
2005 for ( i=0; i<3; ++i ) {
2007 if (
size()[i] > 1 ) ncp *= 2;
2009 if ( z == 1 ) vbstep[2] = 1;
2010 for (
auto it = seg.begin(); it != seg.end(); ++it )
2047 if ( impose < 1 )
return 0;
2049 cerr <<
"Error in impose_superpixels: No multi-level mask!" << endl;
2053 long i, j, cc, is, isn;
2063 cout <<
"Imposing segments using feature type " << impose << endl;
2066 for ( cc=0; cc<
channels(); cc++, j++ ) {
2067 is = isn = (long)(*pmask)[i];
2068 if ( impose == 2 || impose == 3 ) {
2070 }
else if ( impose == 4 || impose == 5 ) {
2073 if ( impose == 3 || impose == 5 ) {
2074 v = seg[isn].channel(cc) - seg[is].channel(cc);
2075 vv = seg[isn].variance(cc) - seg[is].variance(cc);
2077 v = seg[isn].channel(cc);
2078 vv = seg[isn].variance(cc);