linux下QT工程调用opencv、libtorch,并用cmake编译:

文章目录

  • 一、新建QT工程
  • 二、编写CMakeLists.txt文件
  • 三、各个文件的内容如下:
  • 1、mainwindow.ui的设置如下:
  • 2、mainwindow.cpp内容如下:
  • 3、mainwindow.h内容如下:
  • 4、main.cpp内容如下:
  • 5、用cmake编译,进入工程目录下执行命令:
  • 6、在执行make中出现一下问题:

一、新建QT工程

新建QT工程,因为需要用cmake编译代码,因此本工程就吧.pro和usrpro文件去掉了。
工程目录下新建build的文件夹,并新建CMakelists.txt文件。
工程下的目录结构如下:

--build文件夹
--CMakeLists.txt
--CMakeLists.txt.user
--main.cpp
--mainwindow.cpp
--mainwindow.h
--mainwindow.ui

二、编写CMakeLists.txt文件

我的CMakeLists.txt文件内容如下:

cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)project(helloworld)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")set(CMAKE_PREFIX_PATH /home/xxx/Qt5.9.5/5.9.5/gcc_64/lib/cmake/Qt5)set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)find_package(Qt5 COMPONENTS Widgets Gui Core REQUIRED)set(Torch_DIR /home/xxx/software/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
find_package(OpenCV  REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS} ${TORCH_INCLUDE_DIRS})add_executable(helloworld main.cpp mainwindow.cpp mainwindow.h mainwindow.ui)
target_link_libraries(helloworld Qt5::Core)
target_link_libraries(helloworld Qt5::Widgets)
target_link_libraries(helloworld Qt5::Gui)
target_link_libraries(helloworld  ${OpenCV_LIBS} ${TORCH_LIBRARIES}) set_property(TARGET helloworld PROPERTY CXX_STANDARD 14)

注意:请把以上涉及到绝对路径的设置,改为自己的文件路径。

三、各个文件的内容如下:

以下界面设计和代码实现,比较简单,只是为了测试 QT工程下调用opencv和libtorch,并用cmake编译。

1、mainwindow.ui的设置如下:

简单实现:输入R半径,点击count按钮,在eara处输出面积,并在viewLabel处一起显示图像。
界面简单设计。

2、mainwindow.cpp内容如下:


#include <torch/script.h>
#include <torch/torch.h>#include "mainwindow.h"
#include "ui_mainwindow.h"
// #undef errno
#include <iostream>
// #include "torch/script.h"
// #include "torch/torch.h"#include <complex>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include <opencv2/imgproc/types_c.h>
#include <vector>
#include <QImage>const static double PI=3.1416;
//using namespace cv;MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_countButton_clicked()
{bool ok;QString tempStr;QString valueStr=ui->radiusLineEdit->text();int valueInt=valueStr.toInt(&ok);double area=valueInt*valueInt*PI;/* 配置参数 */std::vector <float> mean_ = {0.485, 0.456, 0.406};std::vector <float> std_ = {0.229, 0.224, 0.225};char path[] = "1.jpg";// 读取图片cv::Mat image = cv::imread(path);if (image.empty())fprintf(stderr, "Can not load image\n");转换通道,cv::cvtColor(image, image, CV_BGR2RGB);cv::Mat img_float;image.convertTo(img_float, CV_32F, 1.0 / 255);// resize, 测试一个点数据cv::resize(img_float, img_float, cv::Size(256, 128));std::cout << img_float.at<cv::Vec3f>(256, 128)[1] << std::endl;// 转换成tensorauto img_tensor = torch::from_blob(img_float.data, {1, 3, 256, 128}, torch::kFloat32);//img_tensor = img_tensor.permute({0,3,1,2});// tensor标准化for (int i = 0; i < 3; i++) {img_tensor[0][0] = img_tensor[0][0].sub_(mean_[i]).div_(std_[i]);}//cv::imshow("1", image);// cv::waitKey(0);cv::Mat temp;QImage Qtemp;cvtColor(image, temp, CV_BGR2RGB);//BGR convert to RGBQtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);ui->viewLabel->setPixmap(QPixmap::fromImage(Qtemp));ui->viewLabel->resize(Qtemp.size());ui->viewLabel->show();ui->areaLabel_2->setText(tempStr.setNum(area));
}

3、mainwindow.h内容如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>namespace Ui {class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_countButton_clicked();private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H

4、main.cpp内容如下:

#include "mainwindow.h"
#include <QApplication>
#include <QLabel>int main(int argc, char *argv[])
{QApplication a(argc, argv);//QLabel label("hello world!");// label.show();MainWindow w;w.show();
//    Dialog w;
//    w.show();return a.exec();
}

5、用cmake编译,进入工程目录下执行命令:

cd build
cmake ..
make
./helloworld

如果正常的话会出现如下界面:

6、在执行make中出现一下问题:

问题一、

xxx@xxx-Precision-5820-Tower-X-Series:~/vsProjects/qtProjects/helloword/build$ make
Scanning dependencies of target helloworld_automoc
[ 20%] Automatic moc, uic and rcc for target helloworld
Generating ui_mainwindow.h
Generating moc_mainwindow.cpp
[ 20%] Built target helloworld_automoc
Scanning dependencies of target helloworld
[ 40%] Building CXX object CMakeFiles/helloworld.dir/main.cpp.o
[ 60%] Building CXX object CMakeFiles/helloworld.dir/mainwindow.cpp.o
In file included from /home/xxx/software/libtorch/include/ATen/core/ivalue.h:1138:0,from /home/xxx/software/libtorch/include/ATen/record_function.h:3,from /home/xxx/software/libtorch/include/ATen/Dispatch.h:5,from /home/xxx/software/libtorch/include/ATen/ATen.h:13,from /home/xxx/software/libtorch/include/torch/csrc/api/include/torch/types.h:3,from /home/xxx/software/libtorch/include/torch/script.h:3,from /home/xxx/vsProjects/qtProjects/helloword/mainwindow.cpp:20:
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h:647:36: error: expected unqualified-id before ‘)’ tokenconst std::vector<IValue>& slots() const {^
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h:647:36: error: expected ‘;’ at end of member declaration
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h:647:44: error: expected unqualified-id before ‘{’ tokenconst std::vector<IValue>& slots() const {^
In file included from /home/xxx/software/libtorch/include/c10/core/DeviceType.h:8:0,from /home/xxx/software/libtorch/include/c10/core/Device.h:3,from /home/xxx/software/libtorch/include/c10/core/Allocator.h:6,from /home/xxx/software/libtorch/include/ATen/ATen.h:7,from /home/xxx/software/libtorch/include/torch/csrc/api/include/torch/types.h:3,from /home/xxx/software/libtorch/include/torch/script.h:3,from /home/xxx/vsProjects/qtProjects/helloword/mainwindow.cpp:20:
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h: In member function ‘c10::intrusive_ptr<T> c10::IValue::toCustomClass() &&’:
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h:834:3: error: expected unqualified-id before ‘(’ tokenTORCH_CHECK(^
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h: In member function ‘c10::intrusive_ptr<T> c10::IValue::toCustomClass() const &’:
/home/xxx/software/libtorch/include/ATen/core/ivalue_inl.h:852:3: error: expected unqualified-id before ‘(’ tokenTORCH_CHECK(^
In file included from /home/xxx/software/libtorch/include/torch/csrc/jit/frontend/tracer.h:9:0,from /home/xxx/software/libtorch/include/torch/csrc/autograd/generated/variable_factories.h:12,from /home/xxx/software/libtorch/include/torch/csrc/api/include/torch/types.h:7,from /home/xxx/software/libtorch/include/torch/script.h:3,from /home/xxx/vsProjects/qtProjects/helloword/mainwindow.cpp:20:
/home/xxx/software/libtorch/include/torch/csrc/jit/api/object.h: In member function ‘size_t torch::jit::Object::num_slots() const’:
/home/xxx/software/libtorch/include/torch/csrc/jit/api/object.h:129:28: error: expected unqualified-id before ‘(’ tokenreturn _ivalue()->slots().size();^
CMakeFiles/helloworld.dir/build.make:86: recipe for target 'CMakeFiles/helloworld.dir/mainwindow.cpp.o' failed
make[2]: *** [CMakeFiles/helloworld.dir/mainwindow.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/helloworld.dir/all' failed
make[1]: *** [CMakeFiles/helloworld.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

网上相关的一些方法都尝试了,始终不行。后来参考此文档里面:
https://blog.csdn.net/qq_45662588/article/details/120541586
应该是头文件使用出现问题后经过尝试发现头文件的顺序不正确也会报错。
因此我把mainwindow.cpp下包含的libtorch的两个头文件放在所有头文件的最前面,再重新编译,成功通过。

#include <torch/script.h>
#include <torch/torch.h>#include "mainwindow.h"
#include "ui_mainwindow.h"
// #undef errno
#include <iostream>
// #include "torch/script.h"
// #include "torch/torch.h"#include <complex>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include <opencv2/imgproc/types_c.h>
#include <vector>
#include <QImage>

我原来是将#include "torch/script.h"和 #include "torch/torch.h"放在了中间位置,会报上面的错误,我将两个头文件放在最前面就没有报错了。

问题二:
之前还出现过:

Failed to compute shorthash for libnvrtc.so

参考https://blog.csdn.net/xzq1207105685/article/details/117400187的方法,
在CMakeList.txt开头添加:

find_package(PythonInterp REQUIRED)

添加后,再执行cmake …和make,就没有报错这个错误了。
后来又去掉了这句话,也没有报错了。有些奇怪,反正没有错误就好了。

问题三:
是之前我写的文章里提到的:

/helloword/build$ ./helloworld
./helloworld: /lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/cpe/anaconda3/lib/libpng16.so.16)

遇到的问题,已经在https://blog.csdn.net/shoukequ8359/article/details/121679024?spm=1001.2014.3001.5501给出了解决方法。即:下载 wget http://www.zlib.net/fossils/zlib-1.2.9.tar.gz,编译安装,并复制libz.so.1和libz.so.1.2.9到我的目录x86_64-linux-gnu下,即可解决问题。

linux下QT工程调用opencv、libtorch,并用cmake编译,及其遇到的一些问题的解决方法相关推荐

  1. linux下启动node服务出现events.js:167 throw er; // Unhandled ‘error‘ event 的解决方法

    linux下启动node服务出现events.js:167 throw er; // Unhandled 'error' event 的解决方法 pi@raspberrypi:~/ftp/files/ ...

  2. Linux下Qt/PyQt5无法调用fcitx中文输入法解决办法

    系统平台:Linux Mint 18.3 xfce 64bit (文末有福利) 博主相关文章: (1)PyQt5练习:结合matplotlib绘图 (2)PyQt5练习:词典翻译 (3)Linux下Q ...

  3. qt调用linux 进程,Linux 下qt 程序打包发布(使用linuxdelpoyqt ,shell 脚本)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u014746574/article/d ...

  4. Qt中调用OpenCV函数库时Crashed问题的解决。

    这几天想在虚拟机上搭建Ubuntu的开发环境,包括了Matlab和QT(C++)的开发工具安装等,同时由于做图像处理,所以还必须要安装FFMPEG和OpenCV库.下面就讲讲我们安装时出现qt中调用O ...

  5. Linux下 Qt界面程序嵌入另一个Qt界面程序_Qt应用嵌入外部进程窗口

    项目工程的实现,想要使用多个程序进行实现,在里面存在一定的调用的过程:调查的情况如下 Qt界面程序嵌入另一个Qt界面程序[Linux] Qt界面程序嵌入另一个Qt界面程序[Linux]_ptc321的 ...

  6. Linux下 QT中 log4cplus 最基本配置及使用

    Linux下 QT中 log4cplus 最基本配置及使用 鉴于网上很多使用方法不是很详细,对小白不友好,并且以此作为记录,方便下次配置. 本文是基于我自己下载的版本写的,请各位根据自己下载的版本更改 ...

  7. linux下qt静态编译_自由出土文物的空间_百度空间

    linux下qt静态编译_自由出土文物的空间_百度空间 linux下qt静态编译_自由出土文物的空间_百度空间 linux下qt静态编译 2012-04-09 13:10 测试通过,贴一下过程,仅用来 ...

  8. Linux下使用Java调用Hikvision设备网络SDK使用指南

    1 简介  由于在开发过程中,本来以为抓图项目会部署在Windows服务器上,但随着项目的进行发现项目需要部署在Linux系统,甚至是国产化平台银河麒麟上,但在部署时发现在国产化平台部署时出现缺包的问 ...

  9. Linux下qt实现个人资源管理功能:记事本,图片浏览,网络聊天,数据库管理

    Linux下qt实现个人资源管理功能:记事本,图片浏览,网络聊天,数据库管理 实验内容和环境:(包括软件平台和硬件平台) QT5.14.1 Ubuntu操作系统 实验内容及步骤 (含源程序): 用QT ...

最新文章

  1. java oracle数据库高效分页查询_oracle高效分页查询总结
  2. java导入包写在第二行的吗_使用Java命令行方式导入第三方jar包来运行Java程序的命令...
  3. [SDOI2018] 旧试题
  4. 基于数字移相信号发生器的频率相位差测量系统的FPGA实现
  5. 2015年第六届蓝桥杯 - 省赛 - C/C++大学A组 - G. 手链样式
  6. T-SQL笔记6:GO
  7. android--超级手电筒的开发记录
  8. 树莓派删除python2.7设置python3.5
  9. 企业级项目实战讲解!java的war包能直接改名么
  10. [笔记]java-package
  11. 如何反映两条曲线的拟合精度_中走丝线切割机床的加工精度用什么来衡量
  12. 各大媒体优劣对比_主流酱油选购对比
  13. 手把手教你用Python实现人脸识别,辨别真假!
  14. 用万用表测量NPN、PNP、NMOS、PMOS的好坏或分极
  15. 容器存储卷的介绍与使用
  16. 【小程序专栏】个人及企业资质该如何注册小程序?
  17. 蚁群算法简介及matlab源代码
  18. 厘米换算英尺英寸 (15 分)如果已知英制长度的英尺foot和英寸inch的值,那么对应的米是(foot+inch/12)×0.3048。现在,如果用户输入的是厘米数,那么对应英制长度的英尺和英寸是?
  19. 伦敦经济学院开设加密货币相关课程
  20. 什么是JTAG及JTAG接口简介

热门文章

  1. 新美大 java待遇_入我新美大的Java后台开发面试题总结
  2. 关于微信开发的语音存储问题
  3. 什么是次世代游戏建模?角色和场景建模,哪个比较容易
  4. 【解决方案】Error response from daemon: Conflict. The container name /mongo is already in use by contain
  5. JOptionPane和图标的用法
  6. Android调用系统应用打开各种类型文件
  7. 前端学习之路---node.js(二)
  8. 聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例
  9. Hive数据连接与函数(2)
  10. 多客技巧分享|【建议收藏】TikTok七大避坑指南帮你少走许多弯路