RGB与HSI空间相互转换
2009-08-01 11:15
417 查看
void HSItrans(char tp)
{
/*
** input: tp
** tp--varible of char type,it is used to judge whether HSI-RGB or RGB-HSI.
** h:[0,360]
** s:[0,1]
** I:[0,1]
** r:[0,255]
** g:[0,255]
** b:[0,255]
*/
if (myHSIArray==NULL) myHSIArray= new sHSI[myPHeight*myPWidth];
double tH,tS,fR,fG,fB;
BYTE R,G,B,tI;
int i,j;
switch (tp)
{
case 't'://RGB-HSI
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{
R=GetRValue(myPArray[i*myPWidth+j]);
G=GetGValue(myPArray[i*myPWidth+j]);
B=GetBValue(myPArray[i*myPWidth+j]);
int col1,col2,angle;
int mx,mn;
mx=max(max(R,G),B);
mn=min(min(R,G),B);
if (mx==mn)
{
myHSIArray[i*myPWidth+j].H=0;
myHSIArray[i*myPWidth+j].S=0;
}
else {
if (mn==B) {
angle=0; col1=G; col2=R;
}
else if (mn==R) {
angle=120; col1=B; col2=G;
}
else if (mn==G) {
angle=240; col1=R; col2=B;
}
myHSIArray[i*myPWidth+j].H=calchue(angle,col1,col2,mn);
myHSIArray[i*myPWidth+j].S=(/*255.0**/float(mx-mn)/float(mx));
}
myHSIArray[i*myPWidth+j].I=mx;
}
}
break;
case 'v': //con't HSI-RGB
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{
tH=myHSIArray[i*myPWidth+j].H;
tS=255.0*myHSIArray[i*myPWidth+j].S;
tI=myHSIArray[i*myPWidth+j].I;
int min,max;
tH=(360.0f*tH/252.0f);
max=tI;
if (tS==0) {
R=G=B=max;
}
else {
min=(int)((255.0-tS)*max/255.0);
if (tH<=120) {
B=min;
if (tH<=60) {
R=max;
G=(int)(min+(double)tH*(max-min)/(120.0-tH));
}
else {
G=max;
R=(int)(min+(120.0-tH)*(max-min)/tH);
}
}
else if (tH<=240) {
R=min;
if (tH<=180) {
G=max;
B=(int)(min+(tH-120.0)*(max-min)/(240.0-tH));
}
else {
B=max;
G=(int)(min+(240.0-tH)*(max-min)/(tH-120.0));
}
}
else {
G=min;
if (tH<=300) {
B=max;
R=(int)(min+(tH-240.0)*(max-min)/(360.0-tH));
}
else {
R=max;
B=(int)(min+(360.0-tH)*(max-min)/(tH-240.0));
}
}
myPArray[i*myPWidth+j]=RGB(R,G,B);
}
}
}
break;
}
}
int calchue(int angle, int col1, int col2, int min)
{
int rcode;
rcode=(int) (252.0*(angle+((120.0)*(col1-min)/
(col1+col2-(min+min))))/360.0);
return(rcode);
}
{
/*
** input: tp
** tp--varible of char type,it is used to judge whether HSI-RGB or RGB-HSI.
** h:[0,360]
** s:[0,1]
** I:[0,1]
** r:[0,255]
** g:[0,255]
** b:[0,255]
*/
if (myHSIArray==NULL) myHSIArray= new sHSI[myPHeight*myPWidth];
double tH,tS,fR,fG,fB;
BYTE R,G,B,tI;
int i,j;
switch (tp)
{
case 't'://RGB-HSI
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{
R=GetRValue(myPArray[i*myPWidth+j]);
G=GetGValue(myPArray[i*myPWidth+j]);
B=GetBValue(myPArray[i*myPWidth+j]);
int col1,col2,angle;
int mx,mn;
mx=max(max(R,G),B);
mn=min(min(R,G),B);
if (mx==mn)
{
myHSIArray[i*myPWidth+j].H=0;
myHSIArray[i*myPWidth+j].S=0;
}
else {
if (mn==B) {
angle=0; col1=G; col2=R;
}
else if (mn==R) {
angle=120; col1=B; col2=G;
}
else if (mn==G) {
angle=240; col1=R; col2=B;
}
myHSIArray[i*myPWidth+j].H=calchue(angle,col1,col2,mn);
myHSIArray[i*myPWidth+j].S=(/*255.0**/float(mx-mn)/float(mx));
}
myHSIArray[i*myPWidth+j].I=mx;
}
}
break;
case 'v': //con't HSI-RGB
for(i=0; i<(int)myPHeight; i++)
{
for (j=0; j<(int)myPWidth; j++)
{
tH=myHSIArray[i*myPWidth+j].H;
tS=255.0*myHSIArray[i*myPWidth+j].S;
tI=myHSIArray[i*myPWidth+j].I;
int min,max;
tH=(360.0f*tH/252.0f);
max=tI;
if (tS==0) {
R=G=B=max;
}
else {
min=(int)((255.0-tS)*max/255.0);
if (tH<=120) {
B=min;
if (tH<=60) {
R=max;
G=(int)(min+(double)tH*(max-min)/(120.0-tH));
}
else {
G=max;
R=(int)(min+(120.0-tH)*(max-min)/tH);
}
}
else if (tH<=240) {
R=min;
if (tH<=180) {
G=max;
B=(int)(min+(tH-120.0)*(max-min)/(240.0-tH));
}
else {
B=max;
G=(int)(min+(240.0-tH)*(max-min)/(tH-120.0));
}
}
else {
G=min;
if (tH<=300) {
B=max;
R=(int)(min+(tH-240.0)*(max-min)/(360.0-tH));
}
else {
R=max;
B=(int)(min+(360.0-tH)*(max-min)/(tH-240.0));
}
}
myPArray[i*myPWidth+j]=RGB(R,G,B);
}
}
}
break;
}
}
int calchue(int angle, int col1, int col2, int min)
{
int rcode;
rcode=(int) (252.0*(angle+((120.0)*(col1-min)/
(col1+col2-(min+min))))/360.0);
return(rcode);
}
相关文章推荐
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- 数字图像处理 颜色空间RGB、HSI、CMYK、YUV的相互转换
- 数字图像处理 颜色空间RGB、HSI、CMYK、YUV的相互转换
- HSI颜色空间和RGB颜色空间之间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- OpenCV实现RGB颜色空间和HSI颜色空间的相互转换
- RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)
- opencv实现将RGB图像转换到HSI空间
- HSV颜色空间 与 RGB 颜色空间的相互转换
- VC编程实现色彩空间RGB与HSB(HSV)相互转换
- 使用OpenCV实现RGB、HSI、CMYK颜色空间的转换
- photoshop中RGB与LAB空间的相互转换
- 图像的RGB颜色空间和HSI空间的转换
- 基于OpenCV的RGB和HSV色彩空间相互转换C++程序
- OPENCV中 RGB 转换到 HSI空间
- Matlab中RGB和HSI的相互转换
- Matlab中RGB和HSI的相互转换
- RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)