File Exchange

## Region Growing (2D/3D grayscale)

version 1.5.0.0 (289 KB) by Daniel

### Daniel (view profile)

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

Updated 15 Aug 2011

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.

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

Inputs:
- 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

Outputs:
- 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) (https://www.mathworks.com/matlabcentral/fileexchange/32532-region-growing-2d-3d-grayscale), MATLAB Central File Exchange. Retrieved .

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

### abd jaw (view profile)

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

JUN MA

### JUN MA (view profile)

Rojo Mettle-Nunoo

Akib Ahmed

### Akib Ahmed (view profile)

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?

Thanks

Jurgen

### Jurgen (view profile)

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.

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

JH6

### 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

Mohamed Khalid

### Mohamed Khalid (view profile)

andhavarapu lokesh

### andhavarapu lokesh (view profile)

how to use this can anyone tell

Farah Bazzi

### Farah Bazzi (view profile)

Hi,
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 ?

afshin shoeibi

afshin shoeibi

Nassir H. Salman

### Nassir H. Salman (view profile)

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

Nassir H. Salman

### Nassir H. Salman (view profile)

Matlab is great software and thanks to Mathworks

jack nn

### jack nn (view profile)

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)

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

Isabel

gang

### gang (view profile)

thks very much good job!

Hanbo Chen

Yang

Omer Demirkaya

### Omer Demirkaya (view profile)

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

### Omer Demirkaya (view profile)

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

Omer.

Daniel

### 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.

Michael

### Michael (view profile)

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

### maram (view profile)

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

Daniel

### Daniel (view profile)

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

maram

### maram (view profile)

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

Daniel

### 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

### Gomathi C (view profile)

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
numel(initPos)=2.

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

Ramachandran M.G.

### Ramachandran M.G. (view profile)

good work

Ramachandran M.G.

Daniel

### 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

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.

Jerod Rasmussen

### Jerod Rasmussen (view profile)

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