I have a medical brain image from which I'm trying to isolate the brain (the gray pixels) from the rest (black and white parts).
I'm looking for some functions that are best suited for this.
Thanks
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityThis can actually be done quite gracefully. Since it seems like there's no full white within the brain itself, we replace all the White with Black, then use ImageCrop
.
ImageCrop[DeleteSmallComponents@ColorReplace[i, {White -> Black}]]
You can use RemoveBackground
as well if you don't want black in the background.
We use RemoveBackground with a marker in the centre of the image, then crop, delete small leftovers of the borders, crop again, and finally replace transparency with black.
RemoveAlphaChannel[
ImageCrop[
ColorNegate[
DeleteSmallComponents[
ColorNegate[
ImageCrop[
RemoveBackground[i, {"Foreground", ImageDimensions[i]/2}]]]]]],
RGBColor[0.`, 0.`, 0.`]]
Or, since it seems like the image is relatively standard, you can just set given borders for the brain and crop to that every time.
DeleteSmallComponents@Dilation[Binarize[img, {0.4, 0.6}], 1]
perhaps. $\endgroup$