环境:jdk1.8/ scala2.11 / idea / win7
写在前
- 不需要在windows上安装opencv。已经通过javacpp自动安装相关 jni dll
- 自己参考 javacv-examples。javacv相关的例子在 OpenCV_Cookbook 目录下,仔细阅读 README.md
- 可以先了解 javacpp 和 javacpp-presets
- 例子都是java gui相关的:jwt 、javaFX
入门例子
- pom.xml
javacv-platform会加入很多依赖,非必要不要自定义引入javacv/javacpp替代
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.4.0-1.5.4</version>
</dependency>
- 入门代码 MyFirstOpenCVApp.scala
import javax.swing._
import org.bytedeco.javacv._
import org.bytedeco.opencv.global.opencv_core._
import org.bytedeco.opencv.global.opencv_imgcodecs._
import org.bytedeco.opencv.global.opencv_imgproc._
import org.bytedeco.opencv.opencv_core._
object MyFirstOpenCVApp extends App {
// Read an image. 改为自己的图片路径即可
val src = imread("data/boldt.jpg")
display(src, "Input")
// Apply Laplacian filter
val dest = new Mat()
Laplacian(src, dest, src.depth(), 1, 3, 0, BORDER_DEFAULT)
display(dest, "Laplacian")
//---------------------------------------------------------------------------
/** Display `image` with given `caption`. */
def display(image: Mat, caption: String): Unit = {
// Create image window named "My Image."
val canvas = new CanvasFrame(caption, 1)
// Request closing of the application when the image window is closed.
canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
// Convert from OpenCV Mat to Java Buffered image for display
val converter = new OpenCVFrameConverter.ToMat()
// Show image on window
canvas.showImage(converter.convert(image))
}
}
- 运行,可以看到 jwt 弹出图片
api 与 其他例子
api文档链接,自己慢慢研究example。javacv官方推荐先阅读 opencv相关文档。
javacv example路径:https://github.com/bytedeco/javacv-examples。javacv相关的例子在 OpenCV_Cookbook
遇到过的问题
- 找不到 jnijavacpp.dll
控制台报错:
no openblas_nolapack in java.library.path
解决:添加maven依赖:javacv-platform