Code covered by the BSD License  

Highlights from
Image segmentation using statistical region merging

4.66667

4.7 | 12 ratings Rate this file 114 Downloads (last 30 days) File Size: 59.57 KB File ID: #25619
image thumbnail

Image segmentation using statistical region merging

by Sylvain Boltz

 

20 Oct 2009 (Updated 14 Feb 2010)

Very simple and fast image segmentation code using statistical region merging.

| Watch this File

File Information
Description

From the paper :

Nock, Richard and Nielsen, Frank 2004. Statistical Region Merging. IEEE Trans. Pattern Anal. Mach. Intell. 26, 11 (Nov. 2004), 1452-1458.

It uses an a disjoint-sets data structure with a union-find strategy: http://en.wikipedia.org/wiki/Disjoint-set_data_structure

Just run srmdemo.m !

Matlab implementation, Mex File is TODO

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (27)
02 Nov 2009 ciaiaia ccccccc

----------------
??? Subscripted assignment dimension mismatch.

Error in ==> srm at 107
im_final(:,:,i)=image_seg(map+(i-1)*n_pixels);
----------------
this algo works fine for some image, but other [and I dont know] it doesnt work

why?

03 Nov 2009 Sylvain Boltz

Hi,

This algorithm is designed for images with 3 color channels.

for i=1:3
im_final(:,:,i)=image_seg(map+(i-1)*n_pixels);
end

if your image is black and white or has a different number of channels you should change all the hard coded 3 by size_image(3).

if this does not correct the problem, can you send me the image causing problems as well as the way you open it (e.g. image=double(imread(...))).

Thanks for your feedback

04 Nov 2009 Vincent Garcia

Nice piece of work and nice paper.
The method is pretty simple but the results are very interesting.
The coding style is clear enough to be easily modified.
Thx for sharing you code.
V.

06 Nov 2009 Chuck

Thanks for this - really nice. About the error brought up earlier. I had a couple images that caused the same error. Seems to be an issue with the im_final being re-sized on the fly? Not sure, but I got it working on those images by adding an

im_final = image

just prior to the final "for" loop.

Chuck

03 Dec 2009 create lei

the cause of error mentioned by "ciaiaia ccccccc" is simple .

I believe he run the code sucessfully the first time and then he

changed the input image name ,and then run again without clear

the workspace .

it is easy to correct this problem

you could just add ''clear'' before the code . and then run this

code repeatedly as you want to test different images

24 Dec 2009 igor kolupaev

Your work not only interesting - but helpful, thanks.

28 Dec 2009 rex

when i first run srmdemo.m , MAT display error:

??? Attempted to access pairs1(0); index must be a positive integer or logical.

Error in ==> srm at 50
        C1=pairs1(index(i));

Error in ==> srmdemo at 8
[maps,images]=srm(image,Qlevels);

who knows this problem and fix it?

18 Jan 2010 zhouzhiyuan zhouzhiyuan

when i first run srmdemo.m , Display error!
??? Error: File: srm.m Line: 27 Column: 3
Expression or statement is incorrect--possibly unbalanced (, {, or [.

who can fix this problem

19 Jan 2010 Sylvain Boltz

Your matlab release is old, you should replace :

[~,index]=....
by
[trash,index]=....

I use the new command ~ that ignores useless argouts. If your matlab does not recognize it, just replace all the ~ by trash ( send argouts to a trash variable )

07 Feb 2010 Madhumitha

Hi Sir,
      I ran thru ur coding...but how do I understand the concept on which it works...do iIneed to go thru any paper or study material...if so plz help me by mentioning it...(I could understand only the basic in what is given in wikipedia)

09 Feb 2010 Sylvain Boltz

Hi, please refer to the following paper :

Nock, Richard and Nielsen, Frank 2004. Statistical Region Merging. IEEE Trans. Pattern Anal. Mach. Intell. 26, 11 (Nov. 2004), 1452-1458.

25 Feb 2010 worldguard Zhou

??? Invalid MEX-file 'C:\Documents and
Settings\Administrator.92524BFB5303467\My
Documents\MATLAB\mydone2\srm\boundarygradient.mexw32':
because the configurement of application prgram is incorrect, application prgram doesn't start, resetuping application prgram might be right.
what shall I do ?

25 Feb 2010 worldguard Zhou

My Matlab version 2008b,when I mex the two *.c file, there are so many errors that it is Unable to complete successfully.
Error: Compile of 'boundarygradient.c' failed

02 Mar 2010 Sylvain Boltz

Hi,

The mex files given are from a cpp code.
If you want to compile them with a c compiler, you need to correct some minor symtax differences.
The main one is that you need to declare the variables on the top of the function and not through the program, e.g. avoid for {int i = 0....

I should fix this in a future release, thanks

28 Mar 2010 narendran

help me with this error

??? Error: File: srm.m Line: 28 Column: 3
Expression or statement is incorrect--possibly unbalanced (, {, or [.

i have matlab 2007

07 Apr 2010 Karthik Muthuswamy

Great piece of code. Thanks for sharing!

08 Apr 2010 Ahana

Great work,but is there a way to do this in matlab without using the 'map' matrix,can you give some hint about this approach,thanks in advance

02 May 2010 Judit

hello, I use matlab 7.0 and I have any problems with line 34 in srm_boundarylen, mwSize dims[2] = {nlabels,nlabels};, to solve it I change "mwSize" for "int" but it seems that initializer must be constant, however when I write mwSize dims[2] = {3,3} the figure 101 plots 9 identical imagenes , what is the problem? thanks

11 Jul 2010 t zq

I met same problems as worldguard Zhou , and the mex error is shown as follows. I think it will help you to fix mex error.
>> mex srm_boundarylen.c
srm_boundarylen.c
srm_boundarylen.c(30) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(31) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(32) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(34) : error C2275: 'mwSize' : illegal use of this type as an expression
        d:\program files\matlab\r2006b\extern\include\tmwtypes.h(731) : see declaration of 'mwSize'
srm_boundarylen.c(34) : error C2146: syntax error : missing ';' before identifier 'dims'
srm_boundarylen.c(34) : error C2065: 'dims' : undeclared identifier
srm_boundarylen.c(34) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(34) : error C2059: syntax error : '{'
srm_boundarylen.c(35) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
srm_boundarylen.c(35) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
srm_boundarylen.c(37) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(38) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(40) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(41) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(41) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(41) : error C2143: syntax error : missing ')' before 'type'
srm_boundarylen.c(41) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(41) : error C2065: 'c' : undeclared identifier
srm_boundarylen.c(41) : error C2065: 'cols' : undeclared identifier
srm_boundarylen.c(41) : warning C4552: '<' : operator has no effect; expected operator with side-effect
srm_boundarylen.c(41) : error C2059: syntax error : ')'
srm_boundarylen.c(42) : error C2143: syntax error : missing ';' before 'for'
srm_boundarylen.c(42) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(42) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(42) : error C2143: syntax error : missing ')' before 'type'
srm_boundarylen.c(42) : error C2143: syntax error : missing ';' before 'type'
srm_boundarylen.c(42) : error C2065: 'r' : undeclared identifier
srm_boundarylen.c(42) : error C2065: 'rows' : undeclared identifier
srm_boundarylen.c(42) : warning C4552: '<' : operator has no effect; expected operator with side-effect
srm_boundarylen.c(42) : error C2059: syntax error : ')'
srm_boundarylen.c(43) : error C2143: syntax error : missing ';' before '{'
srm_boundarylen.c(46) : error C2065: 'map' : undeclared identifier
srm_boundarylen.c(46) : error C2065: 'i' : undeclared identifier
srm_boundarylen.c(46) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(47) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(50) : error C2065: 'neighbors' : undeclared identifier
srm_boundarylen.c(50) : error C2065: 'nlabels' : undeclared identifier
srm_boundarylen.c(50) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(51) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(56) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(57) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(60) : error C2109: subscript requires array or pointer type
srm_boundarylen.c(61) : error C2109: subscript requires array or pointer type
 
  D:\PROGRA~1\MATLAB\R2006B\BIN\MEX.PL: Error: Compile of 'srm_boundarylen.c' failed.
 
??? Error using ==> mex
Unable to complete successfully.

======================================
>> mex srm_boundarygradient.c
srm_boundarygradient.c
srm_boundarygradient.c(38) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(39) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(40) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(42) : error C2275: 'mwSize' : illegal use of this type as an expression
        d:\program files\matlab\r2006b\extern\include\tmwtypes.h(731) : see declaration of 'mwSize'
srm_boundarygradient.c(42) : error C2146: syntax error : missing ';' before identifier 'dims'
srm_boundarygradient.c(42) : error C2065: 'dims' : undeclared identifier
srm_boundarygradient.c(42) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(42) : error C2059: syntax error : '{'
srm_boundarygradient.c(43) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
srm_boundarygradient.c(43) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
srm_boundarygradient.c(45) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(46) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(47) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(49) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(50) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(50) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(50) : error C2143: syntax error : missing ')' before 'type'
srm_boundarygradient.c(50) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(50) : error C2065: 'c' : undeclared identifier
srm_boundarygradient.c(50) : error C2065: 'cols' : undeclared identifier
srm_boundarygradient.c(50) : warning C4552: '<' : operator has no effect; expected operator with side-effect
srm_boundarygradient.c(50) : error C2059: syntax error : ')'
srm_boundarygradient.c(51) : error C2143: syntax error : missing ';' before 'for'
srm_boundarygradient.c(51) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(51) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(51) : error C2143: syntax error : missing ')' before 'type'
srm_boundarygradient.c(51) : error C2143: syntax error : missing ';' before 'type'
srm_boundarygradient.c(51) : error C2065: 'r' : undeclared identifier
srm_boundarygradient.c(51) : error C2065: 'rows' : undeclared identifier
srm_boundarygradient.c(51) : warning C4552: '<' : operator has no effect; expected operator with side-effect
srm_boundarygradient.c(51) : error C2059: syntax error : ')'
srm_boundarygradient.c(52) : error C2143: syntax error : missing ';' before '{'
srm_boundarygradient.c(55) : error C2065: 'map' : undeclared identifier
srm_boundarygradient.c(55) : error C2065: 'i' : undeclared identifier
srm_boundarygradient.c(55) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(56) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(59) : error C2065: 'neighbors' : undeclared identifier
srm_boundarygradient.c(59) : error C2065: 'nlabels' : undeclared identifier
srm_boundarygradient.c(59) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(59) : error C2065: 'gradient' : undeclared identifier
srm_boundarygradient.c(59) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(60) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(60) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(65) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(66) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(69) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(69) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(70) : error C2109: subscript requires array or pointer type
srm_boundarygradient.c(70) : error C2109: subscript requires array or pointer type
 
  D:\PROGRA~1\MATLAB\R2006B\BIN\MEX.PL: Error: Compile of 'srm_boundarygradient.c' failed.
 
??? Error using ==> mex
Unable to complete successfully.

12 Jul 2010 Sylvain Boltz

"I met same problems as worldguard Zhou", and I give you indeed the same answer : use a cpp compiler, not a c compiler.

15 Jul 2010 Pablo Sala  
16 Nov 2010 Nam Le

Cool. This works perfectly for me. Thanks for this sharing!

29 Nov 2010 tonyan

Hi,
This works very cool.
I don't kown the purpose of the following program

    while 1
        map_ = map(map) ;
        if isequal(map_,map) ; break ; end
        map = map_ ;
    end

Thanks very much

22 Feb 2011 Hugues

@all with compiler issues: rename *.c to *.cxx if you do not know how to change your compiler.

@Sylvain: Nice piece of work.
But what is the purpose of the last 8 lines of code in srm.m function? Their results are not returned. In fact srm_boundarygradient.c can be ignored.

    bgradient = sparse(srm_boundarygradient(labels, nlabels, normgradient));
    bgradient = bgradient - tril(bgradient);
    idx=find(bgradient>0);
    [~,index]=sort(bgradient(idx));
    n_pairs=numel(idx);
    [xlabels,ylabels]=ind2sub([nlabels,nlabels],idx);
    pairs1=clusterlist(xlabels);
    pairs2=clusterlist(ylabels);

03 Jul 2011 Nitin Kumar  
12 Aug 2011 Diggs

Am currently working on flood detection where am supposed to segment the two images , classify them independently and then later obtain a change image.am woundering how i can use to this same method to segment my images and how i can calculate mean for each segment in the image
and also when i try to run this code on my landsat image,it gives this error

??? Index exceeds matrix dimensions.

Error in ==> srm at 66
        dG=(image_seg(C1+n_pixels)-image_seg(C2+n_pixels))^2;

19 Dec 2011 reena

works well.. thanks for sharing

Please login to add a comment or rating.
Updates
22 Dec 2009

Now supports multiscale segmentation

05 Feb 2010

now with a parameter smallest_region_allowed=10; to filter out small regions at each scale

14 Feb 2010

corrected matlab warnings, as well as a bug that can appear on synthetic images.

Tag Activity for this File
Tag Applied By Date/Time
image processing Sylvain Boltz 21 Oct 2009 10:35:59
image segmentation Sylvain Boltz 21 Oct 2009 10:35:59
clustering Sylvain Boltz 21 Oct 2009 10:35:59
signal processing Sylvain Boltz 21 Oct 2009 10:35:59
region merging Sylvain Boltz 21 Oct 2009 10:35:59
clustering Szilárd 16 Feb 2010 16:08:29
image processing Szilárd 16 Feb 2010 16:08:34
image segmentation Szilárd 16 Feb 2010 16:08:37
region merging Szilárd 16 Feb 2010 16:08:39
signal processing Szilárd 16 Feb 2010 16:08:42
region merging sriram gowtham rengarajan 31 Mar 2011 05:10:12

Contact us at files@mathworks.com