-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathImageProcessingFrame.java
More file actions
191 lines (173 loc) · 5.73 KB
/
Copy pathImageProcessingFrame.java
File metadata and controls
191 lines (173 loc) · 5.73 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
package imageProcessing;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
* This frame has a menu to load an image and to specify various transformations, and a component to
* show the resulting image.
*/
public class ImageProcessingFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400;
private BufferedImage image;
public ImageProcessingFrame()
{
setTitle("ImageProcessingTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
add(new JComponent()
{
public void paintComponent(Graphics g)
{
if (image != null) g.drawImage(image, 0, 0, null);
}
});
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
openFile();
}
});
fileMenu.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
JMenuItem blurItem = new JMenuItem("Blur");
blurItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
float weight = 1.0f / 9.0f;
float[] elements = new float[9];
for (int i = 0; i < 9; i++)
elements[i] = weight;
convolve(elements);
}
});
editMenu.add(blurItem);
JMenuItem sharpenItem = new JMenuItem("Sharpen");
sharpenItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f };
convolve(elements);
}
});
editMenu.add(sharpenItem);
JMenuItem brightenItem = new JMenuItem("Brighten");
brightenItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
float a = 1.1f;
// float b = 20.0f;
float b = 0;
RescaleOp op = new RescaleOp(a, b, null);
filter(op);
}
});
editMenu.add(brightenItem);
JMenuItem edgeDetectItem = new JMenuItem("Edge detect");
edgeDetectItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f };
convolve(elements);
}
});
editMenu.add(edgeDetectItem);
JMenuItem negativeItem = new JMenuItem("Negative");
negativeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
short[] negative = new short[256 * 1];
for (int i = 0; i < 256; i++)
negative[i] = (short) (255 - i);
ShortLookupTable table = new ShortLookupTable(0, negative);
LookupOp op = new LookupOp(table, null);
filter(op);
}
});
editMenu.add(negativeItem);
JMenuItem rotateItem = new JMenuItem("Rotate");
rotateItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (image == null) return;
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(5),
image.getWidth() / 2, image.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BICUBIC);
filter(op);
}
});
editMenu.add(rotateItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
/**
* Open a file and load the image.
*/
public void openFile()
{
JFileChooser chooser = new JFileChooser(".");
chooser.setCurrentDirectory(new File(getClass().getPackage().getName()));
String[] extensions = ImageIO.getReaderFileSuffixes();
chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions));
int r = chooser.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
try
{
Image img = ImageIO.read(chooser.getSelectedFile());
image = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, null);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e);
}
repaint();
}
/**
* Apply a filter and repaint.
* @param op the image operation to apply
*/
private void filter(BufferedImageOp op)
{
if (image == null) return;
image = op.filter(image, null);
repaint();
}
/**
* Apply a convolution and repaint.
* @param elements the convolution kernel (an array of 9 matrix elements)
*/
private void convolve(float[] elements)
{
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
filter(op);
}
}