<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Simple binary image processing
In this tutorial you will learn some simple binary image processing.
In the previous tutorial we learned how to load and save images as well as the simple thresholding operation. This time we will start of with the same image but add 10% of random salt&pepper noise.
Step1:  Using our previous approach of simply thresholding to obtain the brain mask will fail now.
Step2:  What we instead obtain is a rough estimation of the brain mask with noise speckles. First, let's get rid of the small outliers in the background using MedPy's largest_connected_component filter.
Step3:  That already looks better. Note that we could have alternatively used the size_threshold filter, if we had to keep more than a single binary object. Now we can close the inner holes with the help of scipy.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
from medpy.io import load
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
i, h = load("flair.nii.gz")
i[np.random.randint(0, i.shape[0], int(0.05 * i.size)), np.random.randint(0, i.shape[1], int(0.05 * i.size))] = i.min()
i[np.random.randint(0, i.shape[0], int(0.05 * i.size)), np.random.randint(0, i.shape[1], int(0.05 * i.size))] = i.max()
plt.imshow(i, cmap = cm.Greys_r);
Explanation: Simple binary image processing
In this tutorial you will learn some simple binary image processing.
In the previous tutorial we learned how to load and save images as well as the simple thresholding operation. This time we will start of with the same image but add 10% of random salt&pepper noise.
End of explanation
brainmask = i > 0
plt.imshow(brainmask, cmap = cm.Greys_r);
Explanation: Using our previous approach of simply thresholding to obtain the brain mask will fail now.
End of explanation
from medpy.filter import largest_connected_component
brainmask = largest_connected_component(brainmask)
plt.imshow(brainmask, cmap = cm.Greys_r);
Explanation: What we instead obtain is a rough estimation of the brain mask with noise speckles. First, let's get rid of the small outliers in the background using MedPy's largest_connected_component filter.
End of explanation
from scipy.ndimage import binary_fill_holes
brainmask = binary_fill_holes(brainmask)
plt.imshow(brainmask, cmap = cm.Greys_r);
Explanation: That already looks better. Note that we could have alternatively used the size_threshold filter, if we had to keep more than a single binary object. Now we can close the inner holes with the help of scipy.
End of explanation
<END_TASK>