1.OpenCV 下载及安装配置
opencv的下载地址: http://opencv.org/downloads.html
最新版本: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 <opencv2/opencv.hpp> #include <iostream> #include <fstream> #include <sstream> #include <math.h> void train_and_test_lda() { string fn_csv = string ( " at.txt " ); // string fn_csv = string("feret.txt"); vector<Mat> allImages,train_images,test_images; vector < int > allLabels,train_labels,test_labels; try { read_csv(fn_csv, allImages, allLabels); } catch (cv::Exception& e) { cerr << " Error opening file " << fn_csv << " . Reason: " << e.msg << endl; // 文件有问题,我们啥也做不了了,退出了 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<allImages.size() ; i++ ) equalizeHist(allImages[i],allImages[i]); int photoNumber = allImages.size(); for ( int i= 0 ; i<photoNumber ; i++ ) { if ((i%g_photoNumberOfOnePerson)< g_howManyPhotoForTraining) { 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<FaceRecognizer> model = createEigenFaceRecognizer();//定义pca模型 model->train(train_images, train_labels);//训练pca模型,这里的model包含了所有特征值和特征向量,没有损失 model->save("eigenface.yml");//保存训练结果,供检测时使用 */ Ptr <FaceRecognizer> fishermodel = createFisherFaceRecognizer(); fishermodel ->train(train_images,train_labels); // 用保存的降维后的图片来训练fishermodel,后面的内容与原始代码就没什么变化了 fishermodel->save( " fisherlda.yml " ); int iCorrectPrediction = 0 ; int predictedLabel; int testPhotoNumber = test_images.size(); for ( int i= 0 ;i<testPhotoNumber;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 << endl; cout << " accuracy = " << float (iCorrectPrediction)/testPhotoNumber<< endl; } int main() { cout << " lda = " << endl; 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
编写测试代码:
import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; public class TestOpencv { public static void main( String[] args ) { System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat mat = Mat.eye( 3, 3 , CvType.CV_8UC1 ); System.out.println( "mat = " + mat.dump() ); } }
运行输出如下,即环境配置正确: