13,000,324 members (90,371 online)
Add your own
alternative version

#### Stats

52.7K views
26 bookmarked
Posted 2 Sep 2004

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

, 2 Sep 2004
 Rate this:
Please Sign up or sign in to vote.
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...

## License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

## About the Author

 Web Developer Japan
No Biography provided

## Comments and Discussions

 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: 25-Jun-17 7:38 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 3 Sep 2004
Article Copyright 2004 by Fasil
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid