View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Region Adjacency Graph (RAG)

4.8 | 8 ratings Rate this file 23 Downloads (last 30 days) File Size: 180 KB File ID: #16938 Version: 1.3
image thumbnail

Region Adjacency Graph (RAG)


David Legland (view profile)


17 Oct 2007 (Updated )

Computes the graph of adjacent regions in a labeled image

| Watch this File

File Information

Computes region adjacency graph of a labeled 2D or 3D image.

Two regions are considered as neighbor if they are separated by a small number of pixel in the horizontal or vertical direction, as well as depth direction for 3D images.

The result is a N*2 array, containing 2 indices for each couple of neighbor regions.

ADJ has the format [LBL1 LBL2], LBL1 and LBL2 being vertical arrays the same size.
LBL1 is given in ascending order, LBL2 is given in ascending order for each LBL1. Ex :
    [1 2]
    [1 3]
    [1 4]
    [2 3]
    [2 5]
    [3 4]
Alternative syntax:
Return two arrays: the first one is a [N*2] array containing centroids of the N labeled region, and ADJ is the adjacency previously described.

For 3D images, the node array is [N*3].

The algorithm proceed in scanning each line (horizontal, vertical, and in z-direction for 3D images), and by detection transitions in the labels. A background gap of width 1 pixel/voxel is assumed to exist between regions. It is possible to specify different gaps:

* gap = 0 if regions are contiguous (no background),
* gap = 1, when considering the result of a watershed, for example
* gap > 1 if regions are separated by thick background (some neignborhoods can be missed)


This file inspired Microscopy Image Browser (Mib).

MATLAB release MATLAB 7.9 (R2009b)
Other requirements image processing toolbox is not required to run the function, but is required to run the example code.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
16 May 2016 Stephen

Works well.

01 May 2016 Ghazouani

Good tool

08 Oct 2015 Ilya Belevich

Great tool, thank you for sharing!

21 May 2013 G. Sken

G. Sken (view profile)

helpfull to compute adjacency relationship between 3D regions segmented via watershed methods.

03 Oct 2012 David Legland

David Legland (view profile)

Hi Kerem,
I have submitted an updated version, in which you can control the "gap" between two regions. If you chosse a gap value equal to 2, it should fit your needs (hopefully !).

Comment only
20 Aug 2012 Kerem Sahin

Any help for the extension to 8-connected neighborhood of this implementation is appreciated.

Comment only
21 May 2012 Chen

Chen (view profile)

20 Apr 2011 Ryan

Ryan (view profile)

Very useful.

31 May 2010 Joao Henriques

Joao Henriques (view profile)

14 Mar 2008 Perrine Paul

Very efficient. Some example of use would be of interest, for accessing data.

29 Jul 2010 1.1

removed dependencies to image processing toolbox, updated doc

02 Oct 2012 1.2

changes the algorithms to remove the 'diff' function, reducing the amount of memory required, and add psb to use different gaps between regions.

21 May 2013 1.3

correct processing of gap>1, and simplification of the code

Contact us