2019-07-14 23:44发布
shixiaogang 发表于 2014-5-11 20:57 RGB是如何实现转HSI的啊?求楼主给我一下代码,我也在做颜 {MOD}识 别
shixiaogang 发表于 2014-5-11 20:58 我需要精确的识别出红 {MOD},搞了一段时间还是没有进展啊
最多设置5个标签!
- #define min3v(v1, v2, v3) ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
- #define max3v(v1, v2, v3) ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
- typedef struct hsicolor
- {
- int16_t H;
- int16_t S;
- int16_t I;
- }HSI_ColorTypeDef;
- typedef struct rgbcolor
- {
- u8 R;
- u8 G;
- u8 B;
- }RGB_ColorTypeDef;
- //公式参考http://blog.sina.com.cn/s/blog_a5b3ed560100yo26.html
- // H>=0 && H<=360 S>=0 && S<=100 I>=0 && I<=100
- HSI_ColorTypeDef RGB565ToHSI(u16 rgb)
- {
- u16 Max,Min;
- float delta;
- RGB_ColorTypeDef RGBColor;
- HSI_ColorTypeDef HSIColor;
-
- RGBColor.R = ((rgb & 0xf800) >> 8);
- RGBColor.G = ((rgb & 0x07e0) >> 3);
- RGBColor.B = ((rgb & 0x001f) << 3); //将RGB565化为RGB888
-
- Min = min3v(RGBColor.R,RGBColor.G,RGBColor.B);
- Max = max3v(RGBColor.R,RGBColor.G,RGBColor.B);
- delta = Max - Min;
-
- if(Max == RGBColor.R)
- HSIColor.H = (int16_t)(60.0f*((RGBColor.G-RGBColor.B)/delta));
- else if(Max == RGBColor.G)
- HSIColor.H = (int16_t)(60.0f*((RGBColor.B - RGBColor.R)/delta))+120;
- else //if(Max == RGBColor.B)
- HSIColor.H = (int16_t)(60.0f*((RGBColor.R - RGBColor.G)/delta))+240;
-
- if(HSIColor.H < 0)
- HSIColor.H+=360;
-
- HSIColor.I = (int16_t)((Max + Min)*0.1961f); //I = (int16_t)((Max + Min)*1.0f/2/255.0*100);
-
- if(HSIColor.I <= 50)
- HSIColor.S = (delta/(Max + Min))*100;
- else
- if(Max != 255 || Min != 255)
- HSIColor.S = delta*100.0f/(510 - (Max + Min));
- else
- HSIColor.S = 0;
-
- return HSIColor;
- }
复制代码水平有限,精确识别做不到
一周热门 更多>