[2D] Largest Area

Read the info in the top.
This function gets the largest area from a binary image, larger than a given threshold.

function [outim] = largestarea(im,threshold)
% Tim Zaman © 2010, TU Delft
% 'largestarea' reads in a 2-d binary image and outputs a binary image, retaining only the largest blobs larger than the largest by a threshold.
%
% Usage: [outim] = largestarea(im,threshold)
%
% im - 2-d binary image
% outim - Output binary image (with 1s and 0s)
%
% Example:
%
% im = imread('text.png');
% outim = bwlargestblob(im,8);

connectivity=4;

if size(im,3)>1,
error('largestarea accepts only 2 dimensional images');
end

[imlabel totalLabels] = bwlabel(im,connectivity);
sizeBlob = zeros(1,totalLabels);
for i=1:totalLabels,
sizeblob(i) = length(find(imlabel==i));
end

max_global=max(sizeblob);
i=0;
while max(sizeblob)> max_global*threshold
i=i+1;
[maxno largestBlobNo(i)] = max(sizeblob);
sizeblob(largestBlobNo(i))=1;
end

outim = zeros(size(im),'uint8');
for ii=1:i
outim(find(imlabel==largestBlobNo(ii))) = 1;
end
outim=logical(outim);
end

Advertisements

One response

24 03 2011
More code released « Tim Zaman

[…] [2D] Largest Area […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: