原始124×124pix
原图
1次
2次
3次
15次
周期表:
N |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
周期 |
3 |
4 |
3 |
10 |
12 |
8 |
6 |
12 |
30 |
5 |
12 |
N |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
周期 |
14 |
24 |
20 |
12 |
18 |
12 |
9 |
30 |
8 |
15 |
24 |
N |
25 |
50 |
60 |
100 |
120 |
125 |
128 |
256 |
480 |
512 |
1024 |
周期 |
50 |
150 |
60 |
150 |
60 |
250 |
96 |
192 |
120 |
384 |
768 |
package com.zeph.j2se.arnold;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Arnold's Cat Map(Arnold变换)
*
* @author BenZeph
*
*/
public class Arnold {
private File srcImageFile, desImageFile;
private BufferedImage srcImage, desImage;
private int[][] srcMatrix, desMatrix;
private int N;// 图像的长度(宽度)
private int time;// 周期
/**
* Arnold's Cat Map(Arnold变换)
*
* @param srcImageFile
* @param desImageFile
* @param time
* 周期
*/
public Arnold(File srcImageFile, File desImageFile, int time) {
this.srcImageFile = srcImageFile;
this.desImageFile = desImageFile;
this.time = time;
}
/**
* 读取图像
*
* @param imageFile
* @return
*/
public BufferedImage readImage(File imageFile) {
BufferedImage image = null;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
/**
* 获取图像RGB矩阵
*
* @param image
* @return
*/
public int[][] getMatrixRGB(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[][] imageMatrix = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
imageMatrix[i][j] = image.getRGB(i, j);
}
}
return imageMatrix;
}
/**
* 写入图像
*
* @param filePath
*/
public void writeImage(File imageFile, BufferedImage image) {
try {
ImageIO.write(image, "jpg", imageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Arnold变换初始化
*
* @param image
* @return
*/
public boolean initArnold(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
if (width != height) {
return false;
} else {
N = width;
srcMatrix = getMatrixRGB(image);
desMatrix = new int[N][N];
desImage = new BufferedImage(width, height, srcImage.getType());
return true;
}
}
/**
* Arnold变换
*/
public void arnoldTransform() {
srcImage = readImage(srcImageFile);
if (initArnold(srcImage)) {
for (int n = 0; n < time; n++) {
if (n != 0) {
srcMatrix = desMatrix;
desMatrix = new int[N][N];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
desMatrix[(i + j) % N][(i + 2 * j) % N] = srcMatrix[i][j];
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
desImage.setRGB(i, j, desMatrix[i][j]);
}
}
}
writeImage(desImageFile, desImage);
}
public static void main(String[] args) {
File srcImageFile = new File("D://lena124.jpg");
File desImageFile = new File("D://15.jpg");
Arnold arnold = new Arnold(srcImageFile, desImageFile, 15);
arnold.arnoldTransform();
}
}