@@ -555,6 +555,10 @@ staticx int nifti_close(nifti_image *nim, flt iso, flt dx1, flt dx2) {
555555 int nVol = nim -> nvox / nvox3D ;
556556 if (nVol != 1 )
557557 return 1 ;
558+ if (dx1 <= 0.0 ) {
559+ printfx ("close function requires positive dilation (but erode can be 0).\n" );
560+ return EXIT_FAILURE ;
561+ }
558562 flt * imgIn = (flt * )_mm_malloc (nvox3D * sizeof (flt ), 64 ); //alloc for each volume to allow openmp
559563 flt * img = (flt * )nim -> data ;
560564 memcpy (imgIn , img , nvox3D * sizeof (float ));
@@ -566,7 +570,7 @@ staticx int nifti_close(nifti_image *nim, flt iso, flt dx1, flt dx2) {
566570 else
567571 img [i ] = 1 ;
568572 }
569- //step2 apply distance transform
573+ //step2 apply distance transform to DILATE (required)
570574 // -edt
571575 nifti_edt (nim );
572576 //step 3: threshold and make invert binary
@@ -577,22 +581,24 @@ staticx int nifti_close(nifti_image *nim, flt iso, flt dx1, flt dx2) {
577581 else
578582 img [i ] = 1 ;
579583 }
580- //step4 apply distance transform
584+ //step4 apply distance transform to ERODE (optional)
581585 // -edt
582- nifti_edt (nim );
583- //step 5: threshold and make binary, set surviving voxels to iso
584- // -thr $dx2 -bin -mul $iso
585- for (size_t i = 0 ; i < nim -> nvox ; i ++ ) {
586- if (img [i ] < dx2 )
587- img [i ] = 0 ;
588- else
589- img [i ] = iso ;
586+ if (dx2 > 0.0 ) {
587+ nifti_edt (nim );
588+ //step 5: threshold and make binary, set surviving voxels to iso
589+ // -thr $dx2 -bin -mul $iso
590+ for (size_t i = 0 ; i < nim -> nvox ; i ++ ) {
591+ if (img [i ] < dx2 )
592+ img [i ] = 0 ;
593+ //else
594+ // img[i] = iso;
595+ }
590596 }
591597 //step 6 - voxel is brightest of original and mask
592598 // -max scalar
593599 for (size_t i = 0 ; i < nim -> nvox ; i ++ ) {
594600 if (img [i ] > 0 )
595- img [i ] = fmax (img [ i ] , imgIn [i ]);
601+ img [i ] = fmax (iso , imgIn [i ]);
596602 else
597603 img [i ] = imgIn [i ];
598604 }
@@ -620,7 +626,7 @@ staticx int nifti_hollow(nifti_image *nim, flt threshold, flt wallThickness) {
620626 img [i ] = 0 ;
621627 }
622628 if (nThresh < 1 ) {
623- printf ("Hollow error, no voxels survive threshold %g\n" , threshold );
629+ printfx ("Hollow error, no voxels survive threshold %g\n" , threshold );
624630 return 1 ;
625631 }
626632 #ifdef NII2MESH
@@ -642,7 +648,7 @@ staticx int nifti_hollow(nifti_image *nim, flt threshold, flt wallThickness) {
642648 }
643649 }
644650 if (mx <= thick ) {
645- printf ("Hollow error, no surface deeper than %g (deepest point %g)\n" , thick , mx );
651+ printfx ("Hollow error, no surface deeper than %g (deepest point %g)\n" , thick , mx );
646652 return 1 ;
647653 }
648654 for (size_t i = 0 ; i < nim -> nvox ; i ++ ) {
0 commit comments