Although prior information is used to restrict segmentation to the cortex there are still some misclassifications mainly at the border between gray matter and CSF. These rims can be tried to remove using morphological operations. Conditional dilations and erosions based on gray and white matter segmentations are used to create a mask to clean up the segmentations. It begins by taking the white matter, and eroding it a couple of times to get rid of any odd voxels. The algorithm continues on to do conditional dilations for several iterations, where the condition is based upon gray or white matter being present. This identified region is then used to clean up the grey and white matter partitions, and has a slight influences on the CSF partition.