赞
踩
该方式安装的版本可能比较旧。
#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 运行代码。
编辑task.json文件,在args参数下末尾添加
“-lopencv_core”,
“-lopencv_imgcodecs”,
“-lopencv_highgui”
官网下载指定版本的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)
下载对应版本并解压。历史版本
使用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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。