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. 深入理解cookie和session

    cookie和session在java web开发中扮演了十分重要的作用,本篇文章对其中的重要知识点做一些探究和总结. 1.cookie存在于浏览器 随意打开一个网址,用火狐的调试工具,随意选取一个链 ...

  2. angular4.0中form表单双向数据绑定正确姿势

    issue:用[(ngModel)]="property"指令双向数据绑定,报错. reason1:使用ngModel绑定数据需要注入FormsModule模块,在app.modu ...

  3. 第八章:Python基础の面向对象(二)

    本課主題 面向对象的多态 面向对象的成员 成员修饰符 特殊成员 面向对象其他应用 异常处理 设计模式与单例模式 面向对象的多态 指定参数类型只是多态的一种表现 另外一种是允许自己类型和自己的子类型(典 ...

  4. 对《cookie之困》的一些总结与思考

    0x00 前言 早些时候在gayhub翻安全大会的paper,翻到了kcon的一些paper. https://github.com/knownsec/KCon 从前辈们的paper中学到了不少,也产 ...

  5. sql server中的分页数据查询

    1.引言 今天在工作中遇到一个需要进行sql server分页数据查询的问题,但是分页数据查询的sql却忘记了,最终通过查询资料解决了该问题.现在把解决方法记下,以备查阅. 在这里需要感谢博客园的Ql ...

  6. 快速了解Hibernate的使用

    了解hibernate的使用 hibernate是作用于传统的mvc开发dao层的框架 在以往的开发中我们如何的编写dao的代码呢 1.原始的jdbc操作,在dao中到操作Connection/Sta ...

  7. 自学Aruba1.1-Aruba体系结构-产品线

    点击返回:自学Aruba之路Aruba产品线 IP switches: 1500.2500.3500 Controllers:7200 .70x0 Series.7005 Meridian:基于ACE ...

  8. 适合在Markdown里面使用的emoji

    因为Markdown里面加颜色需要写html style, 所以对于一些标题, 还是用一下emoji吧: RED APPLE (

  9. Check whether a remote server port is open on Linux

    链接:https://www.pixelstech.net/article/1514049471-Check-whether-a-remote-server-port-is-open-on-Linux

  10. AspNet Core 核心 通过依赖注入(注入服务)

    说起依赖注入 相信大家已经很熟悉了,这里我在简要的描述一遍, 什么是依赖注入: 我们从字面意义上来解释一下:依赖代表着两个或者多个对象之间存在某些特定的联系:举一个不是很恰当的例子 比如说一度夫妻组成 ...