File Exchange

image thumbnail

Region Growing (2D/3D grayscale)

version (289 KB) by Daniel
Recursive region growing algorithm for 2D/3D grayscale images with polygon and binary mask output


Updated 15 Aug 2011

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

Cite As

Daniel (2020). Region Growing (2D/3D grayscale) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (39)

Hi Daniel
can you please provide a video tutorial on how to work on a dicom image series (2D or 3D) ?
it is just an example to get my head around it.

abd jaw

Undefined operator '-' for input arguments of type 'cell'.
Error in axes2pix (line 57)
delta = xlast - xfirst;
Error in regionGrowing (line 77)
initPos(1) = round(axes2pix(size(cIM, 2), get(himage, 'XData'), p(2)));
Error in abd1272020 (line 60)
[poly im] = regionGrowing( redChannel,[],20,200); % click somewhere inside the liver


Akib Ahmed

Hello, I am new here. Whenever I run your code, I find this error

"Error using regionGrowing (line 61)
Please define one of the current images!"

How can I define one of the current images? Also what format does this code accept?



if tfMean is false, you can calculate J in a single iteration. This is what the function grayconnected (image processing toolbox) does. Other properties worth noting: it grows a single pixel at a time, even if there multiple eligible neighbours with equal values. If there are multiple it just chooses the first pixel, not the necessarily the pixel with the best/nearest value.

Dini Nadiah

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


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


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


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



thks very much good job!

Hanbo Chen


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

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

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



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.


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


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


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


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


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

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 Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Region Growing, Line Simplification

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