1.1

[图文教程]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;
}

四:运行试试

posted @   asml  阅读(2603)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2017-08-13 docker应用笔记
@.@
点击右上角即可分享
微信分享提示