File Exchange

image thumbnail

Region Growing (2D/3D grayscale)

version 1.5 (289 KB) by

Recursive region growing algorithm for 2D/3D grayscale images with polygon and binary mask output



View License

A recursive region growing algorithm for 2D and 3D grayscale image sets with polygon and binary mask output. The main purpose of this function lies on clean and highly documented code.

[P, J] = regionGrowing(cIM, initPos, thresVal, maxDist, tfMean, tfFillHoles, tfSimplify)

- cIM: 2D/3D grayscale matrix
- initPos: Coordinates for initial seed position
- thresVal: Absolute threshold level to be included
- maxDist: Maximum distance to the initial position in [px]
- tfMean: Updates the initial value to the region mean (slow)
- tfFillHoles: Fills enclosed holes in the binary mask
- tfSimplify: Reduces the number of vertices with line simplification

- P: VxN array (with V number of vertices, N number of dimensions). P is the enclosing polygon for all associated pixel/voxel
- J: Binary mask (with the same size as the input image) indicating 1 (true) for associated pixel/voxel and 0 (false) for being outside

Comments and Ratings (33)

Dini Nadiah

@Nasir H. Salman, what parameters do you put for cIM, initPos, thresVal, maxDist, tfMean, tfFillHoles, tfSimplify?


JH6 (view profile)

It seems like this script includes all pixels within the range from the value of the initial seedpoint +/- the absolute threshold.

Small snipped from the code:
cIM(xv+i, yv+j, zv+k) <= (regVal + thresVal) &&...% within range
cIM(xv+i, yv+j, zv+k) >= (regVal - thresVal) % of the threshold?

I needed to include all values higher than the threshold value so changed it to:
cIM(xv+i, yv+j, zv+k) >= thresVal

In case people might need that.

besides that the code works perfect.

Thanks a lot!

Matteo Busi

how to use this can anyone tell

Farah Bazzi

whenever i try to run the code ( 2D example)
>> figure, imshow(cIM, [0 1500]), hold all
>> poly = regionGrowing(I, [], 300);

I get the following error:

Undefined function 'minus' for input arguments of type 'cell'.

Error in axes2pix (line 60)
delta = xlast - xfirst;

Error in regionGrowing (line 77)
initPos(1) = round(axes2pix(size(cIM, 2), get(himage, 'XData'), p(2)));

any help ?

a lot of thanks Mr Daniel
, the algorithm of 2D/3D gray scale image is excellent , i tried it , it works correctly for segmentation, boundary, no. of pixel...but i tried to display the segmented area only with white background, there is some problem .so could you pls help me to do that.
thank u, Nassir

Matlab is great software and thanks to Mathworks

jack nn

hi thanks.
I want to test this code.
I use this:
>> th = 0.35 * max(max(I));
>> [P, J] = regionGrowing(I, [30,50], th, 300, 'true', 'true', 'false');

but the result is this message:

RegionGrowing Opening: Initial position (30|50|1) with 0 as initial pixel value!
RegionGrowing Ending: Found 10390 pixels within the threshold range (418 polygon vertices)!

can you help me? Is there any mistake?

Lena Veis

Nice work..

Ander Biguri

Ander Biguri (view profile)


adi (view profile)

Hi, I tried to run the code and the segmentation I get is in a circle shape and not exactly the shape of the segment.
Is this the way the code works? because according the picture it looks like a free shape


Daniel (view profile)

Please contact me via 'braggpeaks|a|', since my deposited address is expired. Thanks, Daniel.


Isabel (view profile)


gang (view profile)

thks very much good job!

Hanbo Chen


Yang (view profile)

Omer Demirkaya

The following two statements should be replaced.
sumSQR = meanI*meanI; % sum of squares

stdI = sqrt((sumSQR - N*meanI*meanI)/(N-1)); % update standard deviation

Omer Demirkaya

Daniel good work.
I have a suggestion about an efficient mean and standard deviation calculations within your while loop.

Here is the inintilization of the parameters outside the loop.

meanI = regVal; % Mean intensity
sumSQR = 0; % sum of squares
stdI = 0; % Standard deviation
N = 1; % number of pixels

And then within your while loop after the statement,

J(xv+i, yv+j, zv+k) = true;

one can add the following lines of code to progessively calculate mean and standard deviation
gI = cIM(xv+i, yv+j, zv+k);
sumSQR = sumSQR + gI*gI;
meanI = (N*meanI + gI)/(N+1); % update mean
N = N+1; % increment number of pixels
stdI = (sumSQR - N*meanI*meanI)/(N-1); % update standard deviation



Daniel (view profile)

Hello Michael. Unfortunately, that's true. I think the easiest way is to give over a handle to the desired axis as an additional input argument.


Hey Daniel, running the 2d example with no initial seed produces an error if multiple figures are open, because get(himage,'XData') returns a cell array, and is then used in axes2pix() which accepts only numerical arrays. I'm not sure of the best way to deal with this, aside from including a 'close all' in the code.


maram (view profile)

i want to segment a mammographic image(from mias database. after choosing the seed the result is like a curve!


Daniel (view profile)

Hello moram, could you please provide some further informations (e.g. what exactly are you trying to do)?


maram (view profile)

it works very well with the example associated!!but once i tried on another image i failed!


Daniel (view profile)

The selection of a seeding position currently works only for 2D input images, since the function does not know, in which slice you may set the initial point. One (easy) way is to check whether the input image is 3D and show up the medial (or a random) slice.

Gomathi C

when i try this code for a 3d image setting the initpos value as [] while calling, it gets the input seed point from me. But, unfortunately I get the following error.

??? Attempted to access initPos(3); index out of bounds because

Kindly help me to figure out what I have to do to make the program run.

good work


Daniel (view profile)

Hello marwa. I'm sorry, i am not sure what you mean with "the same characteristics". Do you mean within the same intensity-range? You are welcome to contact me via mail. With regards, Daniel

marwa hadhoud

this is a good work, but i need to ask you, if there is a possibility to make this code search on all the image about all the separated regions in the image that have the same charactristics.

This is great! Worked intuitively right out of the box! Could use an academic reference if available.



some cosmetic changes, attached example.mat

MATLAB Release
MATLAB 7.12 (R2011a)

Inspired by: Region Growing, Line Simplification

Inspired: Region Growing (2D/3D) in C

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video