Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing missing boundaries in an image in MATLAB

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

                             color-coded image

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:

                                                                        enter image description here

like image 686
Amelio Vazquez-Reina Avatar asked Aug 03 '11 18:08

Amelio Vazquez-Reina


People also ask

How do I trace boundaries in Matlab?

Call bwtraceboundary to trace the boundary from the specified point. As required arguments, you must specify a binary image, the row and column coordinates of the starting point, and the direction of the first step. The example specifies north ( 'N' ). boundary = bwtraceboundary(BW,[row, col],'N');

How do I fill a hole in an image in Matlab?

BW2 = imfill( BW ,'holes') fills holes in the input binary image BW . In this syntax, a hole is a set of background pixels that cannot be reached by filling in the background from the edge of the image. BW2 = imfill( BW , conn ,'holes') fills holes in the binary image BW , where conn specifies the connectivity.

What is boundary extraction in image processing?

Boundary extraction is a process to extract the boundary of a sample curve or an image. In this project, some modification is made to the algorithm from Eddins (2006) to extract the boundary. This algorithm is chose because it can extract the boundary properly and the result can be used to extract the data.

What is Bwareaopen in Matlab?

BW2 = bwareaopen( BW , P ) removes all connected components (objects) that have fewer than P pixels from the binary image BW , producing another binary image, BW2 . This operation is known as an area opening.


1 Answers

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
like image 181
gnovice Avatar answered Sep 24 '22 12:09

gnovice