[圖文教程]VS2017搭建opencv & C++ 開發環境


首先從官網下載OpenCV最新版本

截至我寫這文章,4.0已經發布預覽版了,不過在這是沒有的,只能用3.4.2:
https://opencv.org/releases.html

一:安裝

安裝過程不做細解,照着提示做即可

安裝完成,找到這個目錄

添加到環境變量Path

二:創建工程

1. 新建個C++工程

打開VS2017新建一個C++工程

2. 配置

有些屬性需要配置

3. 添加文件

添加一個文件用於寫代碼

4. 包含OpenCV的include和lib

打開項目屬性,到x64這里選"編輯"

添加這3個目錄(請在之前安裝的opencv目錄里找)

庫目錄也添加一下

連接器也要

最后添加lib文件,注意這里要區分debug和release
添加的文件一個文件名是帶d一個是不帶的

三:編輯代碼驗證

#include <iostream>
#include <string> 
#include <Windows.h>
#include  <direct.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

/*
* Comments:獲取程序運行路徑
* Param bool contain_filename :是否包含文件名
* @Return string :程序運行路徑
*/
string GetProgramDir(bool contain_filename)
{
	char exeFullPath[MAX_PATH]; // Full path
	string strPath = "";
	GetModuleFileNameA(NULL, exeFullPath, MAX_PATH);
	strPath = (string)exeFullPath;    // Get full path of the file
	int pos = strPath.length();
	if (!contain_filename)pos = strPath.find_last_of('\\', strPath.length());
	return strPath.substr(0, pos);  // Return the directory without the file name
}

int main()
{
	string runpath = GetProgramDir(false);
	Mat img = imread(runpath + "/1.png");//加載圖片
	if (img.empty())
	{
		printf("讀取圖片失敗!\n");
		system("pause");
		return 0;
	}
	imshow("", img);//顯示出來
	waitKey(0);//等待用戶按鍵繼續
	img.release();//釋放資源
	return 0;
}

四:運行試試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM