图像处理------光源退化效果

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-size: 18px;">基本思想:</span></strong></p><p style="color: rgb(51, 51, 51); font-fam

基本思想:

RGB像素的亮度是由RGB各个分量值的大小决定的,分量越大,亮度越大。看上去

好像光照效果越明显,光源退化效果是模拟光照在图像的中心点上,慢慢扩散到周

围,越靠近中心点像素,图像越亮,越远离图像越暗。原理可以说是非常的简单,

只要计算图像中每个像素到中心像素的欧几里德距离,归一化以后得到scale值(0

到1之间)乘以原来的RGB像素值即得到每个像素处理以后的RGB像素值。

效果如下:


关键代码解释:

中心像素点坐标取得:

int centerX = width/2;

int centerY = height/2;

 

任意一个像素点到中心像素的距离计算:

double xx = (centerX - px)*(centerX - px);

double yy = (centerY - py)*(centerY - py);

return (int)Math.sqrt(xx + yy);

 

距离归一化以及衰减因子考虑:

double scale = 1.0 - getDistance(centerX, centerY, col,row)/maxDistance;

for(int i=0; i<factor; i++) {

scale = scale * scale;

}

计算每个像素点的新RGB值:

tr = (int)(scale * tr);

tg = (int)(scale * tg);

tb = (int)(scale * tb);

滤镜源代码如下:

[java]  view plain copy
  1. package com.gloomyfish.filter.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4.   
  5. public class SpotlightFilter extends AbstractBufferedImageOp {  
  6.     // attenuation coefficient, default is 1 means line decrease...  
  7.     private int factor;  
  8.     public SpotlightFilter() {  
  9.         factor = 1;  
  10.     }  
  11.       
  12.     public void setFactor(int coefficient) {  
  13.         this.factor = coefficient;  
  14.     }  
  15.   
  16.     @Override  
  17.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  18.         int width = src.getWidth();  
  19.         int height = src.getHeight();  
  20.   
  21.         if ( dest == null )  
  22.             dest = createCompatibleDestImage( src, null );  
  23.   
  24.         int[] inPixels = new int[width*height];  
  25.         int[] outPixels = new int[width*height];  
  26.         getRGB( src, 0, 0, width, height, inPixels );  
  27.         int index = 0;  
  28.         int centerX = width/2;  
  29.         int centerY = height/2;  
  30.         double maxDistance = Math.sqrt(centerX * centerX + centerY * centerY);  
  31.         for(int row=0; row<height; row++) {  
  32.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  33.             for(int col=0; col<width; col++) {  
  34.                 index = row * width + col;  
  35.                 ta = (inPixels[index] >> 24) & 0xff;  
  36.                 tr = (inPixels[index] >> 16) & 0xff;  
  37.                 tg = (inPixels[index] >> 8) & 0xff;  
  38.                 tb = inPixels[index] & 0xff;  
  39.                 double scale = 1.0 - getDistance(centerX, centerY, col, row)/maxDistance;  
  40.                 for(int i=0; i<factor; i++) {  
  41.                     scale = scale * scale;  
  42.                 }  
  43.   
  44.                 tr = (int)(scale * tr);  
  45.                 tg = (int)(scale * tg);  
  46.                 tb = (int)(scale * tb);  
  47.                   
  48.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  49.                   
  50.             }  
  51.         }  
  52.         setRGB( dest, 0, 0, width, height, outPixels );  
  53.         return dest;  
  54.     }  
  55.       
  56.     private double getDistance(int centerX, int centerY, int px, int py) {  
  57.         double xx = (centerX - px)*(centerX - px);  
  58.         double yy = (centerY - py)*(centerY - py);  
  59.         return (int)Math.sqrt(xx + yy);  
  60.     }  
  61.   
  62. }  
相关文章
|
移动开发 JavaScript 前端开发
【Html.js——图片折叠效果】折叠手风琴(蓝桥杯真题-1763)【合集】
本项目实现了一个图片折叠手风琴效果,使用jQuery完成。主要包括以下部分: - **介绍**:任务是通过点击图片实现折叠和展开的效果。 - **准备**:内置初始代码,包含 `css/style.css`、`images/` 文件夹、`js/` 文件夹及 `index.html` 等文件。启动 Web Server 服务可运行项目。 - **目标**:完善 `index.js` 文件,使页面达到预期的折叠效果。 - **规定**:严格按步骤操作,保持默认文件结构不变,并在完成后保持 Web 服务正常访问状态。 - **通关代码**:使用 jQuery 实现点击事件,为选中元素添加 `act
581 19
|
存储 缓存 NoSQL
MySQL-分库分表初探
MySQL-分库分表初探
335 0
|
Perl 流计算 内存技术
AXI总线简介
<p style="margin-top: 14px; margin-bottom: 14px; padding-top: 0px; padding-bottom: 0px; color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px; border: 0px;
5583 0
|
资源调度 算法 Java
图像处理------Canny边缘检测
<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-fami
2117 0
|
算法 Java 计算机视觉
图像处理------K-Means算法演示
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">一:数学原理</span></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font
1846 0
|
算法 Java 数据挖掘
图像处理------Fuzzy C Means的聚合算法
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Fuzzy C-Means聚合算法在图像分割(segmentation)和图像视觉处理中常常被用到聚合算法之</p> <p style="color: rgb(51, 51, 51); font-family: A
2976 0
|
Java 计算机视觉 算法
图像处理------基于像素的图像混合
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">介绍几种常见的将两张图像混合在一起形成一张新的图像的算法,</span></p> <p style="color: rgb(51, 51, 51); f
1491 0
|
算法 计算机视觉
图像处理------Mean Shift滤波(边缘保留的低通滤波)
<a target="_blank" href="http://blog.csdn.net/jia20003/article/details/8214567" style="color: rgb(0, 0, 0); text-decoration: none; font-family: 'Microsoft YaHei'; font-size: 20px; line-height: 30px;
3920 0