OpenCV-图像处理- Java(读取与显示图片)

  • 在C++显示图片,OpenCV为其提供了一个叫做HighGUI的接口,尚且没有在java中实现。
  • OpenCV支持有限的图片格式,具体可参阅官方文档(常用的支持bmp、jpeg、png、tiff),可以使用Imgcodecs类的imread方法来读取图片(注意测试时,路径不要包括中文):

结构目录(Eclipse中):

创建两个类:OpenCVTest 和 ImageViewer
OpenCV-图像处理- Java(读取与显示图片)_第1张图片

步骤

  1. 创建类 OpenCVTest
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
 
public class OpenCVTest {
    public static void main(String[] args) {
        // 载入dll(必须先加载)
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //将文件读入为OpenCV的Mat格式。注意测试时,路径不要包括中文
        Mat src = Imgcodecs.imread("E:/Pictures/test.jpg");
        if(src.dataAddr()==0){
            System.out.println("打开文件出错");
        }
        ImageViewer imageViewer = new ImageViewer(src, "第一幅图片");
        imageViewer.imshow();
    }
}
  1. 创建类 ImageViewer
import org.opencv.core.Mat;
 
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
 
 
public class ImageViewer {
 
    private JLabel imageView;
    private Mat image;
    private String windowName;
 
    /**
     * 如果使用junit测试时调用该方法,图像会一闪而过,可通过sleep()等方式暂时显示
     * @param
     */
    public ImageViewer(Mat image) {
        this.image = image;
    }
 
 
    /**
     * @param image      要显示的mat
     * @param windowName 窗口标题
     */
    public ImageViewer(Mat image, String windowName) {
        this.image = image;
        this.windowName = windowName;
    }
 
    /**
     * 图片显示
     */
    public void imshow() {
        setSystemLookAndFeel();
        Image loadedImage = toBufferedImage(image);
        JFrame frame = createJFrame(windowName, image.width(), image.height());
        imageView.setIcon(new ImageIcon(loadedImage));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 用户点击窗口关闭
    }
 
    private void setSystemLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }
 
    private JFrame createJFrame(String windowName, int width, int height) {
        JFrame frame = new JFrame(windowName);
        imageView = new JLabel();
        final JScrollPane imageScrollPane = new JScrollPane(imageView);
        imageScrollPane.setPreferredSize(new Dimension(width, height));
        frame.add(imageScrollPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        return frame;
    }
 
 
    private Image toBufferedImage(Mat matrix) {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (matrix.channels() > 1) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = matrix.channels() * matrix.cols() * matrix.rows();
        byte[] buffer = new byte[bufferSize];
        matrix.get(0, 0, buffer); // 获取所有的像素点
        BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
        return image;
    }
}

参考博客

  1. https://blog.csdn.net/zmx729618/article/details/78113783
  2. https://blog.csdn.net/qq_36224522/article/details/81390110
  3. https://blog.csdn.net/byna11sina11/article/details/79044738

你可能感兴趣的:(OpenCV,OpenCV,Java,读取与显示图片,图像处理)