Skip to content

Commit

Permalink
添加了原理介绍
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhoujay committed May 12, 2016
1 parent 420181f commit 39986ac
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 5 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Low Poly 图片生成器

> 能够生成low poly风格的图片
> 能够生成low poly风格的图片,[Android版本的实现](https://github.com/zzhoujay/LowPolyAndroid)
### 效果图

Expand All @@ -10,7 +10,61 @@

```
LowPoly.generate(inputStream,outputStream);
```
或者
```
LowPoly.generate(inputStream, outputStream, accuracy, scale, fill,format, antiAliasing, pointCount);
```

### jar包使用方法

[下载](lib/LowPoly-1.0.jar)

```
java -jar LowPoly-1.0.jar input_filename output_filename
```

或者

```
java -jar LowPoly-1.0.jar input_filename output_filename accuracy scale fill format antiAliasing pointCount
```

### 原理介绍

Low Poly即低多边形,和提高图片精度相反,我们需要降低图片精度来达到low poly的效果

整个算法最主要的就是两步

1. 降低精度
2. 提取图像信息

使用提取到的图像边缘点和一些随机点生成三角形,并着色,即可完成图片的low poly化

#### 降低精度

降低精度采用的是使用三角形粗化像素点的方法,具体实现使用Delaunay算法,具体实现参见`Delaunay.java`

如果单纯只是降低精度的效果图如下:

![](img/1.png)

其中如果去掉填充的颜色(由于点是随机生成的,所以两次生成的点不一样)

![](img/2.png)

取的点越多,生成的图片就会越接近原图片


#### 提取图像信息

采用Sobel算法进行边缘检测,提取图像关键信息,具体参见`Sobel.java`

采集到的点

![](img/3.png)

由于源图片信息量较大采集到的点也就很多,最终效果是由随机的点和采集到的点叠加产生的。


_by zzhoujay_
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ version '1.0'

apply plugin: 'java'

archivesBaseName="LowPoly"

sourceCompatibility = 1.5

sourceCompatibility = 1.8

repositories {
mavenCentral()
}


jar {
manifest {
attributes 'Main-Class': 'com.zzhoujay.lowpoly.Simple'
}
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Binary file added img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created by .ignore support plugin (hsz.mobi)
!LowPoly-1.0.jar
Binary file added lib/LowPoly-1.0.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions src/main/java/com/zzhoujay/lowpoly/LowPoly.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public final class LowPoly {

public static void generate(InputStream inputStream, OutputStream outputStream) throws IOException {
generate(inputStream, outputStream, 50, 1, true, "png", false);
generate(inputStream, outputStream, 50, 1, true, "png", false, 300);
}

/**
Expand All @@ -30,9 +30,10 @@ public static void generate(InputStream inputStream, OutputStream outputStream)
* @param fill 是否填充颜色,为false时只绘制线条
* @param format 输出图片格式
* @param antiAliasing 是否抗锯齿
* @param pointCount 随机点的数量
* @throws IOException
*/
public static void generate(InputStream inputStream, OutputStream outputStream, int accuracy, float scale, boolean fill, String format, boolean antiAliasing) throws IOException {
public static void generate(InputStream inputStream, OutputStream outputStream, int accuracy, float scale, boolean fill, String format, boolean antiAliasing, int pointCount) throws IOException {
if (inputStream == null || outputStream == null) {
return;
}
Expand All @@ -50,7 +51,7 @@ public static void generate(InputStream inputStream, OutputStream outputStream,
}
});

for (int i = 0; i < 100; i++) {
for (int i = 0; i < pointCount; i++) {
particles.add(new int[]{(int) (Math.random() * width), (int) (Math.random() * height)});
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/zzhoujay/lowpoly/Simple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.zzhoujay.lowpoly;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Created by zhou on 16-5-12.
*/
public class Simple {

public static void main(String[] args) throws IOException {
if (args.length < 2) {
throw new RuntimeException("请输入有效的图片");
}
String in = args[0];
String out = args[1];
File inFile = new File(in);
if (inFile.exists()) {
File outFile = new File(out);
FileInputStream inputStream = new FileInputStream(inFile);
FileOutputStream outputStream = new FileOutputStream(outFile);
long lastTime = System.currentTimeMillis();
if (args.length >= 8) {
int accuracy = Integer.valueOf(args[2]);
float scale = Float.valueOf(args[3]);
boolean fill = Boolean.valueOf(args[4]);
String format = args[5];
boolean antiAliasing = Boolean.valueOf(args[6]);
int pointCount = Integer.valueOf(args[7]);
LowPoly.generate(inputStream, outputStream, accuracy, scale, !fill, format, antiAliasing, pointCount);
} else {
LowPoly.generate(inputStream, outputStream);
}
System.out.println("目标已保存至:" + outFile.getAbsolutePath());
System.out.println("用时:" + (System.currentTimeMillis() - lastTime));
} else {
throw new RuntimeException("源文件不存在");
}
}
}

0 comments on commit 39986ac

Please sign in to comment.