图像处理之玻璃水印特效(祝大家圣诞节快乐)
from:http://blog.csdn.net/jia20003/article/details/13159535
Water Ripple Effect - 水波纹效果
一:原理
模拟水波纹效果,最常见的是sine或者cosn的函数,周期性变化,贴近自然
当水波纹中中间开始向四周扩散的时候,一般都是慢慢的失去能量,振幅也是
越来越小,所以程序要模拟这个过程时候,要加上一个能量递减因子。然后用
公式 y = a*sine(bx + c)来表示波纹公式。
二:程序实现
最重要的一步是计算水波纹的振幅。在任意一点确定水波的中心位置,可以是
鼠标随机选取,对半径范围内的像素位置实现水波生成,然后转换为位置,对
位置实现浮点数取整,然后使用适当的插值算法,本例使用双线性插值。
三:程序效果
四:滤镜完全源代码,这次我写了些中文注解,不给源代码的博文不是好博文
- package com.gloomyfish.filter.study;
- import java.awt.image.BufferedImage;
- public class WaterFilter extends AbstractBufferedImageOp {
- private float wavelength = 16;
- private float amplitude = 10;
- private float phase = 0;
- private float centreX = 0.5f;
- private float centreY = 0.5f;
- private float radius = 50;
- private float radius2 = 0;
- private float icentreX;
- private float icentreY;
- public WaterFilter() {
- }
- @Override
- public BufferedImage filter(BufferedImage src, BufferedImage dest) {
- int width = src.getWidth();
- int height = src.getHeight();
- 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 );
- icentreX = width * centreX;
- icentreY = height * centreY;
- if ( radius == 0 )
- radius = Math.min(icentreX, icentreY);
- radius2 = radius*radius;
- int index = 0;
- float[] out = new float[2];
- for(int row=0; row<height; row++) {
- for(int col=0; col<width; col++) {
- index = row * width + col;
- // 获取水波的扩散位置,最重要的一步
- generateWaterRipples(col, row, out);
- int srcX = (int)Math.floor( out[0] );
- int srcY = (int)Math.floor( out[1] );
- float xWeight = out[0]-srcX;
- float yWeight = out[1]-srcY;
- int nw, ne, sw, se;
- // 获取周围四个像素,插值用,
- if ( srcX >= 0 && srcX < width-1 && srcY >= 0 && srcY < height-1) {
- // Easy case, all corners are in the image
- int i = width*srcY + srcX;
- nw = inPixels[i];
- ne = inPixels[i+1];
- sw = inPixels[i+width];
- se = inPixels[i+width+1];
- } else {
- // Some of the corners are off the image
- nw = getPixel( inPixels, srcX, srcY, width, height );
- ne = getPixel( inPixels, srcX+1, srcY, width, height );
- sw = getPixel( inPixels, srcX, srcY+1, width, height );
- se = getPixel( inPixels, srcX+1, srcY+1, width, height );
- }
- // 取得对应的振幅位置P(x, y)的像素,使用双线性插值
- /*if(xWeight >=0 || yWeight >= 0)
- {
- outPixels[index] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
- }
- else
- {
- outPixels[index] = inPixels[index];
- }*/
- outPixels[index] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
- }
- }
- setRGB( dest, 0, 0, width, height, outPixels );
- return dest;
- }
- private int getPixel(int[] pixels, int x, int y, int width, int height) {
- if (x < 0 || x >= width || y < 0 || y >= height) {
- return 0; // 有点暴力啦,懒得管啦
- }
- return pixels[ y*width+x ];
- }
- protected void generateWaterRipples(int x, int y, float[] out) {
- float dx = x-icentreX;
- float dy = y-icentreY;
- float distance2 = dx*dx + dy*dy;
- // 确定 water ripple的半径,如果在半径之外,就直接获取原来位置,不用计算迁移量
- if (distance2 > radius2) {
- out[0] = x;
- out[1] = y;
- } else {
- // 如果在radius半径之内,计算出来
- float distance = (float)Math.sqrt(distance2);
- // 计算改点振幅
- float amount = amplitude * (float)Math.sin(distance / wavelength * ImageMath.TWO_PI - phase);
- // 计算能量损失,
- amount *= (radius-distance)/radius; // 计算能量损失,
- if ( distance != 0 )
- amount *= wavelength/distance;
- // 得到water ripple 最终迁移位置
- out[0] = x + dx*amount;
- out[1] = y + dy*amount;
- }
- }
- }
觉得不错请顶一下,谢谢!
转载请务必注明出处!谢谢!
图像处理之玻璃水印特效(祝大家圣诞节快乐)相关推荐
- Pmcaff祝各位圣诞节快乐!
圣诞节紧急通知 还在等着圣诞老人送礼物.恐怕要让你失望了. 据路边社消息,圣诞老人在从丹麦进入中国境内的途中因雾霾太大看不清路而摔倒,至今无人敢扶,仍在雪地里趴着.而且没有社保,由于年近古稀,失去 ...
- 用c语言表达圣诞节快乐的英文,圣诞节快乐用英文怎么说
圣诞快乐除了Merry Christmas还可以怎么说呢?出国留学网英语栏目为大家带来"圣诞节快乐用英文如何表达",希望大家喜欢. Merry Christmas Merry X' ...
- 怎么用计算机圣诞节快乐,2019圣诞节快乐祝福 最新圣诞节快乐祝福的话
2019圣诞节到了,想好在朋友圈发点什么吗?在这里小编为大家分享一些圣诞节快乐祝福.一起来看看吧. 春生杨柳冬藏雪,平安夜后圣诞节.心有空灵因志高,从从容容是书香.十年寒窗十年苦,十年一觉十年梦.成功 ...
- html圣诞快乐英文,圣诞节快乐的英文 圣诞快乐英文单词
圣诞节是西方国家盛大的节日,相当于我国的春节.圣诞节是基督教徒的节日,各国基督教徒都要举行纪念仪式.圣诞节从西方流传过来,在我国也很流行,在这一天,家人.朋友或情侣之间都会互相准备圣诞礼物,有时还会写 ...
- html5 canvas实现图片玻璃碎片特效
今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效.图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息.效果图如下: 在线预览 源码下载 实现代码: html代码: & ...
- linux设置切换窗口特效,Linux_在Ubuntu下启用亮丽的透明玻璃窗口特效,本文将采用图文并茂的形式向 - phpStudy...
在Ubuntu下启用亮丽的透明玻璃窗口特效 本文将采用图文并茂的形式向大家演示在 Ubuntu 8.04 (Hardy Heron) 下启用亮丽的透明玻璃窗口特效.以下步骤均在 Thinkpad T6 ...
- Python-OpenCV使用基本图像处理技术进行水印去除
使用基本图像处理技术进行水印去除,主要采用OpenCV提供的库函数以及基本的图像处理进行图像修复,但是从鲁棒性方面可能存在不足.处理的图像可能存在一定的模糊水印去除不完整.留边等问题.本文参考了部分其 ...
- 圣诞节快乐 | 圣诞特效来了!!
圣诞节(Christmas)又称耶诞节.耶稣诞辰,译名为"基督弥撒",是西方传统节日,起源于基督教,在每年公历12月25日. 作为程序员的我们怎么庆祝这个节日呢? 当然是少不了圣诞 ...
- js 图片打碎_html5 canvas打碎的图片玻璃碎片特效
特效描述:html5canvas 打碎的图片 玻璃碎片特效.玻璃碎片特效 代码结构 1. 引入JS 2. HTML代码 var imageWidth = 768, imageHeight = 485; ...
最新文章
- 【Codeforces】427B Prison Transfer(别让罪犯跑了...)
- codeforces数学1600day4[贪心数学公式推导CodeForces - 1151D ,思维CodeForces - 1085C,数论同余+组合计数 CodeForces - 1056B]
- linux视频学习6(mysql的安装/)
- VS2003转VS2010 fatal error C1189: #error
- cvc降噪和主动降噪_市面上的降噪耳机,物理降噪和主动降噪,你会怎么选择?...
- 《深入理解Java函数式编程》系列文章
- Java中interrupted()和isInterrupted()之间的区别
- JavaScript学习笔记:语句
- 近两天让我羞愧难当的遭遇
- 对运算符重载的函数2
- Linux-完美解决linux系统镜像下载速度慢的问题
- 批处理文件(bat文件)注册dll批量注册dll
- 程序员工作交接文档怎么写_IT交接事项.doc
- php 读取脸型,基于OpenCV的PHP图像人脸检测识别技术
- 计算机nls数据丢失损坏无法启动,电脑开机时,显示NLS数据丢失或损坏怎么处理...
- 从前端页面上下载为png格式的图片
- 在午睡,手机接连振动,是几个朋友用短信微信问我
- 双摄像头测距的OpenCV实现
- theano及cuda环境搭建
- 跟着吉庆写文档(流程图) の 让“伊妹儿“帮你管理时间
热门文章
- fs_struct和file_struct关系
- linux内核网络协议栈--发送流程及函数(十)
- python中字符串单引号、双引号、三引号_Python:字符串单引号(‘)、双引号(“)、三单引号(”’)和三双引号(“””)的区别...
- 学生选课管理系统c语言程序报告,c语言课程设计学生选课管理系统实验报告.doc...
- 中盐总公司:盐业公司24小时配送保供应
- js之call,apply和bind的模拟实现
- CCF NOI1113 括号匹配
- MongoDB入门学习(二):MongoDB的基本概念和数据类型
- 深入理解javascript原型和闭包(6)——继承
- 阿里云accessKey如何创建?~ 2015.08.25