天天看點

Java Opencv讀取圖檔并顯示

本篇部落格介紹如何在Java中使用Opencv實作對圖檔的讀取和顯示。

首先是顯示部分:

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
  
  
public class ShowImage {  

    private JFrame frame;  
  
  
    /** 
     * Create the application. 
     */  
    public ShowImage(Mat mat) {  
        initialize(mat);  
    }  
  
    public JFrame getFrame() {
		return frame;
	}
    /** 
     * Initialize the contents of the frame. 
     */  
    private void initialize(Mat mat) {  
        frame = new JFrame();  
        frame.setBounds(20, 100, mat.width()+100, mat.height()+80);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.getContentPane().setLay