图像处理------调整亮度与饱和度

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>什么是亮度:</strong></span></p><p style="color: rgb(51, 51, 51); font-fa

什么是亮度:

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

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

什么是饱和度:

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

图像看上去更加自然。

基本思想:

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

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

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

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

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

程序效果:

滤镜源代码:

[java]  view plain copy
  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, 0, 0, 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, 0, 0, width, height, outPixels );  
  209.         return dest;  
  210.     }  
  211.   
  212. }  
相关文章
|
Docker 容器
Docker删除容器日志
Docker删除容器日志
3395 0
|
机器学习/深度学习 人工智能 自然语言处理
教育领域的AI进展:智能辅导与个性化学习的技术革新与挑战
随着人工智能技术的发展,AI Agent在教育领域的应用日益广泛,特别是在智能辅导与个性化学习方面展现出巨大潜力。通过自然语言处理、机器学习和数据分析等技术,AI可模拟个性化辅导员,根据学生的学习情况提供定制化资源与实时反馈。未来,AI Agent将更注重情感分析与跨学科培养,成为教师的有力助手,推动教育公平与效率提升。然而,数据隐私、个体差异及教育资源不平衡等问题仍需克服,以实现更智能化、全面化的教育生态。
1081 10
教育领域的AI进展:智能辅导与个性化学习的技术革新与挑战
|
12月前
|
存储 缓存 监控
NAS读取延时问题深度解析:NFS缓存机制与优化实战
本文深入解析NFS在分布式存储中的缓存一致性问题,结合电商案例,剖析客户端属性缓存导致的文件更新延迟。系统介绍acregmin、actimeo等关键参数,并提供禁用缓存、精细调优及架构升级三类实战方案,平衡性能与一致性。附监控诊断与进阶优化建议,助力生产环境高效稳定运行。(238字)
|
并行计算 量子技术 数据安全/隐私保护
量子计算与量子密码(入门级-少图版)(2)
量子计算与量子密码(入门级-少图版)(2)
1396 1
|
人工智能 前端开发 API
Gemini Coder:基于 Google Gemini API 的开源 Web 应用生成工具,支持实时编辑和预览
Gemini Coder 是一款基于 Google Gemini API 的 AI 应用生成工具,支持通过文本描述快速生成代码,并提供实时代码编辑和预览功能,简化开发流程。
1521 38
Gemini Coder:基于 Google Gemini API 的开源 Web 应用生成工具,支持实时编辑和预览
|
监控 数据可视化 API
低代码平台对接API:无需编程快速搭建电商数据中台
低代码平台助力电商企业快速搭建数据中台,通过可视化操作整合京东、淘宝等多平台API,实现库存监控、价格对比、订单分析等功能,降低技术门槛,提升运营效率。
|
编解码 Ubuntu Linux
Linux Mint vs Ubuntu:哪个更适合初学者?
由于Linux Mint只是Ubuntu的更漂亮版本,因此两种发行版的游戏功能没有太大差异。只要您拥有最新的英伟达或Mesa(AMD)驱动程序,就应该没问题。
|
小程序 Java 关系型数据库
微信记账小程序
微信记账小程序
1242 0
|
机器学习/深度学习 人工智能 算法
《C++与 SIMD 指令集:人工智能算法优化新动力》
在AI快速发展的今天,C++结合SIMD指令集成为优化算法性能的关键。SIMD通过并行处理多个数据,显著提升向量和矩阵运算速度,尤其在深度学习中表现突出。优化数据布局与算法设计,不仅提高计算效率,还降低能耗,但需注意代码的可读性和跨平台兼容性。随着技术进步,SIMD在C++中的应用将更加广泛,助力AI技术的发展。
563 12
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
1951 1