Monday, August 2, 2010

Image segmantation using Region Splitting and merging

clear all; close all; clc;
f = imread('coins.png');
g = splitmerge(f,2,@predicate);
figure,imshow(f);
figure,imshow(g);

function g = splitmerge(f, mindim, fun)
% splitmerge Segment an image using a split-and merge algorithm.
% split regions down to size 1-by-1.
q = 2^nextpow2(max(size(f)));
[m n]=size(f);
f = padarray(f, [q-m, q-n], 'post');
s = qtdecomp(f, @split_test, mindim, fun);
lmax = full(max(s(:)));
g=zeros(size(f));
marker = zeros(size(f));
for k = 1:lmax
[vals, r, c] = qtgetblk(f, s,k);
if ~isempty(vals)
for i= 1:length(r)
xlow = r(i);
ylow = c(i);
xhigh = xlow + k -1;
yhigh = ylow + k -1;
region = f(xlow:xhigh, ylow:yhigh);
flag = feval(fun, region);
if flag
g(xlow:xhigh, ylow:yhigh) = 1;
marker(xlow, ylow) = 1;
end
end
end
end
g = bwlabel(imreconstruct(marker,g));
g = g(1:m,1:n);

No comments: