1. 环境搭建。

opencv官网,往下拉。

opencv java 教程 java opencv mat_opencv java 教程

 

opencv java 教程 java opencv mat_System_02

 

 

opencv java 教程 java opencv mat_Image_03

 在opencv\build\java\以及opencv\build\java\x64下提取jar包以及动态库。

windows : dll

linux : so

mac : dylib

opencv java 教程 java opencv mat_opencv java 教程_04

 

 

<dependency>
            <groupId>org</groupId>
            <artifactId>opencv</artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}\src\main\resources\lib\opencv\opencv-440.jar</systemPath>
</dependency>class DemoApplicationTests {
public void testOpencv() throws Exception {
        // 解决awt报错问题
        System.setProperty("java.awt.headless", "false");
        System.out.println(System.getProperty("java.library.path"));
        // 加载动态库
        URL url = ClassLoader.getSystemResource("lib/opencv/opencv_java440.dll");
        System.load(url.getPath());
        // 读取图像
        Mat image = imread("C:\\Users\\admin\\Pictures\\hello.jpg");
        if (image.empty()) {
            throw new Exception("image is empty");
        }
        imshow("Original Image", image);

        // 创建输出单通道图像
        Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
        // 进行图像色彩空间转换
        cvtColor(image, grayImage, COLOR_RGB2GRAY);

        imshow("Processed Image", grayImage);
        imwrite("D://hello.jpg", grayImage);
        waitKey();
    }
}