下载openCV
官网地址:Releases - OpenCV
下载地址:Download OpenCV from SourceForge.net
这里我用的opencv440的windows版本
下载完成后点击exe进行安装,安装完成后会生成opencv文件夹,里面有各个环境文件
boot 示例
1.在opencv\build\java\以及opencv\build\java\x64下提取jar包以及动态库。
2.在resources下创建lib/opencv文件夹,并将这两个包放进去。
3.在pom.xml添加依赖如下(或者在项目之中添加jar包)
<dependency>
<groupId>org</groupId>
<artifactId>opencv</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}\src\main\resources\lib\opencv\opencv-440.jar</systemPath>
</dependency>
4. 灰化图片Demo
package com.me.lab.lab_tool;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.COLOR_RGB2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;
/**
* @author An
* @title: DemoApplicationTests
* @projectName me_lab
* @description: 灰化图片
* @date 2022/1/5 0005下午 22:06
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
/**
* @return
* @Description
* @Param
* @Author zhangsan
* @Date 2020.09.05 9:43
**/
@Test
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("D:\\IDEA\\me\\me_lab\\lab_tool\\img\\11.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:\\IDEA\\me\\me_lab\\lab_tool\\img\\hui11.jpg", grayImage);
waitKey();
}
}
效果图