-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageUtil.java
More file actions
282 lines (237 loc) · 10.7 KB
/
ImageUtil.java
File metadata and controls
282 lines (237 loc) · 10.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package utils;
import org.omg.CORBA.PUBLIC_MEMBER;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Zhongyi on 2/8/16.
*/
public class ImageUtil {
public static final int RGB_BLACK = 0xff000000, RGB_WHITE = 0xffffffff,
RGB_RED = 0xffff0000, RGB_GREEN = 0xff00ff00, RGB_BLUE = 0xff0000ff;
public static BufferedImage getGreyImage(BufferedImage image) {
int height = image.getHeight(), width = image.getWidth();
BufferedImage binaryImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = image.getRGB(i, j);
binaryImage.setRGB(i, j, rgb);
}
}
return binaryImage;
}
public static BufferedImage getDiffedImage(BufferedImage requestImage, BufferedImage backgroundImage, int tolerance) {
BufferedImage resultImage = deepCopyBufferImage(requestImage);
int height = resultImage.getHeight(), width = resultImage.getWidth();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int requestImageRGB = resultImage.getRGB(x, y),
backgroundImageRGB = backgroundImage.getRGB(x, y);
if (Math.abs(getGrayScale(requestImageRGB) - getGrayScale(backgroundImageRGB)) < tolerance) {
resultImage.setRGB(x, y, RGB_WHITE);
} else {
resultImage.setRGB(x, y, RGB_BLACK);
}
}
}
return resultImage;
}
public static BufferedImage getDenoisedImage(BufferedImage image, int tolerance) {
int height = image.getHeight(), width = image.getWidth();
BufferedImage imageCopy = deepCopyBufferImage(image);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int count = getContrastNeighborsCount(image, x, y);
if (count > tolerance) imageCopy.setRGB(x, y, RGB_BLACK | (RGB_WHITE - image.getRGB(x, y)));
}
}
return imageCopy;
}
public static BufferedImage getSeparatedBlockImage(BufferedImage image, int verticalTolerance, int horizontalTolerance) {
int height = image.getHeight(), width = image.getWidth();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; ) {
y = fillSeparatedBlocksVertically(image, x, y, y + verticalTolerance);
}
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; ) {
x = fillSeparatedBlocksHorizontally(image, y, x, x + horizontalTolerance);
}
}
return image;
}
public static BufferedImage getCroppedImage(BufferedImage image, int x, int y, int width, int height) {
BufferedImage img = image.getSubimage(x, y, width, height);
BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(img, 0, 0, null);
return copyOfImage;
}
public static BufferedImage getCroppedImage(BufferedImage image, Rectangle rec) {
return getCroppedImage(image, (int) rec.getX(), (int) rec.getY(), (int) rec.getWidth(), (int) rec.getHeight());
}
private static int[] getRGBArray(int rgb) {
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
return new int[]{red, green, blue};
}
private static double getGrayScale(int rgb) {
int[] rgbArray = getRGBArray(rgb);
return getGrayScale(rgbArray[0], rgbArray[1], rgbArray[2]);
}
private static double getGrayScale(int r, int g, int b) {
return r * 0.299 + g * 0.587 + b * 0.114;
}
/* Length of a must be equal to b. */
private static double getVectorDistance(int[] a, int[] b) {
if (a.length != b.length) return -1;
double sqrDistance = 0;
for (int i = 0; i < a.length; i++) {
sqrDistance += Math.pow(a[i] - b[i], 2);
}
return Math.sqrt(sqrDistance);
}
private static int getContrastNeighborsCount(BufferedImage image, int x, int y) {
int height = image.getHeight(), width = image.getWidth();
int[] offset = new int[]{-1, 0, 1};
int count = 0;
int ref = image.getRGB(x, y);
for (int anOffset : offset) {
for (int anotherOffset : offset) {
int xx = x + anOffset, yy = y + anotherOffset;
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
int value = image.getRGB(xx, yy);
if (value != ref) count += 1;
}
}
}
return count;
}
private static BufferedImage deepCopyBufferImage(BufferedImage image) {
ColorModel cm = image.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = image.copyData(null);
raster.createWritableChild(0, 0, image.getWidth(), image.getHeight(), 0, 0, null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
private static int fillSeparatedBlocksVertically(BufferedImage image, int x, int y1, int y2) {
int height = image.getHeight();
y2 = Math.min(y2, height);
if (image.getRGB(x, y1) == RGB_WHITE) return y1 + 1;
int startFillingFlag = y2;
for (int i = y2 - 1; i > y1; i--) {
if (startFillingFlag == y2 && image.getRGB(x, i) == RGB_BLACK) {
startFillingFlag = i;
}
if (startFillingFlag != y2) image.setRGB(x, i, RGB_BLACK);
}
return startFillingFlag;
}
private static int fillSeparatedBlocksHorizontally(BufferedImage image, int y, int x1, int x2) {
int width = image.getWidth();
x2 = Math.min(x2, width);
if (image.getRGB(x1, y) == RGB_WHITE) return x1 + 1;
int startFillingFlag = x2;
for (int i = x2 - 1; i > x1; i--) {
if (startFillingFlag == x2 && image.getRGB(i, y) == RGB_BLACK) {
startFillingFlag = i;
}
if (startFillingFlag != x2) image.setRGB(i, y, RGB_BLACK);
}
return startFillingFlag;
}
public static void markRectOnTheImage(BufferedImage image, Rectangle rectangle, int rgb) {
int height = image.getHeight(), width = image.getWidth(),
x1 = (int) rectangle.getX(), y1 = (int) rectangle.getY(),
x2 = Math.min(x1 + (int) rectangle.getWidth() - 1, width),
y2 = Math.min(y1 + (int) rectangle.getHeight() - 1, height);
for (int i = x1; i <= x2; i++) {
image.setRGB(i, y1, rgb);
image.setRGB(i, y2, rgb);
}
for (int i = y1; i <= y2; i++) {
image.setRGB(x1, i, rgb);
image.setRGB(x2, i, rgb);
}
}
public static void extendConnectedRegion(BufferedImage image, List<Rectangle> blockList, int[][] blockMap, int x, int y, int index, int depth) {
int height = image.getHeight(), width = image.getWidth();
if (image.getRGB(x, y) == RGB_BLACK && blockMap[x][y] == -1) {
if (index == blockList.size()) {
blockList.add(new Rectangle(x, y, 1, 1));
} else {
blockList.get(index).add(x, y);
}
blockMap[x][y] = index;
} else {
return;
}
int[] offset = new int[]{-1, 0, 1};
for (int anOffset : offset) {
for (int anotherOffset : offset) {
int xx = x + anOffset, yy = y + anotherOffset;
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
extendConnectedRegion(image, blockList, blockMap, xx, yy, index, depth + 1);
}
}
}
}
public static int getValidPixelsCountInRegion(BufferedImage image, Rectangle rectangle) {
int height = image.getHeight(), width = image.getWidth(),
x1 = (int) rectangle.getX(), y1 = (int) rectangle.getY(),
x2 = Math.min(x1 + (int) rectangle.getWidth() - 1, width),
y2 = Math.min(y1 + (int) rectangle.getHeight() - 1, height);
int count = 0;
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
if (image.getRGB(i, j) != RGB_WHITE) count += 1;
}
}
return count;
}
public static BufferedImage getGradientEdge(BufferedImage image, int tolerance) {
int height = image.getHeight(), width = image.getWidth();
BufferedImage imageCopy = deepCopyBufferImage(image);
if (image.getWidth() < 3 || image.getHeight() < 3) {
return null;
}
double[][] gradientBucket = new double[width][height];
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 1; i < image.getWidth() - 1; i++) {
for (int j = 1; j < image.getHeight() - 1; j++) {
double horizonG =
getGrayScale(image.getRGB(i + 1, j - 1)) + getGrayScale(image.getRGB(i + 1, j)) + getGrayScale(image.getRGB(i + 1, j + 1)) -
getGrayScale(image.getRGB(i - 1, j - 1)) - getGrayScale(image.getRGB(i - 1, j)) - getGrayScale(image.getRGB(i - 1, j + 1));
double verticalG =
getGrayScale(image.getRGB(i + 1, j + 1)) + getGrayScale(image.getRGB(i, j + 1)) + getGrayScale(image.getRGB(i - 1, j + 1)) -
getGrayScale(image.getRGB(i - 1, j - 1)) - getGrayScale(image.getRGB(i, j - 1)) - getGrayScale(image.getRGB(i + 1, j - 1));
gradientBucket[i][j] = Math.sqrt(
Math.pow(horizonG, 2) + Math.pow(verticalG, 2)
);
if (gradientBucket[i][j] > max) {
max = gradientBucket[i][j];
}
if (gradientBucket[i][j] < min) {
min = gradientBucket[i][j];
}
}
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
gradientBucket[i][j] = ((gradientBucket[i][j] - min) / (max - min)) * 255;
if (gradientBucket[i][j] > tolerance) {
imageCopy.setRGB(i, j, RGB_BLACK);
} else {
imageCopy.setRGB(i, j, RGB_WHITE);
}
}
}
return imageCopy;
}
}