【问题标题】:Display image in mat with jframe (OpenCV 3.00)使用 jframe 在 mat 中显示图像(OpenCV 3.00)
【发布时间】:2015-07-27 07:10:39
【问题描述】:

如何在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()));

【问题讨论】:

    标签: java swing opencv image-processing


    【解决方案1】:

    将 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多