Java 运动模糊代码

想用Java 写个运动模糊的效果,无奈本人水平有限,国内也没找到资源,于是Google到了一个文档,特地分享出来!

本代码源自 http://www.jhlabs.com/ip/blurring.html

Java运动模糊算法:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*; public class MotionBlurOp extends AbstractBufferedImageOp {
private float centreX = 0.5f, centreY = 0.5f;
private float distance=20.0f; //这里设置运动距离
private float angle;
private float rotation;
private float zoom; public MotionBlurOp() {
} public MotionBlurOp( float distance, float angle, float rotation, float zoom ) {
this.distance = distance;
this.angle = angle;
this.rotation = rotation;
this.zoom = zoom;
} public void setAngle( float angle ) {
this.angle = angle;
} public float getAngle() {
return angle;
} public void setDistance( float distance ) {
this.distance = distance;
} public float getDistance() {
return distance;
} public void setRotation( float rotation ) {
this.rotation = rotation;
} public float getRotation() {
return rotation;
} public void setZoom( float zoom ) {
this.zoom = zoom;
} public float getZoom() {
return zoom;
} public void setCentreX( float centreX ) {
this.centreX = centreX;
} public float getCentreX() {
return centreX;
} public void setCentreY( float centreY ) {
this.centreY = centreY;
} public float getCentreY() {
return centreY;
} public void setCentre( Point2D centre ) {
this.centreX = (float)centre.getX();
this.centreY = (float)centre.getY();
} public Point2D getCentre() {
return new Point2D.Float( centreX, centreY );
} private int log2( int n ) {
int m = 1;
int log2n = 0; while (m < n) {
m *= 2;
log2n++;
}
return log2n;
} public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
if ( dst == null )
dst = createCompatibleDestImage( src, null );
BufferedImage tsrc = src;
float cx = (float)src.getWidth() * centreX;
float cy = (float)src.getHeight() * centreY;
float imageRadius = (float)Math.sqrt( cx*cx + cy*cy );
float translateX = (float)(distance * Math.cos( angle ));
float translateY = (float)(distance * -Math.sin( angle ));
float scale = zoom;
float rotate = rotation;
float maxDistance = distance + Math.abs(rotation*imageRadius) + zoom*imageRadius;
int steps = log2((int)maxDistance); translateX /= maxDistance;
translateY /= maxDistance;
scale /= maxDistance;
rotate /= maxDistance; if ( steps == 0 ) {
Graphics2D g = dst.createGraphics();
g.drawRenderedImage( src, null );
g.dispose();
return dst;
} BufferedImage tmp = createCompatibleDestImage( src, null );
for ( int i = 0; i < steps; i++ ) {
Graphics2D g = tmp.createGraphics();
g.drawImage( tsrc, null, null );
g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
g.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f ) ); g.translate( cx+translateX, cy+translateY );
g.scale( 1.0001+scale, 1.0001+scale ); // The .0001 works round a bug on Windows where drawImage throws an ArrayIndexOutofBoundException
if ( rotation != 0 )
g.rotate( rotate );
g.translate( -cx, -cy ); g.drawImage( dst, null, null );
g.dispose();
BufferedImage ti = dst;
dst = tmp;
tmp = ti;
tsrc = dst; translateX *= 2;
translateY *= 2;
scale *= 2;
rotate *= 2;
}
return dst;
} public String toString() {
return "Blur/Motion Blur...";
}
}

测试代码:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; /**
* Created by zdmein on 2018/1/10.
*/
public class MotionBlurOpTest {
public static void main(String [] args) throws IOException {
BufferedImage sourceImage = ImageIO.read(new File("flower.jpg"));
MotionBlurOp filter=new MotionBlurOp();
BufferedImage destImage=filter.filter(sourceImage,null);
ImageIO.write(destImage, "PNG", new File("MotionBlurOpflower.jpg")); }
}



Java 运动模糊的更多相关文章

  1. 从单幅图像高质量去除运动模糊——读JiaYaJia同名英文论文总结

    原始论文在这里 http://www.cse.cuhk.edu.hk/leojia/projects/motion_deblurring/ 一.概述 论文根据以下的基本模糊图像模型建立 其中I是我们观 ...

  2. Unity shader学习之屏幕后期处理效果之运动模糊

    运动模糊,代码如下: using UnityEngine; public class MotionBlurRenderer : PostEffectRenderer { [Range(0.1f, 0. ...

  3. 维纳滤波和编码曝光PSF去除运动模糊【matlab】

    编码曝光知识 - ostartech - 博客园 https://www.cnblogs.com/wxl845235800/p/8276362.html %%%%%%%%%%%%%%%%%%%%%%% ...

  4. Win8 Metro(C#)数字图像处理--2.50图像运动模糊

    原文:Win8 Metro(C#)数字图像处理--2.50图像运动模糊  [函数名称] 图像运动模糊算法    MotionblurProcess(WriteableBitmap src,int  ...

  5. OpenCV3入门(十三)图像运动模糊

    1.原理 运动模糊产生: 由于相机传感器或物体相对运动, 按快门瞬间造成图像产生运动模糊. 在用摄像机获取景物图像时,如果在相机曝光期间景物和摄像机之间存在相对运动,例如用照相机拍摄快速运动的物体,或 ...

  6. Java JDBC 模糊查询 避免输入_,%返回全部数据

    Java JDBC 模糊查询 避免输入_,%返回全部数据 "SELECT * FROM employees WHERE INSTR(first_name,?)>0 " 仅供参 ...

  7. Unity Shader 屏幕后效果——摄像机运动模糊(速度映射图实现)

    速度映射图主要是为了得到每个像素相对于前一帧的运动矢量,其中一种方法是使用摄像机的深度纹理来推导. 推导过程如下: 先由深度纹理逆推出NDC(归一化的设备坐标)下的顶点坐标,利用VP矩阵(视角*投影矩 ...

  8. PS 过滤器——运动模糊

    %%%%%  motion blur clc; clear all; close all; Image=imread('4.jpg'); Image=double(Image); theta=pi/4 ...

  9. PS 滤镜——运动模糊

    %%%%%  motion blur clc; clear all; close all; Image=imread('4.jpg'); Image=double(Image); theta=pi/4 ...

随机推荐

  1. linux系统常见压缩命令

    在linux环境中,压缩文件的扩展名基本是:*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2 *.Z compress程序压缩的文件 *.gz gzip程序压缩的文件 *.bz2 ...

  2. sqlalchemy 踩过的坑

    记录下Sqlalchemy遇到的问题,不定时更新. 设置主键为非自增 sqlalchemy 在sql server中默认主键是自增的,如果在数据库设置的主键不是自增的,这个时候插入就会出现异常: 提示 ...

  3. 关于Python的函数(Method)与方法(Function)

    先上结论: 函数(function)是Python中一个可调用对象(callable), 方法(method)是一种特殊的函数. 一个可调用对象是方法和函数,和这个对象无关,仅和这个对象是否与类或实例 ...

  4. 原生JavaScript如何解决父元素查找指定类名的子元素的问题

    问题:已知一个元素的类名是right并且这个元素的祖先元素的类名为parent,现在想通过原生JavaScript获得这个类名为right的元素. 我的思路:利用递归.先判断已知的祖先元素(题中的pa ...

  5. [Spark内核] 第38课:BlockManager架构原理、运行流程图和源码解密

    本课主题 BlockManager 运行實例 BlockManager 原理流程图 BlockManager 源码解析 引言 BlockManager 是管理整个Spark运行时的数据读写的,当然也包 ...

  6. blog建表操作

    表思维导图:   数据库:表 from django.db import modelsfrom django.conf import settingsfrom django.contrib.auth. ...

  7. 【Win 10 应用开发】MIDI 音乐合成——更改乐器音色

    在开始今天的吹 BB 博文之前,说点题外话. 首先,上次老周给大伙伴们介绍完发送 MIDI 音符,本来说好的接着说一下如何更改乐器音色,为啥这么久都没更新呢.特特来解释一下,最近老周接了一个 ASP. ...

  8. lxml的另一种用法

    python中lxml库是一个十分强大的xml解析库,最近在看<白帽子将web扫描>这本书的时候,里面提供了一种不同于以往的用法,因此在这将这个方法记录下来 传统的lxml库的使用方法类似 ...

  9. (2环境架设)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练

    从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...

  10. 五分钟学习React(三):纯HTML代码搭建React应用

    上一期我们使用了React官方的脚手架运行React应用.大家可能会觉得这种方法很繁琐,需要配置各种第三方插件.JQuery时代的前端真是让人怀念.这一期,我就带领大家创建一个"怀旧版&qu ...