本文主要是介绍Ubuntu下vscode配置OpenCV以及Libtorch,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
opencv安装
- sudo apt-get update
- sudo apt-get install libopencv-dev
该方式安装的版本可能比较旧。
测试代码
#include <opencv2/opencv.hpp>#include <iostream>int main() {cv::Mat image = cv::imread("t.png");cv::imshow("Image", image);cv::waitKey(0);return 0;}
Ctrl + F5,然后选择g++ build active file 运行代码。
出现对‘cv::imread(cv::String const&, int)’未定义的引用
编辑task.json文件,在args参数下末尾添加
“-lopencv_core”,
“-lopencv_imgcodecs”,
“-lopencv_highgui”
手动安装指定版本opencv
-
官网下载指定版本的source代码,并解压到本地。
-
解压后执行make命令
mkdir build cd build cmake .. make -j8 sudo make install
-
/etc/ld.so.conf.d/路径下创建任意一个.conf文件,把lib文件的路径写在里面,一般是 /usr/local/lib,然后执行 sudo ldconfig即可。(否则会报错:error while loading shared libraries: libopencv_core.so.3.4: cannot open shared object file)
Libtorch配置
-
下载对应版本并解压。历史版本
-
使用cmake进行构建。cmake内容如下:
cmake_minimum_required(VERSION 2.6)project(libtorch_t)set(Torch_DIR /home/yang/Documents/libtorch/share/cmake/Torch) #你解压的libtorch的绝对路径 find_package(Torch REQUIRED)set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") #set(CUDA_INCLUDE_DIRS "/usr/local/cuda/include")#main.cpp exe add_executable(libtorch_t libtorch_t.cpp)#link libtorch .a .so target_link_libraries(libtorch_t "${TORCH_LIBRARIES}") target_include_directories(libtorch_t PRIVATE CUDA_INCLUDE_DIRS)set_property(TARGET libtorch_t PROPERTY CXX_STANDARD 14)
-
编写libtorch_t测试代码
#include <torch/script.h> #include <iostream>int main() {torch::Tensor tensor = torch::rand({2, 3});std::cout << tensor << std::endl; }
-
运行run.sh 其内容如下:
cmake .
make
./libtorch_t
这篇关于Ubuntu下vscode配置OpenCV以及Libtorch的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!