创建项目
Maven集成 OpenCV
1 2 3 4 5
| <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.5</version> </dependency>
|
引入的不要是 OpenPnP OpenCV 而是 bytedeco/javacv
javacv 自带了各平台 OpenCV .dll .so .o .dylib 不用再自己安装 OpenCV了
无需加载动态库
URL url = ClassLoader.getSystemResource(“lib/opencv/opencv_java440.dll”);
System.load(url.getPath())
或者
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //加载动态链接库
使用 bytedeco 有一点要注意
OpenCV 原本的 import 路径为 org.opencv
JavaCV import 路径为 org.bytedeco.opencv
路径不是完全对应的 比如 Imgcodecs
原API 路径为: org.opencv.imgcodecs.Imgcodecs
bytedeco 路径为: org.bytedeco.opencv.global.opencv_imgcodecs
DEMO
网上随便找了个示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import org.opencv.core.*; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier;
public class FaceOpenCV {
static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
static void face() { CascadeClassifier faceCV = new CascadeClassifier("G:\\SystemSoft\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"); Mat image = Imgcodecs.imread("F:\\workTest\\FaceOpenCVData\\1test.png"); MatOfRect face = new MatOfRect(); faceCV.detectMultiScale(image, face); Rect[] rects = face.toArray(); System.out.println("匹配到" + rects.length + "个人脸"); for (int i = 0; i < rects.length; i++) { Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 0, 255)); Imgproc.putText(image, "FACE", new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false); } HighGui.imshow("人脸识别", image); HighGui.waitKey(0); }
public static void main(String[] args) { face(); } }
|
用 javacv 修改一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import org.bytedeco.opencv.global.opencv_imgcodecs; import org.bytedeco.opencv.global.opencv_imgproc; import org.bytedeco.opencv.opencv_core.Mat; import org.bytedeco.opencv.opencv_core.Point; import org.bytedeco.opencv.opencv_core.Rect; import org.bytedeco.opencv.opencv_core.RectVector; import org.bytedeco.opencv.opencv_core.Scalar; import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
public class App { public static void main(String[] args) { CascadeClassifier facebook = new CascadeClassifier("\\data\\haarcascades\\haarcascade_frontalface_alt.xml"); Mat image = opencv_imgcodecs.imread("\\1.jpg"); RectVector face = new RectVector(); facebook.detectMultiScale(image, face); Rect[] rects = face.get(); System.out.println("匹配到 " + rects.length + " 个人脸"); for (int i = 0; i < rects.length; i++) { opencv_imgproc.rectangle(image, new Point(rects[i].x(), rects[i].y()), new Point(rects[i].x() + rects[i].width(), rects[i].y() + rects[i].height()), new Scalar(0, 0, 255, 0)); opencv_imgproc.putText(image, "Human", new Point(rects[i].x(), rects[i].y()), opencv_imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, new Scalar(0, 255, 0, 0), 1, opencv_imgproc.LINE_AA, false); } opencv_imgcodecs.imwrite("\\2.jpg", image);
} }
|
使用 MAVEN 时需要添加
-Djavacpp.platform=linux-x86_64
指定目标平台
不然 windows linux macos x86 x64 一堆jar都会被导入
源码