HSI algorithm
Calculates the hue, saturation and intensity values based on the three color components of an image pixel.
Using the code
Implemented as a class CHsi
.
#ifndef __HSI_H__
#define __HSI_H__
#include <math.h>
#define IN
#define OUT
#define PI 3.14159
#define _Max(x,y) ((x)>(y) ? (x) : (y))
#define _Min(x,y) ((x)<(y) ? (x) : (y))
class CHsi
{
public:
CHsi();
virtual ~CHsi();
void HSI(IN unsigned int r,IN unsigned int g,IN unsigned int b,\
OUT double &Hue,OUT double &Saturation,OUT double &Intensity);
private:
unsigned int nImax,nImin,nSum,nDifference;
};
#endif
#include "Hsi.h"
CHsi::CHsi()
{
}
CHsi::~CHsi()
{
}
void CHsi::HSI(IN unsigned int r,IN unsigned int g,IN unsigned int b,
OUT double &Hue,OUT double &Saturation,OUT double &Intensity)
{
if( (r<0 && g<0 && b<0) || (r>255 || g>255 || b>255) )
{
Hue=Saturation=Intensity=0;
return;
}
if(g==b)
{
if(b<255)
{
b=b+1;
}
else
{
b=b-1;
}
}
nImax = _Max(r,b);
nImax = _Max(nImax,g);
nImin = _Min(r,b);
nImin = _Min(nImin,g);
nSum = nImin+nImax;
nDifference =nImax-nImin;
Intensity = (float)nSum/2;
if(Intensity<128)
{
Saturation=(255*((float)nDifference/nSum));
}
else
{
Saturation=(float)(255*((float)nDifference/(510-nSum)));
}
if(Saturation!=0)
{
if(nImax == r)
{
Hue=(60*((float)g-(float)b)/nDifference);
}
else if(nImax == g)
{
Hue=(60*((float)b-(float)r)/nDifference+120);
}
else if(nImax == b)
{
Hue=(60*((float)r-(float)g)/nDifference+240);
}
if(Hue<0)
{
Hue=(60*((float)b-(float)r)/nDifference+120);
}
}
else
{
Hue=-1;
}
return;
}
Using the HSI class.
Declare an object of the class and call the public function of the class feeding the appropriate parameters as shown below. Result values are fed in the three formal parameters namely Hue, Saturation and Intensity.
#include "Hsi.h"
void main()
{
int HSINo=5;
double Hue,Saturation,Intensity;
CHsi objHsi;
objHsi.HSI (255,251,25,Hue,Saturation,Intensity);
}
Points of Interest
Useful for color processing to separate the desired color area in an image.
Updates coming soon...