Skip to content

Commit

Permalink
mri_mask.cpp. #NF. Added ability to invert mask before applying it
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Greve committed Dec 6, 2021
1 parent 8332148 commit 214ad4a
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions mri_mask/mri_mask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ static float transfer_val;
static int keep_mask_deletion_edits = 0; // if 1, keep mask voxels with value=1
int DoAbs = 0;
int DoBB = 0, nPadBB[6];
double bgnoisescale = 0;
int bgnoisesign=0;
int InvertMask = 0;

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -120,6 +123,32 @@ int main(int argc, char *argv[])
ErrorExit(ERROR_BADPARM, "%s: could not read mask volume %s",
Progname, argv[2]) ;

if(InvertMask){
double thresh = threshold;
if(! ThresholdSet) thresh = 0.5;
printf("Inverting and binarizing mask thresh = %g\n",thresh);
int c,count=0;
for(c=0; c < mri_mask->width; c++){
int r,s;
double v;
for(r=0; r < mri_mask->height; r++){
for(s=0; s < mri_mask->depth; s++){
v = MRIgetVoxVal(mri_mask,c,r,s,0);
if(DoAbs) v = fabs(v);
if(v < thresh) {
MRIsetVoxVal(mri_mask,c,r,s,0,1);
count++;
}
else MRIsetVoxVal(mri_mask,c,r,s,0,0);
}
}
}
printf("count %d\n",count);
printf("Resetting threshold to 0.5\n");
threshold = 0.5;
ThresholdSet = 1;
}

printf("DoAbs = %d\n",DoAbs);

/* Read transform and apply it to mri_mask */
Expand Down Expand Up @@ -280,6 +309,7 @@ int main(int argc, char *argv[])
}
}
}

if(DoBB){
printf("Computing bounding box, npad = %d, %d, %d, %d, %d, %d\n",
nPadBB[0],nPadBB[1],nPadBB[2],nPadBB[3],nPadBB[4],nPadBB[5]);
Expand All @@ -296,26 +326,22 @@ int main(int argc, char *argv[])
}

int mask=0;
if (do_transfer)
{
if (do_transfer) {
mask = (int)transfer_val;
out_val = transfer_val;
}
if (dilate > 0)
{
if (dilate > 0) {
int i ;
for (i = 0 ; i < dilate ; i++)
MRIdilate(mri_mask, mri_mask);
}

mri_out = MRImask(mri_in, mri_mask, NULL, mask, out_val) ;
if (!mri_out)
{
if (!mri_out) {
ErrorExit(Gerror, "%s: stripping failed", Progname) ;
}

if (keep_mask_deletion_edits)
{
if (keep_mask_deletion_edits) {
MRI *mri_tmp ;
mri_tmp = MRImask(mri_out, mri_mask_orig, NULL, 1, 1) ; // keep voxels = 1
if (!mri_tmp)
Expand All @@ -324,6 +350,23 @@ int main(int argc, char *argv[])
MRIfree(&mri_out) ; mri_out = mri_tmp ;
}

if(bgnoisescale > 0){
printf("Adding background noise %g %d\n",bgnoisescale,bgnoisesign);
for (z = 0 ; z <mri_mask->depth ; z++) {
for (y = 0 ; y < mri_mask->height ; y++) {
for (x = 0 ; x < mri_mask->width ; x++) {
int m = (int)MRIgetVoxVal(mri_mask, x, y, z, 0);
if(m) continue;
double v = (drand48() - bgnoisesign/2.0)*bgnoisescale;
MRIsetVoxVal(mri_out, x, y, z, 0, v) ;

}
}
}


}

printf("Writing masked volume to %s...", argv[3]) ;
MRIwrite(mri_out, argv[3]);
printf("done.\n") ;
Expand Down Expand Up @@ -377,6 +420,14 @@ get_option(int argc, char *argv[])
{
DoAbs = 1;
}
else if (!stricmp(option, "invert"))
{
InvertMask = 1;
}
else if (!stricmp(option, "no-invert"))
{
InvertMask = 0;
}
else if (!stricmp(option, "lh"))
{
DoLH = 1;
Expand Down Expand Up @@ -506,6 +557,11 @@ get_option(int argc, char *argv[])
nargs = 3 ;
printf("debugging node (%d, %d, %d)\n", Gx,Gy,Gz) ;
}
else if (!stricmp(option, "bgnoise")){
sscanf(argv[2],"%lf",&bgnoisescale);
sscanf(argv[3],"%d",&bgnoisesign);
nargs = 2;
}
else
{
fprintf(stderr, "unknown option %s\n", argv[1]) ;
Expand Down

0 comments on commit 214ad4a

Please sign in to comment.