1.OpenCV 下载及安装配置
最新版本:opencv3.0.0
注意:支持的visual studio2013
我们可以下载稳定版本:opencv2.4.11
安装:双击opencv-2.4.11解压到某一目录下即可
配置:在系统环境变量Path中,添加相应的路径。
32位添加:C:\opencv\opencv2.4.11\build\x86\vc10\bin
64位添加:C:\opencv\opencv2.4.11\build\x86\vc10\bin 和 C:\opencv\opencv2.4.11\build\x86\vc10\bin
2.VisualStudio 下 OpenCV调试
新建一个控制台程序
按照导航一步步完成项目创建
配置项目的include目录,指向opencv的安装目录,如下图所示
配置项目依赖的lib目录及依赖的库,如下图所示
环境配置好后,编写测试代码
#include "stdafx.h"#include#include#include#include#include
voidtrain_and_test_lda()
{string fn_csv = string("at.txt");//string fn_csv = string("feret.txt");
vectorallImages,train_images,test_images;
vectorallLabels,train_labels,test_labels;try{
read_csv(fn_csv, allImages, allLabels);
}catch (cv::Exception&e) {
cerr<
exit(1);
}if(allImages.size()<=1) {string error_message ="This demo needs at least 2 images to work. Please add more images to your data set!";
CV_Error(CV_StsError, error_message);
}for(int i=0 ; i
equalizeHist(allImages[i],allImages[i]);int photoNumber =allImages.size();for(int i=0 ; i
{if((i%g_photoNumberOfOnePerson)
{
train_images.push_back(allImages[i]);
train_labels.push_back(allLabels[i]);
}else{
test_images.push_back(allImages[i]);
test_labels.push_back(allLabels[i]);
}
}/*Ptr model = createEigenFaceRecognizer();//定义pca模型
model->train(train_images, train_labels);//训练pca模型,这里的model包含了所有特征值和特征向量,没有损失
model->save("eigenface.yml");//保存训练结果,供检测时使用*/Ptr fishermodel =createFisherFaceRecognizer();
fishermodel->train(train_images,train_labels);//用保存的降维后的图片来训练fishermodel,后面的内容与原始代码就没什么变化了
fishermodel->save("fisherlda.yml");int iCorrectPrediction = 0;intpredictedLabel;int testPhotoNumber =test_images.size();for(int i=0;i
{
predictedLabel= fishermodel->predict(test_images[i]);if(predictedLabel ==test_labels[i])
iCorrectPrediction++;
}string result_message = format("Test Number = %d / Actual Number = %d.", testPhotoNumber, iCorrectPrediction);
cout<< result_message <
cout<
}intmain() {
cout<
train_and_test_lda();return 0;
}
编译通过后,正常运行说明opencv环境配置正确。
3.Eclipse下OpenCV开发环境配置
在eclipse中,新建java project,如下图所示
新建成功后,修改项目的build path,增加外部jar,如下图所示
选择C:\opencv\opencv2.4.11\build\java\opencv-2411.jar
点开opencv-2411.jar,选择Native library location, 点击edit
32位系统指向:C:\opencv\opencv2.4.11\build\java\x86
64位系统指向:C:\opencv\opencv2.4.11\build\java\x64
编写测试代码:
importorg.opencv.core.Core;importorg.opencv.core.CvType;importorg.opencv.core.Mat;public classTestOpencv {public static voidmain( String[] args )
{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat mat= Mat.eye( 3, 3, CvType.CV_8UC1 );
System.out.println("mat = " +mat.dump() );
}
}
运行输出如下,即环境配置正确: