# 序言
因为之前 C++ 代码一直是在 ubuntu 的 CLion 上写的,对 Visual Studio 的相关配置并不是很熟悉,最近需要在 win 上开发,所以不得不用 Visual Studio 来编写代码,在做项目的同时,简单记录下 Visual Studio 的相关配置,以调用 ncnn 和 opencv 为例。
在下面的配置中,经本人尝试,ncnn 环境只能在 release 下运行,不能再 debug 环境下运行,即使改为 opencv_world460d.lib
# 一、准备 ncnn 和 opencv
# 1.1 windows 下编译 ncnn
首先需要安装 Visual Studio 2022 社区版,这个不用多说了吧,安装结束后打开 vs2022 的 x64 命令行终端
在 windows 下编译 ncnn 前需要先下载编译 protobuf-3.4.0,下载后解压,编译命令过程如下:
cd <protobuf-root-dir> | |
mkdir build-2022 | |
cd build-2022 | |
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake | |
nmake | |
nmake install |
编译完后再编译 ncnn,编译命令过程如下:
git clone https://github.com/Tencent/ncnn.git | |
cd ncnn | |
git submodule update --init # 如果这一步一直更新不了的话,需要把 cmake 编译时候的 - DNCNN_VULKAN 设置为 OFF,不然编译不通过 | |
mkdir build && cd build | |
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=<protobuf-root-dir>/build-2022/install/include -DProtobuf_LIBRARIES=<protobuf-root-dir>/build-2022/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=<protobuf-root-dir>/build-2022/install/bin/protoc.exe -DNCNN_VULKAN=OFF .. | |
nmake | |
nmake install |
protobuf-root-dir 是刚才编译的 protobuf 的目录,根据自己的来修改。
# 1.2 安装 opencv
opencv 的安装比较简单,去 opencv 官网下载 windows 的文件下来安装即可,会得到一个 opencv 的文件夹
# 二、项目配置
打开 vs2022,新建一个空项目:
得到空项目结构如下:
然后依次点击视图 => 其他窗口 => 属性管理器,在 Release | x64(与上面编译过程中的参数对应)处右击进入属性界面。点击 VC++ 目录,在包含目录中依次添加如下内容:
<opencv-root-dir>/build/include | |
<opencv-root-dir>/build/include/opencv | |
<opencv-root-dir>/build/include/opencv2 | |
<ncnn-root-dir>/build/install/include/ncnn | |
<protobuf-root-dir>/build/install/include | |
我的配置 | |
D:/software/opencv/build/include | |
D:/software/opencv/build/include/opencv2 | |
D:/Projects/ncnn/build/install/include/ncnn | |
D:/Projects/protobuf/protobuf-3.4.0/build-2022/install/include |
在库目录中依次添加如下内容:
<opencv-root-dir>/build/x64/vc15/lib | |
<ncnn-root-dir>/build/install/lib | |
<protobuf-root-dir>/build/install/lib | |
我的配置 | |
D:/software/opencv/build/x64/vc15/lib | |
D:/Projects/ncnn/build/install/lib | |
D:/Projects/protobuf/protobuf-3.4.0/build-2022/install/lib |
然后在属性界面选择链接器 => 输入,在附加依赖项中依次添加如下内容:
ncnn.lib | |
libprotobuf.lib | |
opencv_world455.lib | |
我的配置 | |
ncnn.lib | |
libprotobuf.lib | |
opencv_world460.lib | |
opencv_world460d.lib |
注意:网上很多教程没有这一步,没配置的话可能会报那种找不到 dll 文件的错误,是因为没有将 opencv 里面的相关文件复制到 C 盘中的文件夹里面
操作方法:将 opencv/build/x64/vc15/bin 目录下面的 opencv_world455.dll 和 opencv_world455d.dll 文件复制到 C:\Windows\System32 这个文件夹里面(详细看图)
拷贝到 C:\Windows\System32 中:
如果是 opencv 其他的版本,把对应的 dll 文件移动到上述两个 C 盘文件夹即可!
至此,项目的配置环境完成。在 VS2022 的运行窗口栏处依次选择 Release 和 x64,与上面的选择对应。
然后就可以编写代码了,这里以 DBNet 文字检测模型代码运行为示例,创建了两个头文件和三个源文件结构如下:
demo.cpp 为主程序入口,代码示例如下:
// | |
// Created by cai on 2022/2/13. | |
// | |
#include "include/DbNet.h" | |
#include <iostream> | |
using namespace cv; | |
using namespace std; | |
int main(){ | |
DbNet dbNet; // 初始化对象 | |
bool retDbNet = dbNet.initModel("D:\\project_code\\OcrDet_ncnn\\model\\det_int8"); // 模型初始化 | |
if (!retDbNet){ | |
printf("DBNet load model fail!"); | |
} | |
const char*imagepath = "D:\\project_code\\OcrDet_ncnn\\test_img"; | |
vector<String> imagesPath; | |
cv::glob(imagepath,imagesPath); | |
for (int i =0;i<imagesPath.size();i++) { | |
// 载入图像 | |
cout << imagesPath[i] << endl; | |
Mat image = imread(imagesPath[i]); | |
if (image.empty()) { | |
cout << "Error: Could not load image" << endl; | |
return -1; | |
} | |
vector<Rect> rect; | |
rect = dbNet.getTextImages(image); // 检测接口 | |
for(auto &xywh : rect){ | |
cv::rectangle(image, xywh, Scalar(0, 0, 255),1, LINE_8,0); // 画矩形框 | |
} | |
cv::imshow("img0",image); | |
cv::waitKey(); | |
} | |
return 0; | |
} |
这里用的 DBNet 的模型经过量化后不到 600k,直接运行代码后得到检测输出结果如下,检测图和掩码图效果都还不错: