12,360,217 members (62,201 online)
alternative version

51.1K views
26 bookmarked
Posted

# Class to calculate HSI (Hue,Saturation,Intensity)

, 2 Sep 2004
 Rate this:
Algorithm for HSI calculation

## 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`.

```//Declaration of class CHsi
// HSI.h file
//
#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// __HSI_H__```
```//Implementation of class CHsi
#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; //Declaration of the class
objHsi.HSI (255,251,25,Hue,Saturation,Intensity);//Calling the HSI function
}```

## Points of Interest

Useful for color processing to separate the desired color area in an image. Updates coming soon...

A list of licenses authors might use can be found here

## Share

 Web Developer Japan
No Biography provided

## You may also be interested in...

 First Prev Next
 RGB to HSI mouass25-Jun-10 13:46 mouass 25-Jun-10 13:46
 Re: RGB to HSI frobertrti6-Mar-11 13:44 frobertrti 6-Mar-11 13:44
 Thanks rajeshcodeproject4-Mar-06 23:07 rajeshcodeproject 4-Mar-06 23:07
 CRGB Kochise3-Sep-04 20:50 Kochise 3-Sep-04 20:50
 Already treated Stlan3-Sep-04 3:29 Stlan 3-Sep-04 3:29
 Re: Already treated Fasil3-Sep-04 14:38 Fasil 3-Sep-04 14:38
 Last Visit: 31-Dec-99 18:00     Last Update: 1-Jul-16 8:11 Refresh 1