forked from yinchuandong/JavaVerify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocess.java
More file actions
170 lines (133 loc) · 3.87 KB
/
Preprocess.java
File metadata and controls
170 lines (133 loc) · 3.87 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package train;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Preprocess {
public Preprocess(){
}
private void run(){
File dir = new File("download");
//只列出jpg
File[] files = dir.listFiles(new FilenameFilter() {
public boolean isJpg(String file){
if (file.toLowerCase().endsWith(".jpg")){
return true;
}else{
return false;
}
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return isJpg(name);
}
});
for (File file : files) {
try {
BufferedImage img = ImageIO.read(file);
BufferedImage binaryImg = getBinaryImage(img);
ImageIO.write(binaryImg, "JPG", new File("1_gray/" + file.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 二值化
* @param sourceImage
* @return 二值化之后的图像
*/
public BufferedImage getBinaryImage(BufferedImage sourceImage){
double Wr = 0.299;
double Wg = 0.587;
double Wb = 0.114;
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
int[][] gray = new int[width][height];
//灰度化
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Color color = new Color(sourceImage.getRGB(x, y));
int rgb = (int) ((color.getRed()*Wr + color.getGreen()*Wg + color.getBlue()*Wb) / 3);
gray[x][y] = rgb;
}
}
BufferedImage binaryBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
//二值化
int threshold = getOstu(gray, width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (gray[x][y] > threshold) {
int max = new Color(255, 255, 255).getRGB();
gray[x][y] = max;
}else{
int min = new Color(0, 0, 0).getRGB();
gray[x][y] = min;
}
binaryBufferedImage.setRGB(x, y, gray[x][y]);
}
}
return binaryBufferedImage;
}
/**
* 获得二值化图像
* 最大类间方差法
* @param gray
* @param width
* @param height
*/
private int getOstu(int[][] gray, int width, int height){
int grayLevel = 256;
int[] pixelNum = new int[grayLevel];
//计算所有色阶的直方图
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int color = gray[x][y];
pixelNum[color] ++;
}
}
double sum = 0;
int total = 0;
for (int i = 0; i < grayLevel; i++) {
sum += i*pixelNum[i]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
total += pixelNum[i]; //n为图象总的点数,归一化后就是累积概率
}
double sumB = 0;//前景色质量矩总和
int threshold = 0;
double wF = 0;//前景色权重
double wB = 0;//背景色权重
double maxFreq = -1.0;//最大类间方差
for (int i = 0; i < grayLevel; i++) {
wB += pixelNum[i]; //wB为在当前阈值背景图象的点数
if (wB == 0) { //没有分出前景后景
continue;
}
wF = total - wB; //wB为在当前阈值前景图象的点数
if (wF == 0) {//全是前景图像,则可以直接break
break;
}
sumB += (double)(i*pixelNum[i]);
double meanB = sumB / wB;
double meanF = (sum - sumB) / wF;
//freq为类间方差
double freq = (double)(wF)*(double)(wB)*(meanB - meanF)*(meanB - meanF);
if (freq > maxFreq) {
maxFreq = freq;
threshold = i;
}
}
return threshold;
}
public static void main(String[] args){
System.out.println("---begin---");
long start = System.currentTimeMillis();
Preprocess model = new Preprocess();
model.run();
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
System.out.println("---end----");
}
}