-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple_Filters.java
More file actions
195 lines (159 loc) · 4.81 KB
/
Simple_Filters.java
File metadata and controls
195 lines (159 loc) · 4.81 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import ij.plugin.frame.*;
import ij.*;
import ij.process.*;
import ij.gui.*;
/*
This plugin must be compiled and run from the inside of the ImageJ
program.
Plugins -> Compile and Run... -> ../Simple_Filters.java
This ImageJ plugin creates a simple graphical user interface with 5
options to manipulate any desired image that must be accesed through
ImageJ program.
Filters that are implemented into this plugin:
Median Filter - used to reduce noise from the image;
Contrast Filter - used to lower the contrast of the image;
Sobel X Filter - used to emphasize the edges on the X axis;
Sobel Y Filter - used to emphasize the edges on the Y axis;
Reset - used to reset the image to it's primary state;
*/
public class Simple_Filters extends PlugInFrame implements ActionListener { // Simple_Filters class
private Panel panel;
private static Frame frame;
private int previousID;
public Simple_Filters() {
super("Simple Filters");
if (frame != null) {
frame.toFront();
return;
}
frame = this;
// Puts a key listener on the main ImageJ object.
addKeyListener(IJ.getInstance());
setLayout(new FlowLayout());
panel = new Panel();
panel.setLayout(new GridLayout(2, 3, 5, 5));
addButton("Median Filter");
addButton("Contrast Filter");
addButton("Sobel X Filter");
addButton("Sobel Y Filter");
addButton("Reset");
add(panel);
// Lays out the Window class components with the
// desired dimensions in the window.
pack();
// Positions the plugin window in the middle
// of the screen.
GUI.center(this);
setVisible(true);
}
void addButton(String label) {
Button b = new Button(label);
b.addActionListener(this);
// Adds a key listener for the buttons on
// the main ImageJ object.
b.addKeyListener(IJ.getInstance());
panel.add(b);
}
public void actionPerformed(ActionEvent e) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp == null) {
IJ.beep(); // Creates a beep sound.
IJ.showStatus("No image."); // Prints a message on the status bar.
previousID = 0;
return;
}
if (!imp.lock()) {
previousID = 0;
return;
}
int id = imp.getID();
if (id != previousID) {
// Returns the pixel copy of an image so it can be reset.
imp.getProcessor().snapshot();
}
previousID = id;
String label = e.getActionCommand();
if (label == null) {
return;
}
new Runner(label, imp);
}
public void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
frame = null;
}
}
class Runner extends Thread { // Runner class
private String command;
private ImagePlus imp;
Runner(String command, ImagePlus imp) {
super(command);
this.command = command;
this.imp = imp;
setPriority(Math.max(getPriority()-2, MIN_PRIORITY));
start();
}
public void run() {
try {
runCommand(command, imp);
} catch(OutOfMemoryError e) {
IJ.outOfMemory(command);
if (imp != null) {
imp.unlock();
}
} catch(Exception e) {
CharArrayWriter caw = new CharArrayWriter();
PrintWriter pw = new PrintWriter(caw);
e.printStackTrace(pw);
IJ.log(caw.toString());
IJ.showStatus("");
if (imp != null) {
imp.unlock();
}
}
}
void runCommand(String command, ImagePlus imp) {
// Returns an ImageProcessor class associated with the current
// image. It is used to manipulate the image. If there is no
// ImageProcessor associated with the image, then null is returned.
ImageProcessor ip = imp.getProcessor();
IJ.showStatus(command + "...");
long startTime = System.currentTimeMillis();
// Returns the current selection on the image. Returns null
// if there is no selection present.
Roi roi = imp.getRoi();
// Returns the selected region (ROI - region of interest) as
// the ByteProcessor object.
ImageProcessor mask = roi != null ? roi.getMask() : null;
if (command.equals("Median Filter")) {
ip.medianFilter();
}
else if (command.equals("Contrast Filter")) {
int[] contrast = new int[] {0, -1, 0, -1, 5, -1, 0, -1, 0};
ip.convolve3x3(contrast);
}
else if (command.equals("Sobel X Filter")) {
int[] sobelX = new int[] {-1, 0, 1, -2, 0, 2, -1, 0, 1};
ip.convolve3x3(sobelX);
}
else if (command.equals("Sobel Y Filter")) {
int[] sobelY = new int[] {-1, -2, -1, 0, 0, 0, 1, 2, 1};
ip.convolve3x3(sobelY);
}
else if (command.equals("Reset")) {
ip.reset();
}
if (mask != null) {
ip.reset(mask);
}
// Updates the image according to the used filter.
imp.updateAndDraw();
imp.unlock();
IJ.showStatus((System.currentTimeMillis() - startTime) + " milliseconds");
}
} // Runner class
} // Simple_Filters class