Fix missing borders in an image in MATLAB

Say that I have an integer matrix such as a mapped one:

                            color-coded image

In the image above, the dark borders are represented by the number 0 and are one pixel wide (please ignore the scaling artifacts).

Is there an efficient way to add missing dark borders in MATLAB? (white circles show examples of places where there are no borders).

I would like to ensure that each color area is completely covered by a dark border with a 4- pixel pixel connection.

Note that the solution will surely flip nonzero values ​​to zero.

The corresponding matrix is ​​of type uint32 (displayed in the color above).

EDIT: :

                                                                        enter image description here

+5
1

, , ( CIRCSHIFT). , 0 , :

rawImage = ...;                            %# Your starting image
shiftedImage = circshift(rawImage,1);      %# Shift image down one row
index = (rawImage ~= shiftedImage) & ...   %# A logical matrix with ones where
        rawImage & shiftedImage;           %#   up-down neighbors differ and
                                           %#   neither is black
rawImage(index) = 0;                       %# Set those pixels to black
shiftedImage = circshift(rawImage,[0 1]);  %# Shift image right one column
index = (rawImage ~= shiftedImage) & ...   %# A logical matrix with ones where
        rawImage & shiftedImage;           %#   left-right neighbors differ and
                                           %#   neither is black
rawImage(index) = 0;                       %# Set those pixels to black
+7

All Articles