1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package Kmeans;
import com.sun.javafx.image.impl.IntArgb;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
BufferedImage image=null;
try {
image= ImageIO.read(new File("src/source/images/test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
if(image==null)return;
//读取图像并转换灰度数组
int width=image.getWidth();
int height= image.getHeight();
int[] arr=new int[width*height];
int[] gray_arr=new int[width*height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//拆分argb值
int pixel=image.getRGB(j,i);
int alpha=(pixel>>24) & 0xff;
int red=(pixel>>16) & 0xff;
int green=(pixel>>8) & 0xff;
int blue=(pixel) & 0xff;
//此处使用平均值法来粗略算出灰度值
int avg=(red+green+blue)/3;
//保存灰度数值
arr[i*width+j]=avg;
//保存灰度图像
gray_arr[i*width+j]=(alpha << 24) | (avg << 16) | (avg << 8) | avg;
}
}
//保存当前保存的灰度图像
BufferedImage gray_image=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int gray=gray_arr[i*width+j];
gray_image.setRGB(j,i,gray);
}
}
try {
writeImageFile(gray_image,"gray.jpg");
} catch (IOException e) {
e.printStackTrace();
}
int val=KMeansUtil.binarization(arr,width,height);
if(val>=0){
BufferedImage value_image=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
//根据K值处理图像数组
for (int i = 0; i < arr.length; i++) {
if(arr[i]<=val){
arr[i]=1;
}else {
arr[i]=0;
}
}
//写入图像数据
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if(arr[i*width+j]==1){
value_image.setRGB(j,i,Color.BLACK.getRGB());
}else {
value_image.setRGB(j,i,Color.WHITE.getRGB());
}
}
}
//保存图像
try {
writeImageFile(value_image,"value.jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//保存图像文件
private static void writeImageFile(BufferedImage bi,String fileName) throws IOException {
File outputfile = new File("src/Kmeans/"+fileName);
String[] arr=fileName.split("\\.");
ImageIO.write(bi, arr[1], outputfile);
}
}
|