hsvrgb.h
来自「通过使机器人进行简单的图像识别」· C头文件 代码 · 共 148 行
H
148 行
/********************************************************************
*
* =-----------------------------------------------------------------=
* = ____ _________ =
* = / _ \ \___ __/ =
* = / /_/ / ____ / / =
* = / _ \ ● / _ \ / / =
* = / /_/ // // / / // / =
* = \______//_//_/ /_//_/ =
* = =
* = Copyright (c) BIN Technology studio,2004 =
* = LET'Z BT =
* =-----------------------------------------------------------------=
*
* FileName : HSVRGB.h
* Description : HSV RGB颜色转换和关于颜色结构的定义
*
* Author : 风间苍月(TuQbasic)
* Email : tuqbasic@sohu.com
*
* Create : 2004.04.10
* LastChange : 2004.04.10
*
* History :
********************************************************************/
#pragma once
class SHSVRGB
{
public:
typedef struct _sHSV
{
float H; // 色相角(0...360]
float S; // 饱和度[0...1]
float V; // 明视度[0...1]
}HSV;
typedef struct _sInciseColor
{
// 切分阈值,HSV模型颜色上下界,注意色相是顺时针两点夹角域
HSV low;
HSV top;
}SHSVRGB::INCISECOLOR;
static unsigned int HSV2RGBEx(HSV &hsv)
{
unsigned char R, G, B;
if (hsv.S == 0)
R= G= B= hsv.V * 255;
else
{
int i = hsv.H/= 60;
float f = hsv.H - i;
float a = hsv.V * ( 1 - hsv.S) * 255;
float b = hsv.V * ( 1 - hsv.S * f) * 255;
float c = hsv.V * ( 1 - hsv.S * (1 - f)) * 255;
hsv.V*= 255;
switch(i)
{
case 6:
case 0: R = hsv.V; G = c; B = a; break;
case 1: R = b; G = hsv.V; B = a; break;
case 2: R = a; G = hsv.V; B = c; break;
case 3: R = a; G = b; B = hsv.V; break;
case 4: R = c; G = a; B = hsv.V; break;
case 5: R = hsv.V; G = a; B = b; break;
}
}
__asm
{
mov ah, 0x00;
mov al, R;
shl eax, 8;
mov al, G;
shl eax, 8;
mov al, B;
}
}
static unsigned char MaxRGB(unsigned char r, unsigned char g, unsigned char b)
{
if (r <= g) r= g;
if (r <= b) r= b;
return r;
}
static unsigned char MinRGB(unsigned char r, unsigned char g, unsigned char b)
{
if (r >= g) r= g;
if (r >= b) r= b;
return r;
}
// 颜色转换,thanks Ehomsoft
static void RGB2HSVEx(unsigned int rgb, HSV &hsv)
{
unsigned char r, g, b;
{
//r= (rgb & 0x00FF0000)>>16;
//g= (rgb & 0x0000FF00)>>8;
//b= (rgb & 0x000000FF);
__asm
{
mov eax, rgb;
mov b, al;
shr eax, 8;
mov g, al;
shr eax, 8;
mov r, al;
}
}
unsigned char cmax, cmin;
{
cmax= MaxRGB(r, g, b);
cmin= MinRGB(r, g, b);
__asm
{
}
}
if (cmax == cmin)
{
hsv.H= hsv.S= hsv.V= .0;
}
else
{
float submaxmin= cmax - cmin;
if (r == cmax)
hsv.H = (float)( g - b) / submaxmin;
else if (g == cmax)
hsv.H = 2 + (float)( b - r) / submaxmin;
else if (b == cmax)
hsv.H = 4 + (float)( r - g)/ submaxmin;
hsv.H *= 60;
if (hsv.H <= 0)
hsv.H+= 360;
hsv.S= (float) submaxmin / cmax;
hsv.V= (float) cmax / 255;
}
}
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?