Say I have an integer-valued matrix such as the one displayed:
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:
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');
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With