Matlab: Segmentation Growing

Segmentation Growing

(German) This post is about a methode of segmentation where a region is defined (seed) . In this seed the standard deviation and the mean is calculated and with a factor f it is weigthed. Pixel which are in the neighborhood of the seed are added to the seed if their value is in the boundary defined by the standard deviation, mean value and weighting factor f. This is done iterative as long as no more pixel is added to the seed. Then the object is segmented.

function [region, area, seed, sx1, sx2, sy1, sy2] = Segmentation_Growing(img,sx1,sx2,sy1,sy2,f):

This function needs an image as an imput. If there is no other input value one can choose with to cursors the starting point sx1, sy1, the left upper corner, and the end point sx2, sy2, the right lower corner. Otherwise one can define the s-value as one wants. If one uses cursors, one can use the second input argument for defining the factor f. This factor describes the weigthing and is in an area bigger equal to 0. This is shown in the following figure below for f=1 (blue), f=1.5 (yellow) and f=2 (green). The red rect shows the chosen seed.
The output of the function is region, which is the segmented object. Area describes the segmented area with value 1 and the rest as 0. Seed logically returns seed, if it is chosen by the cursors. 

In the following figure there are 3 different segmented regions for 3 different f-factors. The figure in the right lower corner shows the window where one sets the cursor.

Here one can download the function from MathWorks or you can copy it from the following source code:

Source Code:

function [region, area, seed, sx1, sx2, sy1, sy2] = Segmentation_Growing(img,sx1,sx2,sy1,sy2,f)
%img is the image which is segmented
%sx1 and sy1 are the values of the upper left corner and
%sx2 and sy2 are the values of the lower right corner
%f can vary between 1 and 2 and is the weigthing factor
%region is the output of the segmented region
%if area is in region it's one otherwise it's 0
%seed is the start area

%definition of f if not already defined
if nargin < 6
    f = 1.5;
end
if nargin == 2
    f = sx1;
end

% initialize the the region of interest by to cursors
if nargin < 3
    fig(1) = figure(1);
    clf(1);
    imshow(img);
    dcm_obj = datacursormode(fig(1));
    set(dcm_obj,'DisplayStyle','datatip','SnapToDataVertex','off','Enable','on')
    disp('Click for left upper corner of region of interest')
    pause
    pos1 = getCursorInfo(dcm_obj);
    pos = pos1.Position;
    sx1 = pos(2);
    sy1 = pos(1);
    disp('Click for right lower corner of region of interest')
    pause
    pos2 = getCursorInfo(dcm_obj);
    pos = pos2.Position;
    sx2 = pos(2);
    sy2 = pos(1);
    close(fig(1));
end

%generate the seed;
seed = img(sx1:sx2, sy1:sy2);
n = size(img);
% all non segmented regions are infinity
region = ones(n)*inf;
region(sx1:sx2, sy1:sy2) = img(sx1:sx2, sy1:sy2);

region2 = ones(n);
% end condition if region in 2 following steps are equal
tf = isequal(region2,region);
q = double(tf);
while q < 1
    %calculate the mean and standard deviation of all non-inf elements
    rv = region(:);
    vec = rv(rv~=inf);
    m = mean(vec);
    s = std(vec);
    %find all elements in region and sum over them
    [i1,j1,v1] = find(region~=inf);
    for r=1:1:numel(i1)
        i = i1(r);
        j = j1(r);
        %sum over all neighboors
        for k=-1:1:1
            for l=-1:1:1
                if i+k >= n(1) || j+l >= n(2) || i+k <= 0 || j+l <= 0
                    continue
                end
                %include all outside particles which fullfill condition
                if region(i+k,j+l) ~= inf
             
                else
                    if img(i+k,j+l) > m-f*s && img(i+k,j+l) <m+f*s
                        region(i+k,j+l) = img(i+k,j+l);
                    end
                end
            end
        end
    end
    % ending condition
    tf = isequal(region2,region);
    q = double(tf);
    region2 = region;
end
% define the area which is segmented as 1, otherwise 0
area = region;
area(region~=inf) = 1;
area(area~=1) = 0;


end


Source Code Application:

 clear;
clc;

img = imread('onion.png');
img = im2double(img);
img = rgb2gray(img);
%a)

[region,area,seed,sx1,sx2,sy1,sy2] = Segmentation_Growing(img,1);
[region2,area2] = Segmentation_Growing(img,sx1,sx2,sy1,sy2,1.5);
[region3,area3] = Segmentation_Growing(img,sx1,sx2,sy1,sy2,2);

n = size(region);
% use my function and plot everything
fig(1) = figure(1);
clf(1);
subplot(2,2,1);hold on;imshow(img);title('original');
subplot(2,2,2);hold on;imshow(region3);title('f = 2.0');
subplot(2,2,3);hold on;imshow(seed);title('seed');
subplot(2,2,4);hold on;title('All together');
imshow(img);
green = cat(3, zeros(n), ones(n), zeros(n));
h = imshow(green);set(h, 'AlphaData', area3);
yellow = cat(3, 0.8*ones(n), 0.8*ones(n), zeros(n));
h2 = imshow(yellow);set(h2, 'AlphaData', area2);
blue = cat(3, zeros(n), zeros(n), ones(n));
h3 = imshow(blue);set(h3, 'AlphaData', area);
plot([sy1 sy2 sy2 sy1 sy1],[sx1 sx1 sx2 sx2 sx1],'r');

Kommentare

Beliebte Posts aus diesem Blog

Easy Plots in HTML/PHP with Google Charts

Matlab: 3D Coordinate System Rotations with Vectors

Matlab: Dijkstra methode - shortest way on gradient (small neighborhood)