問題描述
如何在 VC++ 2010 Express 下安裝和使用 OpenCV 2.4.3?
How do you install and use OpenCV 2.4.3 under VC++ 2010 Express?
推薦答案
1.安裝 OpenCV 2.4.3
首先,從 sourceforge.net 獲取 OpenCV 2.4.3.它是自解壓的,因此只需雙擊即可開始安裝.將其安裝在一個目錄中,例如 C:
.
First, get OpenCV 2.4.3 from sourceforge.net. Its a self-extracting so just double click to start the installation. Install it in a directory, say C:
.
等到所有文件都被提取出來.它將創建一個新目錄 C:opencv
包含 OpenCV 頭文件、庫、代碼示例等.
Wait until all files get extracted. It will create a new directory C:opencv
which
contains OpenCV header files, libraries, code samples, etc.
現在您需要將目錄 C:opencvuildx86vc10in
添加到您的系統路徑中.此目錄包含運行代碼所需的 OpenCV DLL.
Now you need to add the directory C:opencvuildx86vc10in
to your system PATH. This directory contains OpenCV DLLs required for running your code.
打開控制面板 →系統 →高級系統設置 →高級 Tab →環境變量...
Open Control Panel → System → Advanced system settings → Advanced Tab → Environment variables...
在系統變量部分,選擇路徑 (1)、編輯 (2),然后輸入C:opencvuildx86vc10in;
(3),然后點擊確定.
On the System Variables section, select Path (1), Edit (2), and type C:opencvuildx86vc10in;
(3), then click Ok.
在某些計算機上,您可能需要重新啟動計算機,系統才能識別環境路徑變量.
On some computers, you may need to restart your computer for the system to recognize the environment path variables.
這將在您的計算機上完成 OpenCV 2.4.3 的安裝.
This will completes the OpenCV 2.4.3 installation on your computer.
2.創建一個新項目并設置 Visual C++
打開 Visual C++ 并選擇 文件 →新 →項目... →Visual C++ →空項目.為您的項目命名(例如:cvtest
)并設置項目位置(例如:c:projects
).
Open Visual C++ and select File → New → Project... → Visual C++ → Empty Project. Give a name for your project (e.g: cvtest
) and set the project location (e.g: c:projects
).
點擊確定.Visual C++ 將創建一個空項目.
Click Ok. Visual C++ will create an empty project.
確保在解決方案配置組合框中選擇了調試".右鍵單擊 cvtest
并選擇 屬性 →VC++ 目錄.
Make sure that "Debug" is selected in the solution configuration combobox. Right-click cvtest
and select Properties → VC++ Directories.
選擇包含目錄以添加新條目并鍵入C:opencvuildinclude
.
Select Include Directories to add a new entry and type C:opencvuildinclude
.
點擊確定關閉對話框.
返回屬性對話框,選擇庫目錄以添加新條目并鍵入C:opencvuildx86vc10lib
.
Back to the Property dialog, select Library Directories to add a new entry and type C:opencvuildx86vc10lib
.
點擊確定關閉對話框.
返回屬性對話框,選擇鏈接器 →輸入 →附加依賴項以添加新條目.在彈出對話框中,輸入以下文件:
Back to the property dialog, select Linker → Input → Additional Dependencies to add new entries. On the popup dialog, type the files below:
opencv_calib3d243d.lib
opencv_contrib243d.lib
opencv_core243d.lib
opencv_features2d243d.lib
opencv_flann243d.lib
opencv_gpu243d.lib
opencv_haartraining_engined.lib
opencv_highgui243d.lib
opencv_imgproc243d.lib
opencv_legacy243d.lib
opencv_ml243d.lib
opencv_nonfree243d.lib
opencv_objdetect243d.lib
opencv_photo243d.lib
opencv_stitching243d.lib
opencv_ts243d.lib
opencv_video243d.lib
opencv_videostab243d.lib
請注意,文件名以d"(代表調試")結尾.另請注意,如果您安裝了另一個版本的 OpenCV(比如 2.4.9),這些文件名將以 249d 而不是 243d 結尾(opencv_core249d.lib..etc).
Note that the filenames end with "d" (for "debug"). Also note that if you have installed another version of OpenCV (say 2.4.9) these filenames will end with 249d instead of 243d (opencv_core249d.lib..etc).
點擊確定關閉對話框.在項目屬性對話框中單擊確定以保存所有設置.
Click Ok to close the dialog. Click Ok on the project properties dialog to save all settings.
注意:
這些步驟將為調試"解決方案配置 Visual C++.對于發布"解決方案(可選),您需要重復添加 OpenCV 目錄和 Additional依賴項部分,使用:
These steps will configure Visual C++ for the "Debug" solution. For "Release" solution (optional), you need to repeat adding the OpenCV directories and in Additional Dependencies section, use:
opencv_core243.lib
opencv_imgproc243.lib
...
代替:
opencv_core243d.lib
opencv_imgproc243d.lib
...
您已經完成了 Visual C++ 的設置,現在是編寫真正代碼的時候了.右鍵單擊您的項目并選擇添加 →新項目... →Visual C++ →C++ 文件.
You've done setting up Visual C++, now is the time to write the real code. Right click your project and select Add → New Item... → Visual C++ → C++ File.
為您的文件命名(例如:loadimg.cpp
)并點擊確定.在編輯器中輸入以下代碼:
Name your file (e.g: loadimg.cpp
) and click Ok. Type the code below in the editor:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat im = imread("c:/full/path/to/lena.jpg");
if (im.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
imshow("Image", im);
waitKey(0);
}
上面的代碼將加載 c:fullpath olena.jpg
并顯示圖像.你可以使用您喜歡的任何圖像,只需確保圖像的路徑正確即可.
The code above will load c:fullpath olena.jpg
and display the image. You can
use any image you like, just make sure the path to the image is correct.
鍵入 F5 編譯代碼,它將在一個漂亮的窗口中顯示圖像.
Type F5 to compile the code, and it will display the image in a nice window.
這是你的第一個 OpenCV 程序!
And that is your first OpenCV program!
3.從這里去哪里?
既然您的 OpenCV 環境已準備就緒,接下來要做什么?
Now that your OpenCV environment is ready, what's next?
- 轉到示例目錄 →
c:opencvsamplescpp
. - 閱讀并編譯一些代碼.
- 編寫您自己的代碼.
這篇關于在 Visual C++ 2010 Express 中安裝 OpenCV 2.4.3的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!