zoukankan      html  css  js  c++  java
  • 图像处理------调整亮度与饱和度 分类: 视频图像处理 2015-07-24 14:50 31人阅读 评论(0) 收藏

    什么是亮度:

    简单点说一幅图像的亮度属性是图像的RGB值的大小,RGB各个值越大亮度越高RGB

    分量取值范围为0~255之间。调整图像亮度。

    什么是饱和度:

    饱和度是是指颜色的强度,调整饱和度可以修正过度曝光或者未充分曝光的图片。使

    图像看上去更加自然。

    基本思想:

    通常在RGB色彩空间调整亮度与饱和度不是很直观,而HSL彩色空可以很直观表示出

    每个像素的饱和度与亮度。所以首先读取图像的像素RGB值然后再转换到HSL空间得

    到饱和度与亮度值,调整以后再从HSL空间转换到RGB空间的RGB值,对每个像素完

    成这样的调整就完成图像的亮度与饱和度调整。关于RGB与HSL色彩空间的转换

    看这里:http://en.wikipedia.org/wiki/HSL_color_space

    程序效果:

    滤镜源代码:

    1. package com.gloomyfish.filter.study;  
    2.   
    3. import java.awt.image.BufferedImage;  
    4. /** 
    5.  * http://en.wikipedia.org/wiki/HSL_color_space 
    6.  * @author gloomy fish 
    7.  * @date 2012-09-26  
    8.  * 
    9.  */  
    10. public class HSLFilter extends AbstractBufferedImageOp {  
    11.     public final static double c1o60  = 1.0 / 60.0;  
    12.     public final static double c1o255 = 1.0 / 255.0;  
    13.     private double hue;  
    14.     private double saturation;  
    15.     private double lightness;  
    16.       
    17.     public HSLFilter() {  
    18.         System.out.println("Hue Filter");  
    19.     }  
    20.       
    21.     public double getHue() {  
    22.         return hue;  
    23.     }  
    24.   
    25.     public void setHue(double hue) {  
    26.         while (hue < 0.0) {  
    27.             this.hue += 360;  
    28.         }  
    29.         while (hue >= 360.0) {  
    30.             this.hue -= 360;  
    31.         }  
    32.     }  
    33.   
    34.     public double getSaturation() {  
    35.         return saturation;  
    36.     }  
    37.   
    38.     public void setSaturation(double saturation) {  
    39.         if((saturation >= -100.0) && (saturation <= 100.0)) {  
    40.             this.saturation = saturation;  
    41.         }  
    42.     }  
    43.   
    44.     public double getLightness() {  
    45.         return lightness;  
    46.     }  
    47.   
    48.     public void setLightness(double lightness) {  
    49.         if((lightness >= -100.0) && (lightness <= 100.0)) {  
    50.             this.lightness = lightness;  
    51.         }  
    52.     }  
    53.   
    54.     @Override  
    55.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    56.         int width = src.getWidth();  
    57.         int height = src.getHeight();  
    58.         double sat = 127.0d * saturation / 100.0d;  
    59.         double lum = 127.0d * lightness / 100.0d;  
    60.         if ( dest == null )  
    61.             dest = createCompatibleDestImage( src, null );  
    62.   
    63.         int[] inPixels = new int[width*height];  
    64.         int[] outPixels = new int[width*height];  
    65.         getRGB( src, 00, width, height, inPixels );  
    66.         double min, max, dif, sum;  
    67.         double f1, f2;  
    68.         int index = 0;  
    69.         double h, s, l;  
    70.         double v1, v2, v3, h1;  
    71.         for(int row=0; row<height; row++) {  
    72.             int ta = 0, tr = 0, tg = 0, tb = 0;  
    73.             for(int col=0; col<width; col++) {  
    74.                 index = row * width + col;  
    75.                 ta = (inPixels[index] >> 24) & 0xff;  
    76.                 tr = (inPixels[index] >> 16) & 0xff;  
    77.                 tg = (inPixels[index] >> 8) & 0xff;  
    78.                 tb = inPixels[index] & 0xff;  
    79.                   
    80.                 // convert to HSL space  
    81.                 min = tr;  
    82.                 if (tg < min)   
    83.                     min = tg;  
    84.                 if (tb < min)   
    85.                     min = tb;  
    86.                 max = tr;  
    87.                 f1 = 0.0;  
    88.                 f2 = tg - tb;  
    89.                 if (tg > max) {  
    90.                    max = tg;  
    91.                    f1 = 120.0;  
    92.                    f2 = tb - tr;  
    93.                 }  
    94.                 if (tb > max) {  
    95.                    max = tb;  
    96.                    f1 = 240.0;  
    97.                    f2 = tr - tg;  
    98.                 }  
    99.                 dif = max - min;  
    100.                 sum = max + min;  
    101.                 l = 0.5 * sum;  
    102.                 if (dif == 0) {  
    103.                     h = 0.0;  
    104.                     s = 0.0;  
    105.                 }   
    106.                 else if(l < 127.5) {  
    107.                     s = 255.0 * dif / sum;  
    108.                 }  
    109.                 else {  
    110.                     s = 255.0 * dif / (510.0 - sum);  
    111.                 }  
    112.   
    113.                 h = (f1 + 60.0 * f2 / dif);  
    114.                 if (h < 0.0) {   
    115.                     h += 360.0;  
    116.                 }  
    117.                 if (h >= 360.0) {  
    118.                     h -= 360.0;  
    119.                 }  
    120.                   
    121.                 // Apply transformation.  
    122.                 h = h + hue;  
    123.                 if( h >= 360.0) {  
    124.                     h = h - 360.0;  
    125.                 }  
    126.                 s = s + sat;  
    127.                 if( s < 0.0) {  
    128.                     s = 0.0;  
    129.                 }  
    130.                 if( s > 255.0) {  
    131.                     s = 255.0;  
    132.                 }  
    133.                 l = l + lum;  
    134.                 if( l < 0.0) {  
    135.                     l = 0.0;  
    136.                 }  
    137.                 if( l > 255.0) {  
    138.                     l = 255.0;  
    139.                 }  
    140.                   
    141.                 // conversion back to RGB space here!!  
    142.                 if (s == 0) {  
    143.                    tr = (int)l;  
    144.                    tg = (int)l;  
    145.                    tb = (int)l;  
    146.                 } else {  
    147.                   
    148.                    if (l < 127.5) {  
    149.                       v2 = c1o255 * l * (255 + s);  
    150.                    } else {  
    151.                       v2 = l + s - c1o255 * s * l;  
    152.                    }  
    153.                      
    154.                    v1 = 2 * l - v2;  
    155.                    v3 = v2 - v1;  
    156.                    h1 = h + 120.0;  
    157.                    if (h1 >= 360.0)   
    158.                        h1 -= 360.0;  
    159.                      
    160.                    if (h1 < 60.0) {  
    161.                       tr = (int)(v1 + v3 * h1 * c1o60);  
    162.                    }  
    163.                    else if (h1 < 180.0) {  
    164.                       tr = (int)v2;  
    165.                    }  
    166.                    else if (h1 < 240.0) {  
    167.                       tr = (int)(v1 + v3 * (4 - h1 * c1o60));  
    168.                    }  
    169.                    else {  
    170.                       tr = (int)v1;  
    171.                    }  
    172.                      
    173.                    h1 = h;  
    174.                    if (h1 < 60.0) {  
    175.                       tg = (int)(v1 + v3 * h1 * c1o60);  
    176.                    }  
    177.                    else if (h1 < 180.0) {  
    178.                       tg = (int)v2;  
    179.                    }   
    180.                    else if (h1 < 240.0) {  
    181.                       tg = (int)(v1 + v3 * (4 - h1 * c1o60));  
    182.                    }  
    183.                    else {  
    184.                       tg = (int)v1;  
    185.                    }  
    186.                      
    187.                    h1 = h - 120.0;  
    188.                    if (h1 < 0.0) {  
    189.                        h1 += 360.0;  
    190.                    }  
    191.                    if (h1 < 60.0) {  
    192.                       tb = (int)(v1 + v3 * h1 * c1o60);  
    193.                    }  
    194.                    else if (h1 < 180.0) {  
    195.                       tb = (int)v2;  
    196.                    }  
    197.                    else if (h1 < 240.0) {  
    198.                       tb = (int)(v1 + v3 * (4 - h1 * c1o60));  
    199.                    }  
    200.                    else {  
    201.                       tb = (int)v1;  
    202.                    }  
    203.                 }  
    204.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
    205.             }  
    206.         }  
    207.   
    208.         setRGB( dest, 00, width, height, outPixels );  
    209.         return dest;  
    210.     }  
    211.   
    212. }  

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    .NET 事件模型教程(一)
    [转帖]Predicate的用法
    MSBuild入门(续)
    浅析C# 中object sender与EventArgs e
    调用一个按钮的Click事件(利用反射)
    Delegate,Action,Func,匿名方法,匿名委托,事件
    投票系统如何防止一个用户多次投票
    如何发送表单
    SharePoint NLB选Unicast还是选Multicast?
    为SharePoint的多台WFE配置Windows自带的NLB遇到的一个问题
  • 原文地址:https://www.cnblogs.com/mao0504/p/4705513.html
Copyright © 2011-2022 走看看