我正在尝试构建一个以 libtorch 和 opencv 作为依赖项的项目。我使用 cmake 作为我的构建系统,因为这两个库都推荐使用它。我目前陷入困境,我正在尝试使用 libtorch 和 opencv 编译一个最小的程序。
我的程序看起来像这样
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
void showImage(cv::Mat);
at::Tensor imgToTensor(std::string img_path);
using namespace cv;
using std::cout;
using std::endl;
int main() {
std::string img_path = "./images/01 HEAVENLY STAR080.png";
auto tensor = imgToTensor(img_path);
cout << tensor << endl;
}
at::Tensor imgToTensor(std::string img_path){
Mat origImage;
Mat normalizedImage;
Mat sizedImage(500, 200, CV_32FC3);
origImage = imread(img_path, 1);
origImage.convertTo(normalizedImage, CV_32FC3);
resize(normalizedImage, sizedImage, sizedImage.size(), 0, 0, INTER_LINEAR);
auto input = torch::from_blob(sizedImage.data, {sizedImage.rows, sizedImage.cols, 3});
return input;
} …Run Code Online (Sandbox Code Playgroud)