{"id":155,"date":"2010-10-28T21:11:00","date_gmt":"2010-10-28T21:11:00","guid":{"rendered":"http:\/\/brainybehavior.com\/neuroimaging\/?p=155"},"modified":"2010-10-29T00:37:07","modified_gmt":"2010-10-29T00:37:07","slug":"freesurfer-tools-mask-manipulation","status":"publish","type":"post","link":"https:\/\/brainybehavior.com\/neuroimaging\/2010\/10\/freesurfer-tools-mask-manipulation\/","title":{"rendered":"FreeSurfer Tools for Mask Manipulation"},"content":{"rendered":"<p>FreeSurfer includes a useful tool for manipulating mask files. Let&#8217;s say you wanted to pull the left caudate from the aseg.mgz file in order to use it for fiber tracking (that&#8217;s my default example in part because it&#8217;s salient for my research).<\/p>\n<p>I want to be able to work with the FreeSurfer created files in other programs so I&#8217;ll convert to nii.gz.<\/p>\n<p>mri_convert \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/orig.mgz \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/orig.nii.gz<\/p>\n<p>Now I want to convert the aseg.mgz file over. I&#8217;ll use a different method than mri_convert or mri_binarize (although both could work).<\/p>\n<p>mri_label2vol &#8211;subject {subjectID}\u00a0&#8211;seg \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/aseg.mgz &#8211;fillthresh .8 &#8211;identity &#8211;temp \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/orig.mgz &#8211;o \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/aseg2vol.nii.gz<\/p>\n<p>Technically, I didn&#8217;t need the &#8211;subject on there but I like to include it just as a redundancy.<\/p>\n<p>Here&#8217;s the output in ITK-SNAP:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/jt_itksnap_brain_aseg1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-162\" title=\"jt_itksnap_brain_aseg\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/jt_itksnap_brain_aseg1-300x160.png\" alt=\"\" width=\"300\" height=\"160\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/jt_itksnap_brain_aseg1-300x160.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/jt_itksnap_brain_aseg1-1024x546.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>But I don&#8217;t need all of the brain structures, I just want the left caudate (label 11).<\/p>\n<p>mri_binarize &#8211;i \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/aseg2vol.nii.gz &#8211;match 11 &#8211;o \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/left_caudate.nii.gz<\/p>\n<p>You actually can run that command with the basic aseg.mgz (and thus skip the mri_label2vol step above) but I wanted to show both. I&#8217;d recommend using mri_label2vol for binary mask creation but for subcortical structures it shouldn&#8217;t matter. Running this conversion both ways (mri_label2vol and mri_binarize) results in a perfect mask overlap (the blue and red caudates &#8211; one from each method &#8211; appear purple in this screenshot).<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/FSLView-3.1.8-Ortho-view.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-157\" title=\"caudate_conversion_overlap\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/FSLView-3.1.8-Ortho-view-239x300.png\" alt=\"\" width=\"239\" height=\"300\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/FSLView-3.1.8-Ortho-view-239x300.png 239w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/FSLView-3.1.8-Ortho-view-817x1024.png 817w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/FSLView-3.1.8-Ortho-view.png 1116w\" sizes=\"(max-width: 239px) 100vw, 239px\" \/><\/a><\/p>\n<p>Now say you wanted to make a mask a little bit bigger or a little bit smaller (to either pick up surrounding voxels or to exclude part of a mask). You can use mri_binarize to do that (you could even do this with the step above, I&#8217;m separating it out for simplicity):<\/p>\n<p>mri_binarize &#8211;i \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/left_caudate2.nii.gz &#8211;erode 1 &#8211;match 2 &#8211;o \/Applications\/freesurfer\/subjects\/{subjectID}\/mri\/left_caudate3.nii.gz<\/p>\n<p>In that command I input (&#8211;i), erode 1 voxel in 3D (&#8211;erode 1) from the left caudate (&#8211;match 2; that is a 2 because I created this binary mask earlier and gave the left caudate a value of 2), and then specify an output (&#8211;o).<\/p>\n<p>Here you can see the results (visualization in FSLView, which I&#8217;m not overly fond of but it makes viewing mask overlap very easy):<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/caudate_fslview.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-158\" title=\"caudate_fslview\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/caudate_fslview-300x160.png\" alt=\"\" width=\"300\" height=\"160\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/caudate_fslview-300x160.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/10\/caudate_fslview-1024x546.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>As you can see, the eroded caudate is one voxel smaller in 3D than the original caudate.<\/p>\n<p>You can created a summed overlap file (or combine any mask files in the same space together) by using fslmaths (you can also do this with the mri_binarize &#8211;merge option but I&#8217;m just introducing a range of tools):<\/p>\n<p>fslmaths {input file} -add {adding file} {output}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FreeSurfer includes a useful tool for manipulating mask files. Let&#8217;s say you wanted to pull the left caudate from the aseg.mgz file in order to use it for fiber tracking (that&#8217;s my default example in part because it&#8217;s salient for &hellip; <a href=\"https:\/\/brainybehavior.com\/neuroimaging\/2010\/10\/freesurfer-tools-mask-manipulation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,40,13],"tags":[41,67,27,28,43,42],"_links":{"self":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/155"}],"collection":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":5,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/155\/revisions\/160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media\/158"}],"wp:attachment":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}