I am writing a matlab script in order to extract lungs from CT images. In order to remove the background from the image, I need to detect the components that touch the margins of the image and then remove it. How can I perform so once I've found the connected components in the image ?
An example is the image below.

I would like to keep only the inner white part of the image.