Opencv java - 将图像加载到 GUI [英] Opencv java - Load image to GUI

查看:36
本文介绍了Opencv java - 将图像加载到 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java Opencv-2.4.4 和 swing GUI 开发应用程序.问题是我找不到任何解决方案,它显示了如何将处理过的图像(保存在 Mat 对象中)打印到 java swing GUI 的有效方法.目前我正在使用这个笨拙的解决方案:

javax.swing.JLabel outputImage;outputImage.setIcon(new javax.swing.ImageIcon("/home/username/Output.png"));私有无效滑块状态更改(javax.swing.event.ChangeEvent evt){..垫精明;//这里保存了我想要绘制的内容String filename = "/home/username/Output.png";Highgui.imwrite(filename, canny);//写入磁盘outputImage.setIcon(new ImageIcon(ImageIO.read(new File(filename))));//更新图标..}

当用户更改某些值、输入等时,在 GUI 中我必须覆盖磁盘上的 Output.png 并使用磁盘中的新图像更新 jLabel.

有没有更优雅/有效的解决方案?是否可以将 Mat 对象直接绘制或转换为 CanvasImage 或任何可打印为图像的摆动图像?

解决方案

jpeg 编码很有趣,但有几个问题:

  • 它不是无损格式,压缩时会丢失图像数据
  • 需要相当长的时间(大约比下面建议的时间长 6 到 10 倍)
<块引用>

public Image toBufferedImage(Mat m){int type = BufferedImage.TYPE_BYTE_GRAY;如果 ( m.channels() > 1 ) {类型 = BufferedImage.TYPE_3BYTE_BGR;}int bufferSize = m.channels()*m.cols()*m.rows();字节 [] b = 新字节 [bufferSize];m.get(0,0,b);//获取所有像素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);返回图像;}

I'm developing an application using Java Opencv-2.4.4 and swing GUI. Problem is that I'm unable to find any solution, that shows efficient way how to print processed image (saved in Mat object) to java swing GUI. For this moment I'm using this clumsy solution:

javax.swing.JLabel outputImage;
outputImage.setIcon(new javax.swing.ImageIcon("/home/username/Output.png"));

private void sliderStateChanged(javax.swing.event.ChangeEvent evt) { 
       .
       .
  Mat canny; // Here is saved what I want to plot
  String filename = "/home/username/Output.png";
  Highgui.imwrite(filename, canny);  // write to disk
  outputImage.setIcon(new ImageIcon(ImageIO.read(new File(filename)))); //update Icon
       .
       .
}

When user changes some values, inputs etc ., in GUI I have to overwrite Output.png on disk and update jLabel with new image from disk.

Is there any more elegant / efficient solution to this ? Is it posible to plot or convert Mat object directly to Canvas or Image or anything that is printable as image in swing ?

解决方案

jpeg encoding is interesting, but there are a couple problems:

  • it is not a lossless format, you will lose image data when compressing
  • it takes quite a while (around 6 to 10 times longer than the suggested one below)

public Image 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;

  }

这篇关于Opencv java - 将图像加载到 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆