下载
从github中下载,网址https://github.com/opencv/opencv/releases,也可以从官网https://opencv.org/releases/,下载opencv-4.1.1-vc14_vc15.exe
Idea配置
解压
exe解压到D:\YDGreen\,找到下面两个文件
- D:\YDGreen\opencv\build\java\opencv-411.jar
- D:\YDGreen\opencv\build\java\x64\opencv_java411.dll
复制到项目的resource\lib下
项目添加lib
pom.xml方式
<!-- OpenCV -->
<dependency>
<groupId>org</groupId>
<artifactId>opencv</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}\src\main\resources\lib\opencv-411.jar</systemPath>
</dependency>
或者在idea里面添加,如下所示
代码加载dll
加载dll的两种方式
// 方法一
启动参数添加
-Djava.library.path=D:\PData\baidunet\image-process\src\main\resources\lib\;
代码
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 方法二
System.load("D:\\PData\\baidunet\\image-process\\src\\main\\resources\\lib\\opencv_java411.dll");
验证
import org.junit.Test;
import org.opencv.core.Mat;
import java.util.ArrayList;
import java.util.List;
import static org.opencv.core.Core.merge;
import static org.opencv.core.Core.split;
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.imgproc.Imgproc.equalizeHist;
/**
* OpenCV测试
* @author ydfind
* @date 2019.9.20
*/
public class OpenCVTest {
/**
* 测试OpenCV是否能运行:需要自行修改图片位置
* @throws Exception 测试是否成功
*/
@Test
public void testOpenCV() throws Exception {
URL url = ClassLoader.getSystemResource("lib/opencv_java411.dll");
System.load(url.getPath());
Mat image = imread("D:\\tmp\\20190920122234.png", 1);
if (image.empty()){
throw new Exception("image is empty!");
}
imshow("Original Image", image);
List<Mat> imageRGB = new ArrayList<>();
split(image, imageRGB);
for (int i = 0; i < 3; i++) {
equalizeHist(imageRGB.get(i), imageRGB.get(i));
}
merge(imageRGB, image);
imshow("Processed Image", image);
waitKey();
}
}