-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImshow.java
258 lines (227 loc) · 7.06 KB
/
Imshow.java
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
/*
* Author: ATUL
* Thanks to Daniel Baggio , Jan Monterrubio and sutr90 for improvements
* This code can be used as an alternative to imshow of OpenCV for JAVA-OpenCv
* Make sure OpenCV Java is in your Build Path
* Usage :
* -------
* Imshow ims = new Imshow("Title");
* ims.showImage(Mat image);
* Check Example for usage with Webcam Live Video Feed
*/
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
//import java.io.ByteArrayInputStream;
//import java.io.InputStream;
//import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
//import javax.swing.plaf.ButtonUI;
import javax.swing.WindowConstants;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
public class Imshow {
public JFrame Window;
private ImageIcon image;
private JLabel label;
// private MatOfByte matOfByte;
private Boolean SizeCustom;
private int Height, Width;
public Imshow(String title) {
Window = new JFrame();
image = new ImageIcon();
label = new JLabel();
// matOfByte = new MatOfByte();
label.setIcon(image);
Window.getContentPane().add(label);
Window.setResizable(false);
Window.setTitle(title);
SizeCustom = false;
setCloseOption(0);
}
public Imshow(String title, int height, int width) {
SizeCustom = true;
Height = height;
Width = width;
Window = new JFrame();
image = new ImageIcon();
label = new JLabel();
// matOfByte = new MatOfByte();
label.setIcon(image);
Window.getContentPane().add(label);
Window.setResizable(false);
Window.setTitle(title);
setCloseOption(0);
}
public void showImage(Mat img) {
if (SizeCustom) {
Imgproc.resize(img, img, new Size(Height, Width));
}
// Highgui.imencode(".jpg", img, matOfByte);
// byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
// InputStream in = new ByteArrayInputStream(byteArray);
// bufImage = ImageIO.read(in);
bufImage = toBufferedImage(img);
image.setImage(bufImage);
Window.pack();
label.updateUI();
Window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
// CREDITS TO DANIEL: http://danielbaggio.blogspot.com.br/ for the improved
// version !
public BufferedImage toBufferedImage(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels() * m.cols() * m.rows();
byte[] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster()
.getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
// Thanks to sutr90 for reporting the issue : https://github.com/sutr90
public void setCloseOption(int option) {
switch (option) {
case 0:
Window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
break;
case 1:
Window.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
break;
default:
Window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
/**
* Sets whether this window should be resizable or not, by default it is not
* resizable
*
* @param resizable
* <code>true</code> if the window should be resizable,
* <code>false</code> otherwise
*/
public void setResizable(boolean resizable) {
Window.setResizable(resizable);
}
// Thanks to Jan Monterrubio for additional static methods for viewing images.
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow}
*
* @param mat
* the {@link Mat} to display
*/
public static void show(Mat mat) {
show(mat, new Dimension(mat.rows(), mat.cols()), "", false,
WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with
* the given title as the title for the window
*
* @param mat
* the {@link Mat} to display
* @param frameTitle
* the title for the frame
*/
public static void show(Mat mat, String frameTitle) {
show(mat, new Dimension(mat.rows(), mat.cols()), frameTitle, false,
WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with
* the given title as the title for the window and determines whether the
* frame is resizable or not
*
* @param mat
* the {@link Mat} to display
* @param frameTitle
* the title for the frame
* @param resizable
* whether the frame should be resizable or not
*/
public static void show(Mat mat, String frameTitle, boolean resizable) {
show(mat, new Dimension(mat.rows(), mat.cols()), frameTitle, resizable,
WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with a
* set size
*
* @param mat
* the {@link Mat} to display
* @param frameSize
* the size for the frame
*/
public static void show(Mat mat, Dimension frameSize) {
show(mat, frameSize, "", false, WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with a
* set size and given title
*
* @param mat
* the {@link Mat} to display
* @param frameSize
* the size for the frame
* @param frameTitle
* the title for the frame
*/
public static void show(Mat mat, Dimension frameSize, String frameTitle) {
show(mat, frameSize, frameTitle, false, WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with a
* set size and given title and whether it is resizable or not
*
* @param mat
* the {@link Mat} to display
* @param frameSize
* the size for the frame
* @param frameTitle
* the title for the frame
*/
public static void show(Mat mat, Dimension frameSize, String frameTitle,
boolean resizable) {
show(mat, frameSize, frameTitle, resizable,
WindowConstants.EXIT_ON_CLOSE);
}
/**
* Displays the given {@link Mat} in a new instance of {@link Imshow} with a
* set size and given title and whether it is resizable or not, and with the
* close operation set
*
* @param mat
* the {@link Mat} to display
* @param frameSize
* the size for the frame
* @param frameTitle
* the title for the frame
* @param resizable
* wether the frame is resizable or not
* @param closeOperation
* the constant for the default close operation of the frame
*/
public static void show(Mat mat, Dimension frameSize, String frameTitle,
boolean resizable, int closeOperation) {
Imshow frame = new Imshow(frameTitle, frameSize.height, frameSize.width);
frame.setResizable(resizable);
/*
* This is a bad way to access the window, but due to legacy stuff I
* won't change the access patterns
*/
frame.Window.setDefaultCloseOperation(closeOperation);
frame.showImage(mat);
}
}