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

简介: <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, 00, 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, 00, 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. }  
相关文章
|
4月前
|
计算机视觉 Python
图像处理之光源退化效果
图像处理之光源退化效果
19 0
|
编解码 算法 计算机视觉
小波分析法图像处理----图像增强
小波分析法图像处理----图像增强
721 0
小波分析法图像处理----图像增强
|
算法 数据可视化 异构计算
webgl智慧楼宇发光系列之线性采样下高斯模糊
webgl智慧楼宇发光系列之线性采样下高斯模糊 效率问题 线性采样 代码讲解 总结 参考文档
webgl智慧楼宇发光系列之线性采样下高斯模糊
|
算法 计算机视觉 Java
图像处理------调整亮度与对比度
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">很多时候,一张图像被过度曝光显得很白,或者光线不足显得很暗,有时候背景跟图像人物</p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-siz
1911 1
|
资源调度 计算机视觉 Java
图像处理------图像加噪
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图像噪声源于现实世界中数字信号总会受到各种各样的干扰,最终接受的图像和源于的数字信号之间总</p> <p style="color: rgb(51, 51, 51); font-family: Arial; fon
1633 0
|
计算机视觉 Java
图像处理------光束效果
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>原理:</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height
1411 0
|
计算机视觉 Java
图像处理------ 二值膨胀及应用
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>基本原理:</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-heig
1462 0
|
计算机视觉
图像处理------ 二值腐蚀
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>概述:</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height
1408 0
|
计算机视觉 Java
图像处理------直方图均衡化
<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-fam
1469 0
|
资源调度 算法 Java
图像处理------高斯模糊
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">高斯模糊是一种两维的卷积模糊操作,在图像完成高斯模糊相对于均值模糊来说,</p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 1
1617 0