MATLAB: How to remove the background from a binarized image of which I selected a specific ROI

image analysisimage processingImage Processing Toolboximage segmentationMATLAB and Simulink Student Suite

Hi everyone.
In order to conduct an Object analysis from a binary source image (white=main matrix, black=background and pores)
I had to inverse the colours of the image by applying the imcomplement function and then select a Region of Interest (ROI).
From that, as a final image I'd need a binary ROI image with: white=pores, black=main matrix and background, so basically I would like to remove the white background obtained with the imcomplement function.
NOTE: I already tried to select the ROI before and then imcomplement it but it doesn't work because once created, the ROI-image is imrect type instead of logical as it is required by imcomplement.
Attached there are: the source binarized image, an example of ROI-shaped image with inverted colours.
I hope someone can help me to sort this out ASAP. Thanks in advance.
Serena.

Best Answer

I'm confused. What is the "main matrix" part of the image? And what are pores and what is background? The images look the same, just cropped. I don't see any inverting nor do I see why it's needed.
I'll assume you want the white blobs but not the white circular surround. So simply use a circular template mask to get rid of it:
binaryImage(circularMask) = false;
or call imclearborder() to get rid of the circular surround
binaryImage = imclearborder(binaryImage);
imclearborder will also remove and partial blobs crossing the circular border, which is not a bad thing - just depends if you want area fraction and partial blobs are okay, or if you need complete blobs for your computations.
Then call imrect() or rbbox() to define the cropping rectangle, call imcrop() or use indexing to actually make the crop, and you're done.