📄 mycolorspace.cpp
字号:
// MyColorSpace.cpp: implementation of the MyColorSpace class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "myimagedb.h"
#include "MyColorSpace.h"
#include <MATH.H>
#include "mymath.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Coefficient matrix for xyz and rgb spaces
static const int XYZ[3][3] = { { 4125, 3576, 1804 },
{ 2125, 7154, 721 },
{ 193, 1192, 9502 } };
static const double RGB[3][3] = {
{ (float)3.2405, (float)-1.5371, (float)-0.4985 },
{(float)-0.9693, (float)1.8760, (float)0.0416 },
{ (float)0.0556, (float)-0.2040, (float)1.0573 } };
// Constants for LUV transformation
static const float Xn = (float)0.9505;
static const float Yn = (float)1.0;
static const float Zn = (float)1.0888;
static const float Un_prime = (float)0.1978;
static const float Vn_prime = (float)0.4683;
static const float Lt = (float)0.008856;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MyColorSpace::MyColorSpace()
{
}
MyColorSpace::~MyColorSpace()
{
}
BOOL MyColorSpace::Rgb2Hsi(FLOAT r, FLOAT g, FLOAT b, FLOAT& h, FLOAT& s, FLOAT& i)
//冈萨雷斯《数字图像处理》第二版,p235-p237;
{
//有问题;
/*
h = r;
s = g;
i = b;
*/
FLOAT temp1 = (FLOAT) ( 0.5 * ( (r-g) + (r-b) ) );
FLOAT temp2 = (FLOAT) sqrt( (r-g)*(r-g) + (r-g)*(g-b) );
if (temp1>temp2)
{
temp1=temp2=1;//本来不应该,但确实会有这种情况,晕!
}
FLOAT sita = (FLOAT) acos( temp1/temp2 );
if (b<=g)
{
h = sita;
}else
{
h = 360 - sita;
}
if (h>=360)
{
h = h - 360;
}
s = (FLOAT) ( 1 - 3.0 * myMath.ReturnMinInThree(r, g, b) / (r + g + b) );
i = (FLOAT) ( (r + g + b) / 3.0 );
return TRUE;
}
BOOL MyColorSpace::Hsi2Rgb(FLOAT h, FLOAT s, FLOAT i, FLOAT& r, FLOAT& g, FLOAT& b)
//冈萨雷斯《数字图像处理》第二版,p237;
{
/*
r = h;
g = s;
b = i;
*/
if (h<0||h>=360)
{
return FALSE;
}
if (h<120)
{
b = i * ( 1 - s);
r = (FLOAT) ( i * ( 1 + s*cos(h) / cos(60.0-h) ) );
g = 1 - ( r+b );
return TRUE;
}
if (h<240)
{
h = h - 120;
r = i * ( 1 - s);
g = (FLOAT) ( i * ( 1 + s*cos(h) / cos(60.0-h) ) );
b = 1 - ( r+g );
return TRUE;
}
if (h<360)
{
h = h - 240;
g = i * ( 1 - s);
b = (FLOAT) ( i * ( 1 + s*cos(h) / cos(60.0-h) ) );
r = 1 - ( g+b );
return TRUE;
}
return FALSE;
}
BOOL MyColorSpace::Rgb2Ycbcr(FLOAT r, FLOAT g, FLOAT b, FLOAT& y, FLOAT& cb, FLOAT& cr)
//据《Color and Texture Descriptors》by B.S.Manjunath,eg.
{
y = (FLOAT) ( 0.2989*r + 0.5866*g + 0.1145*b );
cb = (FLOAT) ( -0.1687*r - 0.3312*g + 0.5000*b );
cr = (FLOAT) ( 0.5000*r - 0.4183*g - 0.0816*b );
return TRUE;
}
BOOL MyColorSpace::Rgb2Hsv(FLOAT r, FLOAT g, FLOAT b, FLOAT& h, FLOAT& s, FLOAT& v)
//据《Color and Texture Descriptors》by B.S.Manjunath,eg.
{
//无定义(非彩色)时,h返回0;
FLOAT tempmax = (FLOAT) myMath.ReturnMaxInThree(r, g, b);
FLOAT tempmin = (FLOAT) myMath.ReturnMinInThree(r, g, b);
v = tempmax;
if (tempmax == 0)
{
s = 0;
}else
{
s = (tempmax - tempmin) / tempmax;
}
if (tempmax == tempmin)
{
h = 0;//undefined(achromatic color);
}else
{
if (tempmax==r && g>b)
{
h = 60*(g-b) / (tempmax - tempmin);
}else if (tempmax==r &&g<b)
{
h = 360 + 60*(g-b) / (tempmax-tempmin);
}else if (g==tempmax)
{
h = (FLOAT) ( 60 * ( 2.0 + (b-r) / (tempmax-tempmin) ) );
}else
{
h = (FLOAT) ( 60 * ( 4.0 + (r-g) / (tempmax-tempmin) ) );
}
}
return TRUE;
}
BOOL MyColorSpace::Rgb2Hsv2(FLOAT r, FLOAT g, FLOAT b, FLOAT& h, FLOAT& s, FLOAT& v)
//姚子程序中的转换,和上面书中的的不一样???
{
if (r==g && r==b)
{
h = 0;
}else
{
h = (FLOAT) acos( (2*r-g-b) / ( 2.0 * sqrt( pow((r-g),2.0) + (r-b)*(g-b) ) ) );
}
if (b>g)
{
h = (FLOAT) ( 2*PI - h );
}
s = (FLOAT) ( ( __max( r, __max(g,b) ) - __min( r, __min(g,b) ) ) / 255 );
v = (FLOAT) ( (r+g+b) / (3.0*255) );
return TRUE;
}
BOOL MyColorSpace::XyztoRgb(FLOAT* xyzDatas, int width, int height, BYTE* rgbDatas)
{
if (NULL == rgbDatas)
{
CString tempstr;
tempstr.Format("传入内存未分配,在MyColorSpace::RGBtoHSV");
AfxMessageBox(tempstr,NULL,MB_OK);
return FALSE;
}
int i,j;
FLOAT r,g,b;
FLOAT x, y, z;
unsigned long pos;
for(i=0;i<height;i++)
{
for (j=0;j<width;j++)
{
pos = (i*width+j) * 3;
x = xyzDatas[pos];
y = xyzDatas[pos+1];
z = xyzDatas[pos+2];
Xyz2Rgb(x, y, z, r, g, b);
rgbDatas[pos] = (BYTE) ( b*255 );
rgbDatas[pos+1] = (BYTE) ( g*255 );
rgbDatas[pos+2] = (BYTE) ( r*255 );
}
}
return TRUE;
}
BOOL MyColorSpace::RgbtoXyz(BYTE* inDatas, int width, int height, FLOAT* xyzbuff)
//将位图图像数据转换为HSV数据;
{
if (NULL == xyzbuff)
{
CString tempstr;
tempstr.Format("传入内存未分配,在MyColorSpace::RGBtoHSV");
AfxMessageBox(tempstr,NULL,MB_OK);
return FALSE;
}
int i, j;
FLOAT r, g, b;
FLOAT x, y, z;
unsigned long pos;
for(i=0;i<height;i++)
{
for (j=0;j<width;j++)
{
pos = (i*width+j) * 3;
r = (FLOAT) ( (FLOAT)inDatas[pos+2] / 255. );
g = (FLOAT) ( (FLOAT)inDatas[pos+1] / 255. );
b = (FLOAT) ( (FLOAT)inDatas[pos] / 255. );
Rgb2Xyz(r, g, b, x, y, z);
xyzbuff[pos] = x;
xyzbuff[pos+1] = y;
xyzbuff[pos+2] = z;
}
}
return TRUE;
}
BOOL MyColorSpace::RgbtoHsv(BYTE* inDatas, int width, int height, MyHSV* outHSV)
//将位图图像数据转换为HSV数据;
{
if (NULL == outHSV)
{
CString tempstr;
tempstr.Format("传入内存未分配,在MyColorSpace::RGBtoHSV");
AfxMessageBox(tempstr,NULL,MB_OK);
return FALSE;
}
int i,j;
FLOAT r,g,b;
unsigned long pos;
for(i=0;i<height;i++)
{
for (j=0;j<width;j++)
{
pos = i*width+j;
r = (FLOAT) ( (FLOAT)inDatas[3*pos+2] / 255. );
g = (FLOAT) ( (FLOAT)inDatas[3*pos+1] / 255. );
b = (FLOAT) ( (FLOAT)inDatas[3*pos] / 255. );
FLOAT h, s, v;
Rgb2Hsv(r, g, b, h, s, v);
outHSV[pos].h = h;
outHSV[pos].s = s;
outHSV[pos].v = v;
}
}
return TRUE;
}
DOUBLE MyColorSpace::GetHsvDistance(MyHSV x, MyHSV y)
{
//距离在0与sqrt(5)之间;
//来自姚子程序RemoteDemo;
double m_d, v, sc, ss;
v = pow(x.v-y.v, 2);
sc = pow( x.s*cos(x.h)-y.s*cos(y.h), 2 );
ss = pow( x.s*sin(x.h)-y.s*sin(y.h), 2 );
m_d = sqrt( v+sc+ss );
return m_d;
}
/*
BOOL MyColorSpace::Luv2Rgb(FLOAT l, FLOAT u, FLOAT v, FLOAT& r, FLOAT& g, FLOAT& b)
//来自D. Comaniciu, P. Meer,Robust Analysis of Feature Spaces: Color Image Segmentation 的相应代码;
{
if(v<0.1)
{
r=0;
g=0;
b=0;
}
else
{
float my_x, my_y, my_z;
if(v< 8.0)
my_y = (float) ( Yn * v / 903.3 );
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -