OpenCv的Java,C++开发环境配置

时间:2023-01-13 08:43:32

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调试

新建一个控制台程序

OpenCv的Java,C++开发环境配置

按照导航一步步完成项目创建

配置项目的include目录,指向opencv的安装目录,如下图所示

OpenCv的Java,C++开发环境配置

配置项目依赖的lib目录及依赖的库,如下图所示

OpenCv的Java,C++开发环境配置

OpenCv的Java,C++开发环境配置

环境配置好后,编写测试代码

#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();
}
if(allImages.size()<=) {
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= ; i<allImages.size() ; i++)
equalizeHist(allImages[i],allImages[i]); int photoNumber = allImages.size();
for(int i= ; 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 = ;
int predictedLabel;
int testPhotoNumber = test_images.size();
for(int i=;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 ;
}

编译通过后,正常运行说明opencv环境配置正确。

3.Eclipse下OpenCV开发环境配置

在eclipse中,新建java project,如下图所示

OpenCv的Java,C++开发环境配置

新建成功后,修改项目的build path,增加外部jar,如下图所示

OpenCv的Java,C++开发环境配置

选择C:\opencv\opencv2.4.11\build\java\opencv-2411.jar

OpenCv的Java,C++开发环境配置

点开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() );
}
}

运行输出如下,即环境配置正确:

OpenCv的Java,C++开发环境配置