使用 jframe (OpenCV 3.00) 在 mat 中显示图像
Display image in mat with jframe (OpenCV 3.00)
如何在JFrame
中显示Mat图像。 SetIcon()
不接受mat参数。显示前应在 OpenCV 3.00
中使用图像。但是 OpenCV 只能打开 mat 图像。有什么方法可以转换图像吗?
public void displayImage()
{
Mat image = Imgcodecs.imread(getClass().getResource("lena.png").getPath());
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
JLabel lbl=new JLabel();
lbl.setIcon(image);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
我确实找到了转换它的代码,但它有 highgui
,它不再在 OpenCV 3.00 中。
MatOfByte byteMat = new MatOfByte();
Highgui.imencode(".bmp", mat, byteMat);
return new Image(new ByteArrayInputStream(byteMat.toArray()));
将 Mat 转换为 BufferedImage,然后将其绘制在 Panel 上,Canvas 或类似的:
public static BufferedImage bufferedImage(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
m.get(0,0,((DataBufferByte)image.getRaster().getDataBuffer()).getData()); // get all the pixels
return image;
}
如何在JFrame
中显示Mat图像。 SetIcon()
不接受mat参数。显示前应在 OpenCV 3.00
中使用图像。但是 OpenCV 只能打开 mat 图像。有什么方法可以转换图像吗?
public void displayImage()
{
Mat image = Imgcodecs.imread(getClass().getResource("lena.png").getPath());
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
JLabel lbl=new JLabel();
lbl.setIcon(image);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
我确实找到了转换它的代码,但它有 highgui
,它不再在 OpenCV 3.00 中。
MatOfByte byteMat = new MatOfByte();
Highgui.imencode(".bmp", mat, byteMat);
return new Image(new ByteArrayInputStream(byteMat.toArray()));
将 Mat 转换为 BufferedImage,然后将其绘制在 Panel 上,Canvas 或类似的:
public static BufferedImage bufferedImage(Mat m) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
m.get(0,0,((DataBufferByte)image.getRaster().getDataBuffer()).getData()); // get all the pixels
return image;
}