File Exchange

image thumbnail

Color Detection Using HSV Color Space (Training and Testing)

version (166 KB) by Theodoros Giannakopoulos
The provided Matlab functions demonstrate a simple method for training and testing a color detection


Updated 24 Jan 2008

View License

The provided Matlab functions demonstrate a simple method for training and testing a color detection system. In general, a color detection algorithm searches an image for pixels that have a specific color. In this demo the HSV color space has been used, instead of the RGB space. According to that model, H(ue) dimension represents the "color", S(aturation) dimension represents the dominance of that color and the V(alue) dimension represents the brightness. Therefore, the color detection algorithm can search in terms of color position and color "purity", instead of searching for specific RGB values. We have to note here, that in order to detect the desired color, it is needed to define a tolerance in each HSV dimension. For example a tolerance equal to 0.050 in the H dimension means that the algorithm will detect pixels whose H value has a distance (from the desired value) less than 0.050.

For training the color detection scheme (i.e. for calculating the average HSV values of your objects of interest), you just have to run the getHSVColorFromDirectory() function on an known set of JPG images, stored all in a specific directory. For exaple:

HSV = getHSVColorFromDirectory('train');

This will open all JPG images in "train" folder and for each image you will be prompt to select small (5x5) areas of interest (by left clicking).

The returned value (HSV) is a Mx3 matrix (M is the number of images): each row of that matrix corresponds to the median hsv value of the selected areas in the respective image. Therefore, by taking the average, or median of that matrix, you get an estimation of the hsv value you are searching for. Now, you are ready to go through the color detection process:

colorDetectHSV('balls.jpg', median(HSV), [0.05]);

This will detect all pixels of balls.jpg whose H value has a distance of less than 0.050 from the given color (2nd argument). If you want to specify tolerance for all 3 dimensions simply write:

colorDetectHSV('balls.jpg', median(HSV), [0.05 0.5 0.50]);

Theodoros Giannakopoulos

Cite As

Theodoros Giannakopoulos (2020). Color Detection Using HSV Color Space (Training and Testing) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)


Hi please reply if there is a way to calculate euclidean distance between required hsv value and real hsv values.


hi is there any way to calculate tolerance values for all three color dimensions instead of hardcoding it

And here there must be the absolute value: difV = HSV(:,:,3) - hsvVal(3);

There is a mistake in the colordetect function, in this line: I3( find(diffS < T3) ) = 1; it should be difV.


Thank you for the code. Im getting following error. Please can you tell me why is that.

Index exceeds matrix dimensions.

Error in colorDetectHSV (line 39)
diffS = abs(HSV(:,:,2) - hsvVal(2));

Seems "hsvVal" value has a one element. Therefore when looking for 2nd element, the exception is happened.

Thank you.

thank you. i am just a is really helpful to my level to know the basics of the command systax. But there is always a file called 'train' in any demo. May i know what is that file and where it is?



the code is not running when example.m is executed


thanks for the share :)

dai zhengguo

dai zhengguo

my email is

jose Ramireez

Great man :) just what I was looking

Ye Naung Kyaw Kyaw

Semeth Jan

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