-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.java
More file actions
39 lines (33 loc) · 1.26 KB
/
ImageUtils.java
File metadata and controls
39 lines (33 loc) · 1.26 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
import org.opencv.core.Mat;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
public class ImageUtils {
public static BufferedImage Mat2BufferedImage(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;
}
public static void display(Mat m, String title) {
BufferedImage img = ImageUtils.Mat2BufferedImage(m);
JFrame f = new JFrame();
f.setTitle(title);
ImageIcon icon = new ImageIcon(img);
f.setLayout(new FlowLayout());
f.setSize(img.getWidth(null) + 50, img.getHeight(null) + 50);
JLabel lbl = new JLabel();
lbl.setIcon(icon);
f.add(lbl);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}