Click here to Skip to main content
Click here to Skip to main content

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

Share

About the Author

Fasil
Web Developer
Japan Japan
No Biography provided

Comments and Discussions

 
GeneralRGB to HSI Pinmembermouass25-Jun-10 14:46 
GeneralRe: RGB to HSI Pinmemberfrobertrti6-Mar-11 14:44 
GeneralThanks Pinmemberrajeshcodeproject5-Mar-06 0:07 
GeneralCRGB PinmemberKochise3-Sep-04 21:50 
GeneralAlready treated PinmemberStlan3-Sep-04 4:29 
GeneralRe: Already treated PinmemberFasil3-Sep-04 15:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 3 Sep 2004
Article Copyright 2004 by Fasil
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid