File Exchange

image thumbnail

Efficient Graph based image Segmentation

version (1.13 MB) by Su Dongcai
A simple and efficient graph based image segmentation algorithm


Updated 24 Nov 2009

View Version History

View License

This file is an implementation of an image segmentation algorithm described in reference[1], the result of segmentation was proven to be neither too fine nor too coarse. A toolbox regarding to the algorithm was also avalible in reference[2], however, a toolbox in matlab environment is excluded, this file is intended to fill this gap.

To duplicate the result of the screenshot, please run:

[1] Efficient Graph-Based Image Segmentation,
Pedro F. Felzenszwalb and Daniel P. Huttenlocher International Journal of Computer Vision, Volume 59, Number 2, September 2004

The Author want to thanks:
[1] Yan Zhang
2D/3D image segmentation toolbox
For his "binaryHeap.h" class in cpp
[2] Dirk-Jan Kroon
Image Edge Enhancing Coherence Filter Toolbox
For the "Coherence Filter Toolbox" he provided
[3] Luigi Giaccari
For the fast implementation of K-NEAREST NEIGHBORS SEARCH in 3D.

Cite As

Su Dongcai (2021). Efficient Graph based image Segmentation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (32)


is it by mst?

Kuki Karuna

Sorry where's that clown faces image come from?
can i change to another image?

Ratna Saha

Hi, I am facing a problem. If I am running the program to segment image with k-nearest neighbourhood model, Matlab is crashing. However, for adjacent neighbourhood model, I can segment the image. Is there anyone, who has faced the same problem like me? How to solve it?



mahreen kiran

it requires compiler ,,, and I am trying for months but coulnt installed mex files compiler


Can anyone tell me how to run this program. I am completely new user.
Ton of thanks.



Dear sir,
i am getting following error
Error: Could not detect a compiler on local system
which can compile the specified input file(s)

C:\PROGRA~1\MATLAB\R2007B\BIN\MEX.PL: Error: No compiler options file could be found to compile source code. Please run "mex -setup" to rectify.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> test_GraphSeg at 30
mex GraphSeg_mex.cpp

Plz help me. My matlab version is 2007b


Good implementation.

On Linux some modifications:
- change the file seperator '/' by filesep in the test files.

- remove the GLTREE:: in the GLTree.h for the declaration of
void SearchClosest(Coord3D *p,Coord3D *pk,int* idc,double* mindist);
void SearchKClosest(Coord3D *p,Coord3D *pk,int* idc,double* mindist,int k);

- remove the double brackets in the file GLTree.cpp { {step=Maxz-Minz;}}

- change the name of the function for the case sensitive problems in knn_search.m for the DeleteGLTree (the case sensitive problems occur at a few other places)


Very good work, and thanks for sharing.


Very good implementation.

Works for me.

For linux users: be sure to change the VC style `exception(char*)` into `exception()` and nearly everything works out.


I trying to run the code. but error is comes at
img = im2double(img);
line no. 41
please sir, help me to solve the error.


Hi Su,

Nice work! Thanks for the sharing!

Would you please kindly help me on the following 2 questions?

1. There are two methods/applications in ref[1]: Grid Graph and Nearest Neighbor Graph. For grid graph, the input is the image. For NN Graph, the input can be the feature of each pixel. I can't find the NN Graph method in ref[2] and your code. Do you know if there is such code publicly available?
2. for the parameters, how to correspond yours with the ones in ref[1]? say, sigma, threshold(you use .5 while ref[1] use 100-300)?

Thanks in advance!


Hi Su,

I am new in graph-based segmentation.
In the paper, there is parameter k usually set as 150/300. What is k value in your code, how do I change it?

I got segmentation images from all r,g and b channel, how can I intersect with them?

Can someone else guide me through this? I will be really appreciated.


Hi Su,

Thanks for the good work. Could you please let me know how to run it with color image? Thanks a lot.

Syed Afaq Shah

Hi Su,

Is it possible to find the total number of components (segmented regions) in graph based segmented image by using your code? In the paper, the author is using the term C for total number of component. Can you please guide me in this regard.

Syed Afaq Shah

I was compiling but was getting the specified error. However, issue resolved for Windows by some other approach .... Now I am facing problem while running this code in Linux.

Hi, you must first compile this file.

Syed Afaq Shah

I am getting following error while running this code. Can any one help me?

>> L = graphSeg(filted_I, 0.5, 50, 2, 0);
??? Undefined function or method 'GraphSeg_mex' for input arguments of type 'double'.

Error in ==> graphSeg at 44
L = GraphSeg_mex(img, threshold, min_size, nRadius);

tried to fix it using mex but it didn't work.


sir can u please tell how u have loaded image of the clown, i want to try this with different images please tell me the steps....

Green amy

want to download it

woon cho


Can input another image??

Pindi Mani

Please tell me how to run this code..... As i am new to MatLab i do not know....
Please tell me the commands and which file to run....
plz tell me soon......
My email_id is
Expecting a reply soon....
Thank you

Pindi Mani

Please.... As i'm new to matlab.... please tell me how to run this program....

Can u give all the commands to run this program in matlab.....
My email id is

Thank you

Chao Zeng

Good work!

One problem. The following information appears on the command window when I try to run test_GraphSeg.m.
Error: Could not detect a compiler on local system
which can compile the specified input file(s) at C:\Program Files\MATLAB71/bin/ line 501.
??? Error using ==> mex
Unable to complete successfully

Error in ==> test_GraphSeg at 30
mex GraphSeg_mex.cpp"

What's wrong? Thank you


i'm new to matlab.pls temme how to run all those program...
pls help me soon
thid id ma id (

Su Dongcai

Hi zhao:
Good question. According to paper [1], the segmented result of multi-channel image is done by intersection of single-channel image segmentation result. I will update this vision and make it work for color image segmentation later soon. Thanks for your comment.

aber zhao

if the input is the 3 D range image, how can i make this work? thx

piji li

很好很强大 正在找呢


Su Dongcai

Hi zaheer ahmad:
Thanks for your comment, you are right, the main routine "GraphSeg.h" was still written in C++. Sorry to disappoint you, I'm not familiar of working with pointers in matlab, I'm a c programer before a matlab coder. However, I'll be very happy if you or someone else could fill this gap:).

zaheer ahmad

I thin it would be better if you convert all the code into Matlab as stil main code is in C++.

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!