intelliJ IEDA + opencv 开发上手

来源:互联网 发布:网络硬盘录像机多少钱 编辑:程序博客网 时间:2024/06/07 07:16
最近整图像匹配的开发环境太难了,VISUAL STUDIO 2013+OPENCV 3.0失败,原因是不会C++,哭哭哭。转战MATLAB2015B下载完成后能用了,测试几天后不满意,还是觉得OPENCV更靠谱,于是乎换用INTELLIJ + OPENCV3.0 。。。部分成功,图像匹配上终于跑通了,可是不能用SURF算法,究其原因应该是OPENCV本身对JAVA支持的不好,然后换用旧版本2.4.11,哈哈,终于成功了。
总结一下在intelliJ环境下配置OPENCV的过程。
首先下载OPENCV,是个exe,然后文件解压,配置环境变量,在系统变量中的path添加如:F:\opencv2411\opencv\build\java\x64,(64位机器)其中opencv2411是自己创建的文件夹,表示2.4.11版本。然后打开intelliJ软件,File--ProjectStructure--Modules--Dependences点击加号, library--java然后找到F:\opencv2411\opencv\build\java下面的opencv-2411确定就OK了。(JAVA的配置就不说了,随意就能搜到)配置还是很简单的,比起c++开发环境来说简单到爆。
附上图像匹配程序
/**
* Created by wan on 2015/11/4.
*/
import org.opencv.core.*;
import org.opencv.highgui.Highgui;
import org.opencv.features2d.*;
public class Main {

public static void main(String[] args){
System.out.println("Welcome to OpenCV " + Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
/*Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("m = " + m.dump());*/
//读取图片
Mat m1 = Highgui.imread("D:\\lena1.png");
Mat m2 = Highgui.imread("D:\\lena2.png");
//特征点检测
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
MatOfKeyPoint keyPoint1 =new MatOfKeyPoint();
MatOfKeyPoint keyPoint2 =new MatOfKeyPoint();
long a = System.currentTimeMillis();
detector.detect(m1, keyPoint1);
detector.detect(m2, keyPoint2);
System.out.println("Time of detecting features: " + (System.currentTimeMillis() - a) + "ms");
System.out.println(keyPoint1.size() + " " + keyPoint2.size());
//特征点描述
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
Mat decriptors1 = new Mat();
Mat decriptors2 = new Mat();
long b = System.currentTimeMillis();
extractor.compute(m1, keyPoint1, decriptors1);
extractor.compute(m2, keyPoint2, decriptors2);
System.out.println("Time of extracting descriptors: " + (System.currentTimeMillis() - b) + "ms");
System.out.println(decriptors1.size() + " " + decriptors2.size());
//特征点匹配
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2);
MatOfDMatch matches = new MatOfDMatch();
long c = System.currentTimeMillis();
descriptorMatcher.match(decriptors1, decriptors2, matches);
System.out.println("Time of matching: " + (System.currentTimeMillis() - c) + "ms");
System.out.println(matches.size());
//输出匹配后图像
Mat mM = new Mat();
Features2d.drawMatches(m1, keyPoint1, m2, keyPoint2, matches, mM);
Highgui.imwrite("D:\\out.jpg", mM);
}
}
关键的是第二行的一句话,System.loadLibrary(Core.NATIVE_LIBRARY_NAME);一定要添加,不然不能运行。
java语言也比较简单,用起来很方便,不好的地方是图像匹配方法只支持傻瓜般的bruteforce或者flann方法,竟然文献中常用的MaxRatio方法都不提供,opencv这一点太让人失望了。不过总之,能匹配能计时,就够了。

0 0
原创粉丝点击