Skip to content

Commit 7a18db2

Browse files
author
Douglas Greve
committed
mri_ca_label. #NF. Added -insert-from-seg to be able to insert/replace some labels (eg, cblum) from a given seg (eg synthseg). Stand-alone option also available
1 parent db2bd8b commit 7a18db2

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

mri_ca_label/mri_ca_label.cpp

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ char *cmdline2, cwd[2000];
246246
char *rusage_file=NULL;
247247
char *PreGibbsFile=NULL;
248248
int n_omp_threads;
249+
MRI *InsertFromSeg=NULL;
250+
std::vector<int> InsertFromSegIndices;
251+
int MRIinsertFromSeg(MRI *mri_labeled, MRI *InsertFromSeg, std::vector<int> InsertFromSegIndices);
249252

250253
int main(int argc, char *argv[])
251254
{
@@ -1413,6 +1416,11 @@ int main(int argc, char *argv[])
14131416
mri_labeled = newseg;
14141417
}
14151418

1419+
if(InsertFromSeg){
1420+
printf("Inserting from seg\n");
1421+
MRIinsertFromSeg(mri_labeled, InsertFromSeg, InsertFromSegIndices);
1422+
}
1423+
14161424
printf("writing labeled volume to %s\n", out_fname) ;
14171425
if (MRIwrite(mri_labeled, out_fname) != NO_ERROR)
14181426
{
@@ -1464,9 +1472,9 @@ int main(int argc, char *argv[])
14641472
seconds = nint((float)msec/1000.0f) ;
14651473
minutes = seconds / 60 ;
14661474
seconds = seconds % 60 ;
1467-
printf("auto-labeling took %d minutes and %d seconds.\n",
1468-
minutes, seconds) ;
1469-
return(0) ;
1475+
printf("mri_ca_label took %d minutes and %d seconds.\n", minutes, seconds) ;
1476+
printf("mri_ca_label done\n");
1477+
exit(0);
14701478
}
14711479
/*----------------------------------------------------------------------
14721480
Parameters:
@@ -1546,6 +1554,43 @@ get_option(int argc, char *argv[])
15461554
nowmsa = 1 ;
15471555
printf("disabling WMSA labels\n") ;
15481556
}
1557+
else if(!stricmp(option, "insert-from-seg") || !stricmp(option, "sa-insert-from-seg"))
1558+
{
1559+
// -insert-from-seg InserFromSeg.mgz index1 <index2 ...>
1560+
// -sa-insert-from-seg InserFromSeg.mgz index1 <index2 ...> InputSeg OutputSeg
1561+
InsertFromSeg = MRIread(argv[2]);
1562+
if(!InsertFromSeg) exit(1);
1563+
printf("Inserting from seg %s ",argv[2]);
1564+
nargs = 1;
1565+
int k=3;
1566+
while(1){
1567+
if(!argv[k]) break;
1568+
if(!isdigit(argv[k][0])) break;
1569+
InsertFromSegIndices.push_back(atoi(argv[k]));
1570+
printf("%s ",argv[k]);
1571+
k++;
1572+
nargs++;
1573+
}
1574+
printf("\n");
1575+
if(InsertFromSegIndices.size()==0) {
1576+
printf("ERROR: -insert-from-seg needs at least one index to insert\n");
1577+
exit(1);
1578+
}
1579+
MRI *InputSeg=NULL;
1580+
if(!stricmp(option, "sa-insert-from-seg")){
1581+
if(argv[k]==NULL || argv[k+1]==NULL){
1582+
printf("ERROR: -sa-insert-from-seg needs an input and output seg\n");
1583+
exit(1);
1584+
}
1585+
InputSeg = MRIread(argv[k]);
1586+
if(!InputSeg) exit(1);
1587+
printf("Inserting from seg\n");
1588+
MRIinsertFromSeg(InputSeg, InsertFromSeg, InsertFromSegIndices);
1589+
int err = MRIwrite(InputSeg,argv[k+1]);
1590+
exit(err);
1591+
}
1592+
printf("\n") ;
1593+
}
15491594
else if (!stricmp(option, "insert-wm-bet-putctx")){
15501595
sscanf(argv[2],"%d",&insert_wm_bet_putctx_topo);
15511596
nargs = 1 ;
@@ -5382,3 +5427,50 @@ MRI *insert_wm_bet_putctx(MRI *seg, int topo, const char *psfile, MRI *out)
53825427
fflush(stdout);
53835428
return(out);
53845429
}
5430+
5431+
5432+
int MRIinsertFromSeg(MRI *mri_labeled, MRI *InsertFromSeg, std::vector<int> InsertFromSegIndices)
5433+
{
5434+
int nchanged=0;
5435+
for(int c=0; c < mri_labeled->width; c++){
5436+
for(int r=0; r < mri_labeled->height; r++){
5437+
for(int s=0; s < mri_labeled->depth; s++){
5438+
// Get the indices of this voxel for both segs
5439+
int i1 = MRIgetVoxVal(mri_labeled,c,r,s,0);
5440+
int i2 = MRIgetVoxVal(InsertFromSeg,c,r,s,0);
5441+
5442+
if(i1 == i2) continue; // both the same so keep as is
5443+
5444+
// Check whether i1, i2 are in the list
5445+
int i1InList=0, i2InList=0;
5446+
for(int n=0; n < InsertFromSegIndices.size(); n++){
5447+
if(i1==InsertFromSegIndices[n]){
5448+
i1InList = 1;
5449+
break;
5450+
}
5451+
}
5452+
for(int n=0; n < InsertFromSegIndices.size(); n++){
5453+
if(i2==InsertFromSegIndices[n]){
5454+
i2InList = 1;
5455+
break;
5456+
}
5457+
}
5458+
// Neither are in the list, so keep as is
5459+
if(!i1InList && !i2InList) continue;
5460+
5461+
// At this point, at least one of them is in the list, so
5462+
// replace it. This will change things on the boundary. Eg, if
5463+
// i1 is cblum wm and i2 is brainstem, then i1 will become
5464+
// brainstem eventhough brainstem is not in the list. This is
5465+
// probably ok for the main application (replacing aseg cblum
5466+
// with synthseg cblum), but I worry a little bit about causing
5467+
// some artifacts near the boundaries.
5468+
MRIsetVoxVal(mri_labeled,c,r,s,0,i2);
5469+
nchanged++;
5470+
5471+
}
5472+
}
5473+
}
5474+
printf("MRIinsertFromSeg() changed %d\n",nchanged);
5475+
return(nchanged);
5476+
}

mri_ca_label/mri_ca_label.help.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
<explanation>Insert WM between putamen and cortex (replacing putamen)</explanation>
126126
<argument>-sa-insert-wm-bet-putctx inputseg topo outputseg</argument>
127127
<explanation>Stand-alone operation</explanation>
128+
<argument>-insert-from-seg segvol index1 index2 ...</argument>
129+
<explanation>Insert the given indices from segvol into the output seg</explanation>
130+
<argument>-sa-insert-from-seg segvol index1 index2 ... inseg outseg</argument>
131+
<explanation>Stand-alone insert the given indices from segvol into the input seg and save as outseg</explanation>
128132
<argument>-threads or -nthreads NTHREADS</argument>
129133
<explanation>Set the number of open mp threads</explanation>
130134
</optional-flagged>

0 commit comments

Comments
 (0)