简体   繁体   中英

How do I remove/replace border pixels from my image?

I am new to Matlab and will need some help. I have a segmented image in which I would want to remove background (or black) pixels (with value == 1) from the image borders. I have been able to obtain an image mask of the border pixels that I do not want. The interior_blackPixels are useful but I want to get rid of the outer_blackPixels, however, I am not sure how to retrieve the final image without the black pixel borders using the mask (outer_blackPixels) that I have. The code so far is shown below:

img =   [1 1 1 1 1 1 1 1
         1 1 1 1 2 2 2 1
         1 1 1 2 2 2 2 1
         1 1 1 2 2 2 2 1
         1 1 2 2 2 2 2 1
         1 1 2 2 2 2 2 1
         1 3 3 1 1 1 3 1
         1 3 3 1 1 1 3 1
         1 3 3 3 3 3 3 1
         1 1 1 1 1 1 1 1];

% Get the black pixels image array
blackPixels = (img == 1);

% Obtain the other pixels by negating the black pixels
otherPixels = ~blackPixels

% Get the border black pixels (or mask)
outer_blackPixels    = blackPixels & ~imclearborder(blackPixels)
interior_blackPixels = blackPixels & ~outer_blackPixels

Please note that I do not mind replacing the pixel values of the outer_blackPixels to '0' as this will not affect my analysis. Hence, I would expect my final image to be something like this:

img =   [0 0 0 0 0 0 0 0
         0 0 0 0 2 2 2 0
         0 0 0 2 2 2 2 0
         0 0 0 2 2 2 2 0
         0 0 2 2 2 2 2 0
         0 0 2 2 2 2 2 0
         0 3 3 1 1 1 3 0
         0 3 3 1 1 1 3 0
         0 3 3 3 3 3 3 0
         0 0 0 0 0 0 0 0];

Any help/suggestions would be appreciated. Thanks!

You can use bwlabel to differentiate between the 1's areas. Because this function treats only binary images, you can use logical to convert img to binary:

L=bwlabel(~logical(img-1),4)

And then just convert the borders to 0:

img(L==L(1))=0

img =

 0     0     0     0     0     0     0     0
 0     0     0     0     2     2     2     0
 0     0     0     2     2     2     2     0
 0     0     0     2     2     2     2     0
 0     0     2     2     2     2     2     0
 0     0     2     2     2     2     2     0
 0     3     3     1     1     1     3     0
 0     3     3     1     1     1     3     0
 0     3     3     3     3     3     3     0
 0     0     0     0     0     0     0     0

Since you are using the Image Processing Toolbox, I would suggest the imfill function:

img(~imfill(img ~= 1, 'holes')) = 0;

This first creates a mask for pixels in img that are not equal to one, fills any internal holes that contain ones using imfill , negates that to get a mask for the border ones, and indexes img with that mask, setting values to zero.

If you want to do something like this without using the Image Processing Toolbox, a solution of the type from Adiel using bwlabel is the way I would go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM