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() );
}
}
運行輸出如下,即環境配置正確:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

