Linux下QT+OpenCV环境配置【完全解决版】
在Linux下配置QT和OpenCV的环境可以按照以下步骤进行:
- 安装Qt
sudo apt-update
sudo apt install qt5-default
- 安装Qt的OpenCV模块
sudo apt-get install qtdeclarative5-dev-tools
sudo apt-get install libqt5svg5-dev
- 安装OpenCV
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
git checkout 4.5.1
cd ~/opencv_build/opencv_contrib
git checkout 4.5.1
cd ~/opencv_build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules ..
make -j$(nproc)
sudo make install
sudo ldconfig
配置Qt项目文件以使用OpenCV
在Qt的项目文件 (.pro) 中,添加以下内容:
INCLUDEPATH += /usr/local/include/opencv4
LIBS += -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
验证安装
创建一个简单的Qt项目,并在其中使用OpenCV函数,例如读取并显示图像:
#include <QApplication>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
cv::Mat image = cv::imread("path_to_image.jpg");
if(image.empty()) return -1;
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
cv::imshow("Display window", image);
cv::waitKey(0);
return app.exec();
}
确保替换path_to_image.jpg
为实际图片路径。如果能够成功显示图片,则表示OpenCV和Qt的环境配置已成功。
评论已关闭