-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtilities.java
More file actions
156 lines (140 loc) · 4.57 KB
/
Copy pathImageUtilities.java
File metadata and controls
156 lines (140 loc) · 4.57 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
import org.bytedeco.javacv.CanvasFrame;
import java.awt.*;
import java.awt.image.*;
import java.nio.Buffer;
/**
* Created by Linus Karsai (312070209) on 27/05/2014.
*/
public class ImageUtilities {
/**
* Apply convolution filter to image
* @param img input
* @param k convolution filter
* @return altered image
*/
public static BufferedImage convolutionFilter(BufferedImage img, Kernel k){
// Convert image to grayscale
BufferedImage original = deepCopyImage(img);
BufferedImage tmp = null;
BufferedImageOp op = new ConvolveOp(k);
tmp = op.filter(original, null);
return tmp;
}
/**
* Create a deep copy of a buffered image
* @param bi input image
* @return copy
*/
public static BufferedImage deepCopyImage(BufferedImage bi) {
BufferedImage copyofImage =
new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = copyofImage.createGraphics();
g.drawImage(bi, 0, 0, null);
return copyofImage;
}
/**
* Get the grey pixel value from colour
* @param c
* @return
*/
public static int getGreyPixel(Color c){
int newColour = (int)(
c.getRed() * 0.299 +
c.getGreen() * 0.587 +
c.getBlue() * 0.114
);
return newColour;
}
/**
* Get a two-tone version of the pixel back
* @param pixelColor input pixel
* @param threshold value
* @param foreground colour
* @param background colour
* @return output colour
*/
public static Color getTwoTonePixel(Color pixelColor, int threshold, Color foreground, Color background){
int greyInt = getGreyPixel(pixelColor);
if (greyInt > threshold){
return foreground;
} else
return background;
}
/**
* Create an array with the number of each luminosity
* according to index
* @param img
* @return
*/
public static double[] histogramArray(BufferedImage img) {
String output = "";
double histogram[] = new double[256];
int sensitivity = 5;
for (int column = 0; column < img.getHeight(); column += sensitivity){
for (int row = 0; row < img.getWidth(); row += sensitivity){
int greyLevel = getGreyPixel(new Color(img.getRGB(row, column)));
++histogram[greyLevel];
}
}
return histogram;
}
/**
* Get string version of histogram
* @param img input image
* @return string
*/
public static String getHistogram(BufferedImage img) {
String output = "";
double histogram[] = new double[256];
int sensitivity = 5;
for (int column = 0; column < img.getHeight(); column += sensitivity){
for (int row = 0; row < img.getWidth(); row += sensitivity){
int greyLevel = getGreyPixel(new Color(img.getRGB(row, column)));
++histogram[greyLevel];
}
}
output += "HISTOGRAM\n";
double tally = 0;
int steps = 25;
int lastStep = 0;
for (int greyLevel = 0; greyLevel <= 255; ++greyLevel) {
tally += histogram[greyLevel];
if ((greyLevel%steps == 0 && greyLevel != 0) || greyLevel == 255) {
String start = String.format("%03d", lastStep);
String end = String.format("%03d", greyLevel);
output += start + "-" + end + ":";
tally = tally/steps;
for (int i = 0; i <= tally; i += 100/(sensitivity*sensitivity)) {
output += "*";
}
output += "\n";
lastStep = greyLevel+1;
}
}
int total = 0;
for (int i = 0; i <= 255; ++i){
total += histogram[i];
}
return output;
}
/**
* print histogram to standard output
* @param img
*/
public static void printHistogram(BufferedImage img) {
System.out.println(getHistogram(img));
}
/**
* Apply contrast/brightness changes to image
* @param img input image
* @param scaleFactor contrast
* @param brightness
* @return output img
*/
public static BufferedImage contrast(BufferedImage img, float scaleFactor, float brightness) {
BufferedImage tmp = deepCopyImage(img);
RescaleOp rescaleOp = new RescaleOp(scaleFactor, brightness, null);
rescaleOp.filter(tmp,tmp);
return tmp;
}
}