{"id":44,"date":"2010-06-04T01:28:29","date_gmt":"2010-06-04T01:28:29","guid":{"rendered":"http:\/\/brainybehavior.com\/neuroimaging\/?p=44"},"modified":"2010-09-28T00:49:18","modified_gmt":"2010-09-28T00:49:18","slug":"converting-freesurfer-labels-to-masks","status":"publish","type":"post","link":"https:\/\/brainybehavior.com\/neuroimaging\/2010\/06\/converting-freesurfer-labels-to-masks\/","title":{"rendered":"Converting FreeSufer aseg Labels to Individual Volume Masks"},"content":{"rendered":"<p><strong>Note: There are faster (and better) methods than this for creating volumetric masks from aseg labels. I wrote this guide in part to demonstrate some of the features of multiple imaging programs. See <\/strong><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/2010\/06\/saving-individual-labels-from-freesurfers-aseg\/\"><strong>this post<\/strong><\/a><strong> for a faster method. You could also use the FreeSurfer command: mri_binarize. That will be a topic for a future post.<\/strong><\/p>\n<p>Say you wanted to pull an ROI (region of interest: brain structure mask) from FreeSurfer to use for fiber tracking or some other purpose (including volumetric analyses of subcortical brain structures).<\/p>\n<p>Navigate to your FreeSurfer subjects directory using the command line (e.g., <em>cd \/Applications\/freesurfer\/subjects\/<\/em>). I\u2019ll use subject S001 as an example.<\/p>\n<p>Change into the participant\u2019s mri directory: <em>cd S001\/mri<\/em><\/p>\n<p>List (ls) the contents of the directory. The file you most likely want for subcortical structures is called aseg.mgz.<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/freesurfer_terminal_aseg-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-45\" title=\"freesurfer_terminal_aseg\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/freesurfer_terminal_aseg-1-300x165.png\" alt=\"\" width=\"300\" height=\"165\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/freesurfer_terminal_aseg-1-300x165.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/freesurfer_terminal_aseg-1.png 724w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If you want to see what this file looks like in FreeSurfer, enter the following command into Terminal (again this is using S001 as an example; you can also run this command from anywhere; in other words, you do not have to be in any particular location in your computer directories):<\/p>\n<p><em>tkmedit S001 norm.mgz \u2013aseg<\/em><\/p>\n<p><em><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/tkmedit_norm_aseg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46\" title=\"tkmedit_norm_aseg\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/tkmedit_norm_aseg.png\" alt=\"\" width=\"337\" height=\"424\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/tkmedit_norm_aseg.png 337w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/tkmedit_norm_aseg-238x300.png 238w\" sizes=\"(max-width: 337px) 100vw, 337px\" \/><\/a><\/em><\/p>\n<p>Now say you want the caudates to use as ROIs for fiber tracking or for some other use. You need to get the volumes into diffusion space. However, I want to give an example of transforming the segmentations into native space first.\u00a0<strong>Note: getting the aseg.mgz file into native (T1) space is only important if you want to manually clean the segmentations<\/strong> <strong>or if you want to use FLIRT to register them to diffusion space <\/strong>(i.e., if you want to manually modify them using ITK-SNAP or some other tool like that). [As an additional note: I&#8217;ve had better success with ROIs using FLIRT (part of FSL) to register them into diffusion space than using mri_convert (part of FreeSurfer). Using FLIRT and FNIRT &#8211; tools for linear and nonlinear registration, respectively &#8211; will be a future guide]. Use the following command:<\/p>\n<p><em>mri_convert -rl rawavg.mgz -rt nearest aseg.mgz aseg2raw.nii.gz<\/em><\/p>\n<p>That will use FreeSurfer\u2019s mri_convert to reslice aseg.mgz (the automatic segmentation file) like (-rl) a native space volume (rawavg.mgz) and save it as aseg2raw.nii.gz. It is important when dealing with mask files like aseg.mgz, to use the \u2013rt option and set it as nearest (which uses a nearest neighbor resampling and should keep all numerical values in the aseg.mgz value integers. This is important for future processing to run smoothly.<\/p>\n<p>However, if you think that FreeSurfer did a good job (it usually does) with the structures and you just want to use the raw FreeSurfer mask for fiber tracking, you could directly convert the aseg.mgz file into diffusion space without going to T1 space first (this is not my preferred method but it will work for this guide).<\/p>\n<p>In order to do this you can run the mri_convert command but reslicing like one of your diffusion images (such as the nodif.nii.gz file you created with FSL).<\/p>\n<p>For example:<\/p>\n<p><em>mri_convert -rl [path to subject]\/DATA\/nodif.nii.gz -rt nearest aseg.mgz aseg2dif.nii.gz<\/em><\/p>\n<p>Now you can check the transformed file in <a href=\"http:\/\/www.itksnap.org\/\">ITK-SNAP<\/a> (version 2.0.0 was used for this pipeline). Open ITK-SNAP and load the nodif.nii.gz file (File &gt;&gt; Open Grayscale Image &gt;&gt; Browse (or type in the path to the file: e.g., \/Users\/Shared\/diffusion\/subjects\/S001\/DATA\/nodif.nii.gz) &gt;&gt; Next &gt;&gt; Finish).<\/p>\n<p>The image should load:<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_nodif.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-47\" title=\"itksnap_nodif\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_nodif-300x263.png\" alt=\"\" width=\"300\" height=\"263\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_nodif-300x263.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_nodif.png 627w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then load the segmentation image (Segmentation &gt;&gt; Load From Image &gt;&gt; Browse\u2026 (or type in the path to the aseg2dif.nii.gz image: e.g., \/Applications\/freesurfer\/subjects\/S001\/mri\/aseg2dif.nii.gz) &gt;&gt; Next &gt;&gt; Finish)<\/p>\n<p>If you get an error message of any type you either selected the wrong image or you ran the transform (using mri_convert) incorrectly. It should load just fine if you did everything correctly.<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/nodif_aseg_itksnap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-48\" title=\"nodif_aseg_itksnap\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/nodif_aseg_itksnap-300x263.png\" alt=\"\" width=\"300\" height=\"263\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/nodif_aseg_itksnap-300x263.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/nodif_aseg_itksnap.png 627w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Notice all the different colors\/labels. The names are not associated with the labels but each structure has a different label number. If you know the number of the structure(s) you want, you can proceed. If you don\u2019t know the numbers it is easy to find what you want in ITK-SNAP. Navigate through the brain until you find the structure(s) you want. In this case I want the left and right caudates \u2013 I\u2019ll work with them separately though. The left hemisphere should be on the right \u2013 this is radiological orientation (for the axial slices picture a person lying on their back on a table and you are looking up their nostrils. Their left is on the right to you. For the coronal slices just pretend you are looking the person in the face).<\/p>\n<p>Note: there is a faster (but not graphical) way to pull out these labels. Read my post <a href=\"http:\/\/brainybehavior.com\/neuroimaging\/2010\/06\/saving-individual-labels-from-freesurfers-aseg\/\">here<\/a>.<\/p>\n<p>Notice in the screenshot that the image pointers are in the left caudate; also notice the label number.<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/aseg2dif_itksnap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-49\" title=\"aseg2dif_itksnap\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/aseg2dif_itksnap-300x261.png\" alt=\"\" width=\"300\" height=\"261\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/aseg2dif_itksnap-300x261.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/aseg2dif_itksnap.png 632w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Make note of the label number (11 in this instance). This is the number FreeSurfer (at least with version 4.5) assigns all left caudates. The label number for the right caudate is 50.<\/p>\n<p>Now click on the label editor button in ITK-SNAP (see above screenshot). Delete all labels except the one(s) you want. I think it is easiest to do left and right sides at different times (i.e., do one side first, then reload the base image and aseg2dif segmentation and pull out the other structure(s) you want).<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/ase2dif_itksnap_labels.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-50\" title=\"ase2dif_itksnap_labels\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/ase2dif_itksnap_labels-300x258.png\" alt=\"\" width=\"300\" height=\"258\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/ase2dif_itksnap_labels-300x258.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/ase2dif_itksnap_labels.png 723w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>When you have deleted the labels your screen should only have one left:<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/labels_itksnap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-52\" title=\"labels_itksnap\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/labels_itksnap-300x259.png\" alt=\"\" width=\"300\" height=\"259\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/labels_itksnap-300x259.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/labels_itksnap.png 723w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now you can rename the label (e.g., Left Caudate) if you want to but you do not have to.<\/p>\n<p><strong>Important<\/strong>: Click on the Label Id tab and change the label to 1 (and click set).<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_change_label_ID.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-53\" title=\"itksnap_change_label_ID\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_change_label_ID-300x259.png\" alt=\"\" width=\"300\" height=\"259\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_change_label_ID-300x259.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_change_label_ID.png 723w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You are creating a binary mask file for this structure. If you do not change the label ID for the structure you want, you will have a binary file with zeros and some other number instead of just zeros and ones. Many imaging programs that deal with the masks require them to be only zeros and ones.<\/p>\n<p>Close the label editor menu and verify that you have the structure you want. You can render it in 3D if you want. See screenshot below. If you like how the caudate looks and think it was a reasonable registration and transformation into diffusion space, then you can proceed. Otherwise, <strong>and only if you know what you are doing<\/strong>, you can manually edit the caudate a bit should it need some cleaning. It is not recommended that you do much or any editing based on the diffusion image. If the structure is really off then edit it in T1 space. This is one reason I recommend working in T1 space first and then FLIRTing into diffusion space.<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_caudate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-54\" title=\"itksnap_caudate\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_caudate-300x263.png\" alt=\"\" width=\"300\" height=\"263\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_caudate-300x263.png 300w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/06\/itksnap_caudate.png 723w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>OK, I like this caudate (at least enough for this example) so now I\u2019m ready to export it. Go to Segmentation &gt;&gt; Save As Image &gt;&gt; [then specify a path and file name for the mask; e.g., \/Users\/Shared\/diffusion\/subjects\/S001\/ROIS\/S001_l_caudate_from_freesurfer_diffusion.nii.gz) &gt;&gt; Save<\/p>\n<p>Now I repeat the process for other structures I want. In this case, the right caudate (label 50). Go to File &gt;&gt; Unload All Images (cmd+u). Your ITK-SNAP will go blank. Reload the base nodif.nii.gz image (it\u2019s fastest to go to File &gt;&gt; Open Recent). Load in the segmentation (aseg2dif.nii.gz): Segmentation &gt;&gt; Load From Image &gt;&gt; History &gt;&gt; [image] (it will be in the history unless you\u2019ve loaded a bunch of segmentations in the meantime). Delete the labels you do not want, change the label to 1, and then save it out (if you like the segmentation and the registration). It is best to keep your naming convention consistent.<\/p>\n<p>That&#8217;s it! You now have FreeSurfer structures saved out as volumetric masks using ITK-SNAP (which allows easy visualization and cleaning).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: There are faster (and better) methods than this for creating volumetric masks from aseg labels. I wrote this guide in part to demonstrate some of the features of multiple imaging programs. See this post for a faster method. You &hellip; <a href=\"https:\/\/brainybehavior.com\/neuroimaging\/2010\/06\/converting-freesurfer-labels-to-masks\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":46,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,13],"tags":[21,22,67,68,19,24,20,23],"_links":{"self":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/44"}],"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=44"}],"version-history":[{"count":9,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/44\/revisions\/59"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media\/46"}],"wp:attachment":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}