foreword
This article is a note from the video https://www.bilibili.com/video/BV1dp4y177L4 .
OpenCV and Libtorch installation package: https://pan.baidu.com/s/1i3DqTcHFSC1rRDsIgYGCsQ?pwd=8888
VS version: 2019
Opencv version: 3.4.1
Libtorch version: 2.0.1+cu117
Configure the OpenCV environment
1. Open VS and create a console application.
2. In the view, bring up the property manager.
3. In the property manager, select the property of Debug|x64.
4. Add the OpenCV path in the include directory, here I am E:\C_Libiary\opencv3.41\build\include
andE:\C_Libiary\opencv3.41\build\include\opencv2
5. Add library directoryE:\C_Libiary\opencv3.41\build\x64\vc15\lib
6. Additional dependencies addedopencv_world341d.lib
7. Add environment variablesE:\C_Libiary\opencv3.41\build\x64\vc15\bin
8. Copy the three dll files in the bin directory to C:\Windows\System32
the path
9. Place a picture under the cpp file and call the following code for testing
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("1.png"); /*图片 */
imshow("input", src);
waitKey(0);
return 0;
}
Successfully opened, indicating that the OpenCV environment configuration is successful.
Configure the Libtorch environment
1. Add the path of Libtorch in the include directory, here I am E:\C_Libiary\libtorch\include\torch\csrc\api\include
andE:\C_Libiary\libtorch\include
2. Add the library directory, the path isE:\C_Libiary\libtorch\lib
3. Add the following content in the dependencies (some lib files under the Libtorch folder, different versions of Libtorch will be slightly different)
asmjit.lib
c10.lib
c10_cuda.lib
caffe2_nvrtc.lib
clog.lib
cpuinfo.lib
dnnl.lib
fbgemm.lib
fbjni.lib
kineto.lib
libprotobuf.lib
libprotobuf-lite.lib
libprotoc.lib
nvfuser_codegen.lib
pthreadpool.lib
pytorch_jni.lib
torch.lib
torch_cpu.lib
torch_cuda.lib
XNNPACK.lib
4, add environmentPATH=E:\C_Libiary\libtorch\lib;%PATH%
5. Copy all dll files to C:\Windows\System32
the path
6. Enter the following code to test
#include<torch/torch.h>
#include<torch/script.h>
using namespace torch;
using namespace std;
int main() {
Tensor tensor = torch::rand({
1,2,3 });
cout << tensor.sizes() << endl; //方式一,只打印维度信息
tensor.print(); //方式二,除了打印维度信息,数据类型也打印出来
return 0;
}
If it runs successfully, the configuration is successful.