用于计算 HSI(色相、饱和度、亮度)的类






3.33/5 (8投票s)
2004年9月3日

62268
HSI 计算算法
HSI 算法
根据图像像素的三个颜色分量计算色相、饱和度和亮度值。
使用代码
实现为一个类 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; }
使用 HSI 类。
声明该类的对象,并调用该类的公共函数,提供适当的参数,如下所示。结果值馈送到三个正式参数中,即色相、饱和度和亮度。
#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 }
关注点
对于颜色处理,用于分离图像中所需的颜色区域非常有用。后续更新敬请期待…