开发者

Fixing missing boundaries in an image in MATLAB

开发者 https://www.devze.com 2023-03-25 20:35 出处:网络
Say I have an integer-valued matrix such as the one displayed:                       &nbs

Say I have an integer-valued matrix such as the one displayed:

                            

Fixing missing boundaries in an image in MATLAB

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

Is there an efficient way of adding the missing the dark boundaries in MATLAB? (the white circles show examples of locations where boundaries are missing).

I would like to guarantee that each colored region is fully enclosed by a dark boundary under 4-wise pixel connectivity.

Note that a solution will necessarily flip non-zero values to zero.

The matrix in question is of type uint32 (displayed in color above).

EDIT: The original image is here:

        开发者_运维技巧;                                                                

Fixing missing boundaries in an image in MATLAB


I believe you can get fairly decent results with some simple logic involving shifted versions of your image (made using CIRCSHIFT). Assuming a value of 0 represents the color black, this should work:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消