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. 菜鸟之旅——初识.NET

    入坑.Net 也已经两年多了,既然在微软.Net 体系下混,对.Net 体系也需要了解一下,当然这些知识也都是查阅资料都能够查到的,这里主要是对自己所学的整理,况且最近的学习有些闭门造车的味道,现在想 ...

  2. Python的__getattr__和__getattribute__

    __getattr____getattr__在当前主流的Python版本中都可用,重载__getattr__方法对类及其实例未定义的属性有效.也就属性是说,如果访问的属性存在,就不会调用__getat ...

  3. 进程管理工具Supervisor(二)Events

    supervisor可以当做一个简单的进程启动.重启.控制工具使用,也可以作为一个进程监控框架使用,作为后者,需要使用supervisor的Events机制. Event Listeners supe ...

  4. 常见ie css hack

    .all IE{property:value\9;} .gte IE 8{property:value\0;} .lte IE 7{*property:value;} .IE 8/9{property ...

  5. 初读"Thinking in Java"读书笔记之第二章 --- 一切都是对象

    用引用操纵对象 Java里一切都被视为对象,通过操纵对象的一个"引用"来操纵对象. 例如, 可以将遥控器视为引用,电视机视为对象. 创建一个引用,不一定需要有一个对象与之关联,但此 ...

  6. 【数论】Lucas

    就是个Lucas 对于质数p,有C(n,m)=C(n/p,m/p)*C(n%p,m%p)%p 代码 ll C(ll a,ll b) { ; ; if(a<p&&b<p) r ...

  7. duilib基本框架

    最近我一个同学在项目中使用到了duilib框架,但是之前并没有接触过,他与我讨论这方面的内容,看着官方给出的精美的例子,我对这个库有了很大的兴趣,我自己也是初学这个东东,我在网上花了不少时间来找相关的 ...

  8. 垃圾回收(GC) 的基本算法

    GC 作为一个长久的话题,从诞生[1]至今也算是经历了六七十年了,对于很多习惯于使用 Java/Python 的同学来说,对于内存的管理可能会稍微更陌生一些,因为这些语言在语言层面就屏蔽了内存的分配和 ...

  9. JDK源码 - ArrayList

    /** * ArrayList源码分析 * @author liyong * */ public class Util { @SuppressWarnings("unchecked" ...

  10. uboot中 make xxx_config 的作用(以make smdk2410_config为例)

    1.创建到目标板相关文件的链接 ln -s asm-arm asm ln -s arch-s3c24x0 asm-arm/arch ln -s proc-armv asm-arm/proc 2.创建i ...