[C++] VS configuration OpenCV/Libtorch environment

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.
insert image description here

2. In the view, bring up the property manager.

insert image description here

3. In the property manager, select the property of Debug|x64.

insert image description here

4. Add the OpenCV path in the include directory, here I am E:\C_Libiary\opencv3.41\build\includeandE:\C_Libiary\opencv3.41\build\include\opencv2

insert image description here

insert image description here

5. Add library directoryE:\C_Libiary\opencv3.41\build\x64\vc15\lib

insert image description here

6. Additional dependencies addedopencv_world341d.lib

insert image description here

7. Add environment variablesE:\C_Libiary\opencv3.41\build\x64\vc15\bin

insert image description here

8. Copy the three dll files in the bin directory to C:\Windows\System32the path

insert image description here

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;
}

insert image description here

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\includeandE:\C_Libiary\libtorch\include

insert image description here

2. Add the library directory, the path isE:\C_Libiary\libtorch\lib

insert image description here

3. Add the following content in the dependencies (some lib files under the Libtorch folder, different versions of Libtorch will be slightly different)

insert image description here

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%

insert image description here

5. Copy all dll files to C:\Windows\System32the path
insert image description here
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;
}

insert image description here

If it runs successfully, the configuration is successful.

Guess you like

Origin blog.csdn.net/qq1198768105/article/details/132557637