talkanat
talkanat

Reputation: 329

Matlab image segmentation using imageSegmenter tool

I have a fairly simple question. I am trying to segment an image using MATLAB. I have tried the imageSegmenter app, a toolbox with GUI. The tool seems to be working perfectly, especially when I use the "Flood Fill" option with almost any tolerance parameter.

Is there a function (not a tool) form of the flood fill? If yes, what is the name of the function? The documentation seems not be including this information.

Upvotes: 1

Views: 686

Answers (1)

Honeybear
Honeybear

Reputation: 3138

The function grayconnected(I,row,column,tolerance) does, what the Flood-Fill-Tool in the imageSegmeter-Toolbox does: Initialize with a point [x,y] (column-/row-index in the image) and starting from there "flood" surrounding pixels within a given gray value range specified by the tolerance parameter (top-left in the Flood Fill GUI).

Actually you only need that one line (if you have your gray-valued img, an initialization point row,column and picked a tolerance, e.g. 12):

%>>> this is where the magic happens <<<%
segmentation = grayconnected(img, row, column, 12);

For convenience though see below a code snippet with visualization, where you may select your initialization. Input is a colored image (if it's already gray, skip rgb2gray). Output (a segmentation mask) corresponding to each point i is in segmentations(:,:,i). You may merge these single segmentation masks to one or assign them to different objects.

Note that this is really a very basic segmentation method, prone to noise and bad if you don't have a clear contrast (where a single threshold operation might already give you good results without initialization). You can use this initial segmentation to be refined, e.g. with active contours.

[img] = imread('test.jpg');
img = rgb2gray(img);
tolerance = 12; % default setting in imageSegmenter

%% >>>>>>>>>> GET INITIALIZATION POINTS <<<<<<<<<< %%
str = 'Click to select initialization points. Press ENTER to confirm.';
fig_sel = figure(); imshow(img); 
title(str,'Color','b','FontSize',10);
fprintf(['\nNote: ' str '\n'...
    'Pressing ENTER ends the selection without adding a final point.\n' ...
    'Pressing BACKSPACE/DELETE removes the previously selected point.\n'] );
% select points in figure and close afterwards
[x, y] = getpts(fig_sel);
close(fig_sel);

%% >>>>>>>>>> PROCESS INITIALIZATION POINTS <<<<<<<<<< %%
if length(x) == 0
    fprintf('\nError: No points specified. An initialization point is needed!');
else
    segmentations = zeros([size(img) length(x)]);

    fig_result = figure(); hold on;

    for i = 1:length(x)
        % confusing: y corresponds to row, x to column in img
        column = ceil(x(i)); 
        row = ceil(y(i));

        %>>> this is where the magic happens <<<%
        segmentations(:,:,i) = grayconnected(img,row,column,tolerance);

        % show corresponding initialization point
        subplot(1,2,1); imshow(img); hold on; 
        title('Active point (red)');
        plot(x(i),y(i),'r.','MarkerSize',10); % active in red
        plot(x(1:end ~= i),y(1:end ~= i),'b.','MarkerSize',5); % ... others in blue
        hold off;
        % ... with segmentation result
        title('Segmentation result');
        subplot(1,2,2); imshow(segmentations(:,:,i));

        % click through results
        waitforbuttonpress
    end
    close(fig_result);
end

Upvotes: 1

Related Questions