在 Java 中使用 OpenCV 和 JavaCV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using OpenCV in Java with JavaCV
提问by Matthieu Napoli
I am getting desperate !! I am trying to use OpenCVin Java, via JavaCV(JNA to wrap OpenCV for java).
我快绝望了!!我正在尝试通过JavaCV(JNA 为 Java 包装 OpenCV)在Java 中使用OpenCV。
I am on Mac Os X1.5.
我在Mac Os X1.5 上。
I installed OpenCV, and I can compile and run the examples included. So that works.
我安装了 OpenCV,并且可以编译和运行包含的示例。所以这有效。
Now I open Eclipse, and I create a new project, as described here : http://code.google.com/p/javacv/
现在我打开Eclipse,并创建一个新项目,如下所述:http: //code.google.com/p/javacv/
In that new project, only one small class with a call to a opencv function (I used the sample code) :
在那个新项目中,只有一个调用 opencv 函数的小类(我使用了示例代码):
import static name.audet.samuel.javacv.jna.cxcore.*;
import static name.audet.samuel.javacv.jna.cv.*;
import static name.audet.samuel.javacv.jna.highgui.*;
import static name.audet.samuel.javacv.jna.cvaux.*;
public class Test {
public static void main(String[] args) {
IplImage image = cvLoadImage("test.png", 1);
if (image == null) {
System.err.println("Could not load image file.");
} else {
cvSmooth(image, image, CV_GAUSSIAN, 3, 0, 0, 0);
// ...
}
}
}
When I run it, I have the following error :
当我运行它时,出现以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'cxcore': dlopen(libcxcore.dylib, 9): image not found
线程“main”中的异常 java.lang.UnsatisfiedLinkError: Unable to load library 'cxcore': dlopen(libcxcore.dylib, 9): image not found
Please, I need help, I looked over google for hours, I don't know where to look for anymore.
拜托,我需要帮助,我在谷歌上看了几个小时,我不知道去哪里找了。
采纳答案by Matthieu Napoli
It turned out the SVN version was not compatible with JavaCV.
结果发现 SVN 版本与 JavaCV 不兼容。
I downloaded the latest official version (2.1) and compiled it and installed it, and it works.
我下载了最新的官方版本(2.1),编译安装,运行正常。
回答by Radim Burget
You need to link these two libraries:
您需要链接这两个库:
- javacpp.jar
- javacv.jar
In the JavaCV/lib-opencv/win_x86_64 you have to have files of your like:
在 JavaCV/lib-opencv/win_x86_64 中,你必须有你喜欢的文件:
- msvcp100.dll
- msvcr100.dll
- opencv_core220.dll
- opencv_calib3d220.dll
- ...
These DLLs have to be compiled for your platform (win 32 / win 64 / Linux / etc.
必须为您的平台(win 32 / win 64 / Linux / 等)编译这些 DLL。
You have to define path to your OpneCV DLL files:
您必须定义 OpneCV DLL 文件的路径:
-Djava.library.path=lib-opencv/win_x86_64/
- or the DLLs have to be somewhere in your system PATH of your operating system
回答by darkhipo
I had looked at this problem for a while, as the OP suggests all kinds of problems start crawling out of the woodwork. I went through a ton of StackOverflow posts to be able to come up with a relatively painless experience for setting up a OpenCV project in Java. I went through JavaCV and found that it did not meet my needs. I was however able to directly implement functionality referenced in OpenCV posts (C++ posts) but in the Java language when I used certain javacpp versions. I had a ton of compilation issues too since javacpp depends on compiled C++ libraries that must be native to the environment the user is in (something people that live in Java land love to not deal with). Anyway I was able to construct and environment with Maven. I use eclipse but this should work fine with other programming environments. I put up an example project to illustrate how to start build a bootstrap project and start working. The project compares 2 images, given their URLs. It's a equality test, wither the images are identical or not. Hopefully this can help folks setup and get working in this environment and avoid the tons and tons of pitfalls that I encountered when trying to work with OpenCV in Java (I was in the exact same place as OP mentally at that time :) ).
我已经研究了一段时间这个问题,因为 OP 建议各种问题开始从木制品中爬出来。我浏览了大量 StackOverflow 帖子,以便能够为在 Java 中设置 OpenCV 项目提供相对轻松的体验。我浏览了JavaCV,发现它不能满足我的需求。然而,当我使用某些 javacpp 版本时,我能够直接实现在 OpenCV 帖子(C++ 帖子)中引用的功能,但是在 Java 语言中。我也有很多编译问题,因为 javacpp 依赖于编译的 C++ 库,这些库必须是用户所在环境的本机(生活在 Java 土地上的人喜欢不处理的东西)。无论如何,我能够使用 Maven 构建和环境。我使用 eclipse,但这应该适用于其他编程环境。我提出了一个示例项目来说明如何开始构建引导项目并开始工作。该项目比较 2 个图像,给定它们的 URL。这是一个平等测试,图像是否相同。希望这可以帮助人们设置并在这种环境中工作,并避免我在 Java 中尝试使用 OpenCV 时遇到的大量陷阱(当时我在精神上与 OP 处于完全相同的位置:))。
The example: https://github.com/darkhipo/ImgzCmp