File Exchange

image thumbnail

color Image Segmentation

version (16.4 KB) by Athi
Segments a color image into regions.


Updated 07 Jun 2011

View Version History

View License

This program segments a color image into different regions.
The steps followed for segmentation in this program is as follows:

1. The input color image will be coarsely represented using 25 bins.
2. Coarse representation uses the spatial information from a Histogram based windowing process.
3. K-Means is used to cluster the coarse image data.

Those who are new to Matlab Programming can see the "SampleUsage.m" file for the usage of the script "colImgSeg.m".

Those who are already familiar with Matlab Programming can directly use the script "colImgSeg.m".

Cite As

Athi (2021). color Image Segmentation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (27)

Vita Shevchenko

Athi, I need your help.
Run your file and have the result in grey colour.
Don't understand...

Mohamed Asif

mert vatansever

hi Athi,
ı need more explanation about your codes.
so if its possible give me a more more details?
actually ım so new for matlab but ı have a homework about this subject. so pls help me if you can.

more details. more comment about codes. :)


Thanks for this, it is really helpfull

Ashraful Alam

How can i get matlab code in colour image segmentation.


Hi Athi
I have implemented your code..
i am getting correct output when i run it first time..but when i try to run it next time i am getting this error..
Index exceeds matrix dimensions.

Error in colImgSeg (line 22)

Error in nbinsusage (line 19)
outImg = colImgSeg(inImg, nBins, winSize, nClass);

please help me to resolve the same..

Sabarinathan Vadivelu


May i ask if there is a way to keep the same color for the same region of an image if running kmeans several times? thanks a lot!!!



waseem ahmad

hi athi,
i need to find the center of a colored region if u can help me,plz?


Hi Athi
When i am running this code i am getting error like this

Error in ==> colImgSeg at 24
s = size(inImg);
Plz help me


hi athi
thnx dude
can you plz send me the Literature of this program
it will help me in myproject



Its good,

fusedMap = kmeans(inImg_1D,nClass, 'EmptyAction', 'singleton');

I am working on medical images, where the fusedMap becomes NULL.

Whether its for particular type of images ?


if the illustration algorithm is more specific, this program will be more honorable!


thank you so much! it's useful!

Juan Du

Hi Athi,

Thank you. This is very useful tool. I also want to segmenting HSI images. But it does not work with this code. This code seems only works on RGB image. Can you help me to solve the problem?


zhao qiang

zhao qiang

Hi Athi,
Nice contribution.
your program is unstabe ,this is because of the function kmeans,you can try it.
and i want konw what is the meaning of the following statements?

lab = floor(mat(i,j,1)/divis)*(nBins*nBins);
lab = lab + floor(mat(i,j,2)/divis)*(nBins);
lab = lab + floor(mat(i,j,3)/divis);
lab = lab + 1;
TabLabel(lab) = TabLabel(lab) + 1;
sum_lab = sum_lab + lab;
i think this is a weighted action?but why?
look for your reply~


Hi, I tried your program but got lot of errors, like not in the loop, unused variables. Can you fix it?

Ashok Kumar

nanang krisdianto

hi Athi

when i am running the code,
Why i have notification like this

??? Input argument "inImg" is undefined.

Error in ==> colImgSeg at 24
s = size(inImg);

jichao zhao

why I have the following error, I do not have Image Processing Toolbox?

??? Undefined function or method 'kmeans' for input arguments of type 'double'.

Error in ==> colImgSeg at 81
fusedMap = kmeans(inImg_1D,nClass, 'EmptyAction', 'singleton');

Error in ==> SampleUsage at 18
outImg = colImgSeg(inImg, nBins, winSize, nClass);


Its working fine. It helps me to understand color histogram fuzzification. Thanks a lot.


Dear Ramana Murthy,

The output matrix will be same.
But the coloring may differ. Because of default color usage & K-Means.
Am I answered your question?

Venkat R

Venkat R

Hi Athi,
Nice contribution.
When we run the program on same image, different results are coming. It appears there is some bug in your terminating criterion in clustering. Can you fix it.


MATLAB Release Compatibility
Created with R13
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!