File Exchange

image thumbnail

Graph Cut for image Segmentation

version (1.77 KB) by Amarjot
The code segments the grayscale image using graph cuts.


Updated 04 Dec 2014

View License

Running the Code:
% Maximum Image size 128 by 128

[Ncut] = graphcuts(I,pad,MAXVAL)
% function [Ncut] = graphcuts(I)
% Input: I image
% pad: spatial connectivity; eg. 3
% MAXVAL: maximum image value
% Output: Ncut: Binary map 0 or 1 corresponding to image segmentation

Cite As

Amarjot (2020). Graph Cut for image Segmentation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

i also tried for spine segmentationbut getting graphcut error

i tried gray scale and color images and got.
Error in graphcuts (line 18)
WM = zeros(H*W,H*W); countWM = 0;

jose mejia


I wouldn't use this code if I were you. Poorly perform

mog sam

PL Yang


This is a very poor and extremely inefficient implementation of the normalized graph cut algorithm.

hello, i tried implement the code on matlab.i am getting error on line 7
I = double(I); [H,W] = size(I);
error :
Error using graph cuts (line 7)
Not enough input arguments.

Sir, I am not able to understand the construction of weight matrix. Can you provide reference or construct algorithm of steps of the provided program

hello,I tried running the segmentation on a retinal image and this is the error I get. Could anyone drop some points on how to solve it or is it that the data is large for the algorithm to work on
Error using zeros
Requested 507904x507904 (1922.0GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may
take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.

Error in graphcuts (line 20)
WM = zeros(H*W,H*W); countWM = 0;

I am trying to excute the code but it is givng error
Error in graphcuts (line 18)
WM = zeros(H*W,H*W); countWM = 0;
Max variable size exceeded


Thank you for sharing!


@chandra Please use the image size 128 by 128

I am getting an error at line:18, which has the instruction WM = zeros(H*W,H*W);... I am using an image of 256x320, so the zero matrix forming is giving the problem. Is it that I have to use some smaller sized image? I have tried generating zeros(256*256), it also give error. What is the workaround to solve this problem?

joy angel



@matlabuser1 I assume you are passing the to the function.In that case you should not get any error. Can you send me the file in which you are calling the function at I can look into the error.

Firstly, I am getting an error:

??? Input argument "I" is undefined.

Error in ==> graphcuts at 7
I = double(I); [H,W] = size(I);


Also, I am getting the same graph cut for ever image. Please help me out.



You might want to add a input check / limitation to the max. input size of an image.
For a 400x400 image, this code requires approximately 200Gb of memory...

The code is quite compact though. Thanks for sharing.




Why there is a problem when I run this program!!!
Out of memory. Type HELP MEMORY for your options.
Error in graphcuts (line 18)
WM = zeros(H*W,H*W); countWM = 0;

This is a very poor and extremely inefficient implementation of the normalized graph cut algorithm.


I added the maximum image size for which the code runs without error.

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