天天看點

圖像處理------調整亮度與飽和度

什麼是亮度:

簡單點說一幅圖像的亮度屬性是圖像的rgb值的大小,rgb各個值越大亮度越高rgb

分量取值範圍為0~255之間。調整圖像亮度。

什麼是飽和度:

飽和度是是指顔色的強度,調整飽和度可以修正過度曝光或者未充分曝光的圖檔。使

圖像看上去更加自然。

基本思想:

通常在rgb色彩空間調整亮度與飽和度不是很直覺,而hsl彩色空可以很直覺表示出

每個像素的飽和度與亮度。是以首先讀取圖像的像素rgb值然後再轉換到hsl空間得

到飽和度與亮度值,調整以後再從hsl空間轉換到rgb空間的rgb值,對每個像素完

成這樣的調整就完成圖像的亮度與飽和度調整。關于rgb與hsl色彩空間的轉換

看這裡:http://en.wikipedia.org/wiki/hsl_color_space

程式效果:

圖像處理------調整亮度與飽和度

濾鏡源代碼:

package com.gloomyfish.filter.study;  

import java.awt.image.bufferedimage;  

/** 

 * http://en.wikipedia.org/wiki/hsl_color_space 

 * @author gloomy fish 

 * @date 2012-09-26  

 * 

 */  

public class hslfilter extends abstractbufferedimageop {  

    public final static double c1o60  = 1.0 / 60.0;  

    public final static double c1o255 = 1.0 / 255.0;  

    private double hue;  

    private double saturation;  

    private double lightness;  

    public hslfilter() {  

        system.out.println("hue filter");  

    }  

    public double gethue() {  

        return hue;  

    public void sethue(double hue) {  

        while (hue < 0.0) {  

            this.hue += 360;  

        }  

        while (hue >= 360.0) {  

            this.hue -= 360;  

    public double getsaturation() {  

        return saturation;  

    public void setsaturation(double saturation) {  

        if((saturation >= -100.0) && (saturation <= 100.0)) {  

            this.saturation = saturation;  

    public double getlightness() {  

        return lightness;  

    public void setlightness(double lightness) {  

        if((lightness >= -100.0) && (lightness <= 100.0)) {  

            this.lightness = lightness;  

    @override  

    public bufferedimage filter(bufferedimage src, bufferedimage dest) {  

        int width = src.getwidth();  

        int height = src.getheight();  

        double sat = 127.0d * saturation / 100.0d;  

        double lum = 127.0d * lightness / 100.0d;  

        if ( dest == null )  

            dest = createcompatibledestimage( src, null );  

        int[] inpixels = new int[width*height];  

        int[] outpixels = new int[width*height];  

        getrgb( src, 0, 0, width, height, inpixels );  

        double min, max, dif, sum;  

        double f1, f2;  

        int index = 0;  

        double h, s, l;  

        double v1, v2, v3, h1;  

        for(int row=0; row<height; row++) {  

            int ta = 0, tr = 0, tg = 0, tb = 0;  

            for(int col=0; col<width; col++) {  

                index = row * width + col;  

                ta = (inpixels[index] >> 24) & 0xff;  

                tr = (inpixels[index] >> 16) & 0xff;  

                tg = (inpixels[index] >> 8) & 0xff;  

                tb = inpixels[index] & 0xff;  

                // convert to hsl space  

                min = tr;  

                if (tg < min)   

                    min = tg;  

                if (tb < min)   

                    min = tb;  

                max = tr;  

                f1 = 0.0;  

                f2 = tg - tb;  

                if (tg > max) {  

                   max = tg;  

                   f1 = 120.0;  

                   f2 = tb - tr;  

                }  

                if (tb > max) {  

                   max = tb;  

                   f1 = 240.0;  

                   f2 = tr - tg;  

                dif = max - min;  

                sum = max + min;  

                l = 0.5 * sum;  

                if (dif == 0) {  

                    h = 0.0;  

                    s = 0.0;  

                }   

                else if(l < 127.5) {  

                    s = 255.0 * dif / sum;  

                else {  

                    s = 255.0 * dif / (510.0 - sum);  

                h = (f1 + 60.0 * f2 / dif);  

                if (h < 0.0) {   

                    h += 360.0;  

                if (h >= 360.0) {  

                    h -= 360.0;  

                // apply transformation.  

                h = h + hue;  

                if( h >= 360.0) {  

                    h = h - 360.0;  

                s = s + sat;  

                if( s < 0.0) {  

                if( s > 255.0) {  

                    s = 255.0;  

                l = l + lum;  

                if( l < 0.0) {  

                    l = 0.0;  

                if( l > 255.0) {  

                    l = 255.0;  

                // conversion back to rgb space here!!  

                if (s == 0) {  

                   tr = (int)l;  

                   tg = (int)l;  

                   tb = (int)l;  

                } else {  

                   if (l < 127.5) {  

                      v2 = c1o255 * l * (255 + s);  

                   } else {  

                      v2 = l + s - c1o255 * s * l;  

                   }  

                   v1 = 2 * l - v2;  

                   v3 = v2 - v1;  

                   h1 = h + 120.0;  

                   if (h1 >= 360.0)   

                       h1 -= 360.0;  

                   if (h1 < 60.0) {  

                      tr = (int)(v1 + v3 * h1 * c1o60);  

                   else if (h1 < 180.0) {  

                      tr = (int)v2;  

                   else if (h1 < 240.0) {  

                      tr = (int)(v1 + v3 * (4 - h1 * c1o60));  

                   else {  

                      tr = (int)v1;  

                   h1 = h;  

                      tg = (int)(v1 + v3 * h1 * c1o60);  

                      tg = (int)v2;  

                   }   

                      tg = (int)(v1 + v3 * (4 - h1 * c1o60));  

                      tg = (int)v1;  

                   h1 = h - 120.0;  

                   if (h1 < 0.0) {  

                       h1 += 360.0;  

                      tb = (int)(v1 + v3 * h1 * c1o60);  

                      tb = (int)v2;  

                      tb = (int)(v1 + v3 * (4 - h1 * c1o60));  

                      tb = (int)v1;  

                outpixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  

            }  

        setrgb( dest, 0, 0, width, height, outpixels );  

        return dest;  

}  

繼續閱讀