File Exchange

image thumbnail

Region Growing (2D/3D) in C

version (8.71 KB) by Adrian Becker
C implementation of a basic region growing algorithm for images and volumes


Updated 02 Dec 2017

View License

A simple image segmentation method. The segmented region grows from a
seed point by comparing neighbor pixels/voxels.
If a neighbor pixel/voxel is smaller then the specified threshold value it becomes a
part of the region.
The growing algorithm is written in C because the matlab implementations are rather
slow especially for big images or volumes.

Comments and Ratings (8)


This algorithm just uses the (seed value +/- threshold) as a reference (instead of e.g. average or single linkage). I think in that case you can reduce it to a simple 1 iteration thresholding scenario similar to the grayconnected function of the image processing toolbox.

Thanks Juan i recently changed that line.
@Lorenzo: I still can't reproduce your problem. I tested it on at least 10 machines (Windows, Ubuntu, MacOS x86 and x64) but the only error i got was the one considered by Nick. This error is fixed after I changed the datatype of the pDims array

Consider changing const int *pDims; by const mwSize *pDims; in order to work on 64 bit machines.

I still confirm the problem. The C files are compiled and in private folder. I send the commands:
testImg = imread('cameraman.tif');
result = regGrow().segment(testImg,[80 64],0.2,inf,true);

and get again the error "First input must be a matrix".

@Nick: I can neither confirm the error you announce nor the usage of the word "type" in line 53 like the error supposes.

E:\MATLAB\regGrow\@regGrow\regGrow2D.c(53) : error C2143: syntax error : missing ';' before 'type' -> means that the compiler is missing a semicolon in the previous line before the next line starts with the word 'type'.
In my original submission the line starts with a "const int *pDims" and the last line is terminated properly with a semicolon.
I recommend to download and compile again with the unchanged code.
Best regards Adrian


Trying to compile produces dozens of lines of error messages starting with

E:\MATLAB\regGrow\@regGrow\regGrow2D.c(53) : error C2143: syntax error : missing ';' before 'type'
E:\MATLAB\regGrow\@regGrow\regGrow2D.c(54) : error C2143: syntax error : missing ';' before 'const'
E:\MATLAB\regGrow\@regGrow\regGrow2D.c(55) : error C2065: 'pDims' : undeclared identifier
E:\MATLAB\regGrow\@regGrow\regGrow2D.c(55) : warning C4047: '=' : 'int' differs in levels of indirection from 'const size_t *'

@Lorenzo: I reckon you forgot to squeeze out the singleton dimension in the 3D example. The size of the mri example is [height width 1 page]
Make sure that size(input) is either [height width] or [height width page] where height,width and page are non negative and finite integers.
I tested the examples on different machines (linux and windows) and it works fine.
Unfortunatly I found no better example for 3D.
PS: I also updated this submission in oop style with some new methods.

It does not work. I compiled c files obtaining mexw64 files. When run the code on my own function I get the error "First input must be a 3D array".
Also with 3D example get the same result.
How can i solve it?
Thank you.


Changed the data type of the dimension Vector from int to mwSize to prevent errors on x64 machines

Redesigned as a class with an interactive segmentation GUI in case of a 2D input image

Added input parameter for maximum euclidean distance to the seed point

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Region Growing (2D/3D grayscale)