Click here to Skip to main content
15,884,353 members
Articles / Desktop Programming / MFC
Article

Class to calculate HSI (Hue,Saturation,Intensity)

Rate me:
Please Sign up or sign in to vote.
3.33/5 (8 votes)
2 Sep 2004 61.3K   26   6
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


Written By
Web Developer
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRGB to HSI Pin
mouass25-Jun-10 13:46
mouass25-Jun-10 13:46 
GeneralRe: RGB to HSI Pin
frobertpixto6-Mar-11 13:44
frobertpixto6-Mar-11 13:44 
GeneralThanks Pin
rajeshcodeproject4-Mar-06 23:07
rajeshcodeproject4-Mar-06 23:07 
GeneralCRGB Pin
Kochise3-Sep-04 20:50
Kochise3-Sep-04 20:50 
GeneralAlready treated Pin
Stlan3-Sep-04 3:29
Stlan3-Sep-04 3:29 
GeneralRe: Already treated Pin
Fasil3-Sep-04 14:38
Fasil3-Sep-04 14:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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