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. Javascript流程控制

    Javascript流程控制 1.条件语句 (1)if(exp)执行一句代码 (2)if(exp){执行代码段;} (3)if(exp){exp为true执行代码段}else{exp为false执行的 ...

  2. label按钮和文字对齐

    label按钮和文字对齐 做表单的时候,经常遇到:复选框和文字对不齐的情况 ========================== 下面方法可以对齐 <!--label [[--> < ...

  3. 利用scrapy框架进行爬虫

    今天一个网友问爬虫知识,自己把许多小细节都忘了,很惭愧,所以这里写一下大概的步骤,主要是自己巩固一下知识,顺便复习一下.(scrapy框架有一个好处,就是可以爬取https的内容) [爬取的是杨子晚报 ...

  4. Pyqt4的对话框 -- 文件对话框

    文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作 # QInputDialog 文件对话框 # 本示例包含一个菜单栏,一个状态栏和一个被设置为中心部件的文本编辑器. # 状态栏的状态信 ...

  5. 【数论】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 ...

  6. python之 正则表达式

    简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 Emacs 风格的模式.Emacs 风格模式可读 ...

  7. win10 uwp 横向 AppBarButton

    一般看到的 AppBarButton 都是图片在上面,文字在下面,是否可以更改让文字在和图片相同的位置?本文告诉大家如何做出横向的 AppBarButton 把图标和文本放在一起. 如果需要添加 Ap ...

  8. PC端截取GIF图片的软件

    PC端截取GIF图片的软件分享:下载>>

  9. chrome使用技巧整理

    查看chrome的相关快捷键:打开chrome,按下F1,点击"键盘和鼠标快捷键". 1.查看版本: 浏览器输入网址:chrome://version/ 2.查看Chrome进程清 ...

  10. Linux系统下安装jdbc与tomcat

    一.下载Linux版本的jdbc与tomcat 1.1 下载Linux版本的jdbc: 1.1.1 1.1.2 1.1.3 在进行1.1.3操作之前得先勾选我同意协议 1.1.4 下载完成 1.2下载 ...