zoukankan      html  css  js  c++  java
  • 颜色空间转换

    0. 结构体

    typedef struct{    //RGB格式颜色
        unsigned char  red;             // [0,255]
        unsigned char  green;           // [0,255]
        unsigned char  blue;            // [0,255]
    }COLOR_RGB;
    
    typedef struct{    //YUV格式颜色   
        unsigned char Y_hue;              // [0,240]
        unsigned char U_saturation;       // [0,240]
        unsigned char V_luminance;        // [0,240]明亮度}COLOR_YUV;
    
    typedef struct{    //LAB格式颜色    
        unsigned char L_Luminosity;         // [0,100]
        unsigned char A_ColorChannel;       // [-127,128]
        unsigned char B_ColorChannel;        // [127,128]
    }COLOR_LAB;
    
    typedef struct{    //HSL颜色格式               
        unsigned char hue;              // [0,100]
        unsigned char saturation;       // [-128,127]
        unsigned char luminance;        // [-128,127]
    }COLOR_HSL
    

    1. RGB--->YUV

    void RGBtoYUV(const COLOR_RGB *Rgb, COLOR_YUV *Yuv)
    {
      Yuv->Y_hue = 0.299 * Rgb->red + 0.587 * Rgb->green + 0.114 * Rgb->blue;
    	Yuv->U_saturation = -0.147 * Rgb->red -0.289 * Rgb->green + 0.436 * Rgb->blue;
    	Yuv->V_luminance = 0.615 * Rgb->red - 0.515 * Rgb->green - 0.100 * Rgb->blue;
    }
    

    2. RGB---->LAB(error)

    void RGBtoLAB(const COLOR_RGB *Rgb, COLOR_LAB *Lab)
    {
    	Lab->L_Luminosity = 0.2126007 * Rgb->red + 0.7151947 * Rgb->green + 0.0722046 * Rgb->blue;
    	Lab->A_ColorChannel = 0.3258962 * Rgb->red - 0.4992596 * Rgb->green + 0.1733409 * Rgb->blue + 128;
    	Lab->B_ColorChannel = 0.1218128 * Rgb->red + 0.3785610 * Rgb->green - 0.5003738 * Rgb->blue + 128;
    }
    
    http://blog.sina.com.cn/s/blog_91528c150101mwbe.html 
    
    

    3. LAB--->RGB

    void LABtoRGB(const COLOR_LAB *Lab, COLOR_RGB *Rgb)
    {
       Rgb->red = Lab->L_Luminosity + 0.0120308 * Lab->A_ColorChannel + 0.0021207 * Lab->B_ColorChannel;
       Rgb->green = Lab->L_Luminosity - 0.0035973 * Lab->A_ColorChannel - 0.0001765 * Lab->B_ColorChannel;
       Rgb->blue = Lab->L_Luminosity + 0.0002074 * Lab->A_ColorChannel - 0.0044965 * Lab->B_ColorChannel;
    }
    

    4. RGB--->HSL

    #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)))
    
    void RGBtoHSL(const COLOR_RGB *Rgb, COLOR_HSL *Hsl)
    {
      int h,s,l,maxVal,minVal,difVal;
    	int r  = Rgb->red;
    	int g  = Rgb->green;
      int b  = Rgb->blue;
    	
    	maxVal = max3v(r, g, b);
    	minVal = min3v(r, g, b);
    	
    	difVal = maxVal-minVal;
    	
    	//计算亮度
            l = (maxVal+minVal)*240/255/2;
    	
    	if(maxVal == minVal)//如果r=g=b
    	{
    		h = 0; 
    		s = 0;
    	}
    	else
    	{
                    //计算色调
    		if(maxVal==r)
    		{
    			if(g>=b)
    				h = 40*(g-b)/(difVal);
    			else
    				h = 40*(g-b)/(difVal) + 240;
    		}
    		else if(maxVal==g)
    			h = 40*(b-r)/(difVal) + 80;
    		else if(maxVal==b)
    			h = 40*(r-g)/(difVal) + 160;
    		//计算饱和度
    		if(l == 0)
    			s = 0;
    		else if(l<=120)
    			s = (difVal)*240/(maxVal+minVal);
    		else
    			s = (difVal)*240/(511 - (maxVal+minVal));
    	}
        Hsl->hue =        (unsigned char)(((h>240)? 240 : ((h<0)?0:h)));
        Hsl->saturation = (unsigned char)(((s>240)? 240 : ((s<0)?0:s)));
        Hsl->luminance =  (unsigned char)(((l>240)? 240 : ((l<0)?0:l)));
    }
    

    //声明:以上代码是笔者参考网上资源编辑而成,侵删

  • 相关阅读:
    linux常用命令
    ANAFI EXTENOED无人机(1)环境配置和基础开发
    无人机自主降落
    ROS开发(1)安装环境
    bebop无人机(1)环境配置和基础开发
    YOLO标注软件
    Python2与Python3之间切换
    python实现IOU计算
    读取多个(海康大华)网络摄像头的视频流 (使用opencv-python),解决实时读取延迟问题
    如何到外面的世界看看
  • 原文地址:https://www.cnblogs.com/Ocean-Star/p/6994678.html
Copyright © 2011-2022 走看看