How do I remove/replace border pixels from my image?
Show older comments
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. 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:
% set all the border pixels to zeros
new_borderValues = img(outer_blackPixels) == 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];
The main challenge for me however is that I am not sure how to retrieve the final image wit the black pixel borders replaced (as shown above) using the mask (outer_blackPixels) that I have. Any help/suggestions would be appreciated. Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Images in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!