First an opening by reconstruction is applied on the original image using its border as marker. This reconstructs all components that touches the border. To take this result off, it is subtracted from the original image.
The input image and its border are shown in figures 1 and 2 respectively. These images will be the inputs for the opening by reconstruction. Figure 3 shows the result of the opening by reconstruction and figure 4 shows the result of its subtraction from the original image.
fig. 1 - Original Image
fig. 2 - Marker Image
fig. 3 - Opening by Reconstruction
fig. 4 - Final Result