如何在 QtCreator 中链接 opencv 并使用 Qt 库

时间:2022-11-21
本文介绍了如何在 QtCreator 中链接 opencv 并使用 Qt 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这个问题必须重复多次,但它不起作用,有时仍然没有答案.信息来源主要是这些
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.youtube.com/watch?v=dgcXYQijV6c

This question must be duplicate many times, but it just doesn't work and sometimes it still remains unanswered. Sources of information are mainly these
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.youtube.com/watch?v=dgcXYQijV6c

这是我认为应该/可以做的总结.(现在它对我有用.)希望我从一开始就提到了所有内容,目的是编写一个非常清晰的教程.

This is the summation of what I think one should/can do. (And now it works for me.) Hopefully I mentioned everything from the very beginning, the aim is to write a very clear tutorial.

为 QtCreator 安装 OpenCV

  1. 我已经安装了 MS Visual Studio 2010 Professional.(我作为学生有免费许可证)-我认为这没有必要,只是提一下
  2. 下载:适用于 Windows 32 位的 Qt 5.0.1(MinGW 4.7,823 MB)
    2.1 安装: 警告,Qt 使用的所有内容(例如 OpenCV)必须位于名称中不包含空格的目录中.- 即程序文件"是错误的.(但我不希望不同的程序文件直接堆积在 C 上,所以我只创建了一个文件夹Programs",其中安装了所有重要的东西)
  3. 下载: cmake-2.8.10.2-win32-x86.exe - 为所有用户安装(这可以在程序文件中)
  4. 下载: OpenCV-2.4.0.exe,解压到:C:Programsopencv24 - 它将创建一个目录opencv";添加另一个文件夹opencv_bin".现在看起来像这样:
    C:Programsopencv24opencv*
    C:Programsopencv24opencv_bin
  5. 设置 PATH 环境变量,以便有一个到 MinGW 编译器的链接.例如C:ProgramsQtQt5.0.1ToolsMinGWin;
  6. 启动 cmake-gui.exe
    6.1 源码:设置OpenCV的默认目录;C:Programsopencv24opencv
    6.2 binaries:设置opencv_bin目录;C:Programscopencv24opencv_bin
    6.3 点击配置:
    • 选择MinGW MakefilesSpecify native compilers,点击下一步
    • 字段 C 用于 gcc.exe;C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/gcc.exe
    • Field C++ 用于 g++.exe;C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g++.exe
    • 字段fortran可以为空,点击完成
  1. I have already MS Visual Studio 2010 Professional installed. (I have a free licence as a student) - I think this is not necessary, just a mention
  2. Download: Qt 5.0.1 for Windows 32-bit (MinGW 4.7, 823 MB)
    2.1 Install: Warning, everything that Qt uses (e.g. OpenCV) must be in directories that don't contain white-spaces in their names. - i.e. "Program Files" is wrong. (But I don't want different program files to accumulate directly on C, so I've only made a folder "Programs" in which everything important is installed)
  3. Download: cmake-2.8.10.2-win32-x86.exe - Install for all users (this can be in Program Files)
  4. Download: OpenCV-2.4.0.exe, extract to: C:Programsopencv24 - it'll create a dir "opencv"; add another folder "opencv_bin". Now it looks like this:
    C:Programsopencv24opencv*
    C:Programsopencv24opencv_bin
  5. Set PATH environment variable, so that there be a link to MinGW compiler. e.g. C:ProgramsQtQt5.0.1ToolsMinGWin;
  6. Start cmake-gui.exe
    6.1 source code: set the default dir for OpenCV; C:Programsopencv24opencv
    6.2 binaries: set the opencv_bin dir; C:Programscopencv24opencv_bin
    6.3 click configure:
    • Choose MinGW Makefiles and Specify native compilers, click next
    • Field C is for gcc.exe; C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/gcc.exe
    • Field C++ is for g++.exe; C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g++.exe
    • Field fortran can be empty, click finish
  • WITH_QT - 必须被选中.
  • WITH_TBB、WITH_IPP、WITH_CUDA - 必须取消选择
  • CMAKE_BUILD_TYPE - 单击并输入文本调试"(不带引号).
  • 清除搜索字段中的文本.
  • WITH_QT - must be selected.
  • WITH_TBB, WITH_IPP, WITH_CUDA - must be unselected
  • CMAKE_BUILD_TYPE - click and enter a text "Debug" (without quotes).
  • Clear the text from the Search field.

现在我在 QtCreator 中创建了一个新的控制台应用程序.

Now I have created a new console app in QtCreator.

//cvHello.pro
QT       += core
QT       -= gui

TARGET = cvHello
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
INCLUDEPATH += C:/Programs/opencv24/opencv_bin2/install/include
LIBS += "C:/Programs/opencv24/opencv_bin2/bin/*.dll"

SOURCES += main.cpp
OTHER_FILES += 
    img.JPG

和主文件:

//main.cpp
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    cv::Mat mat;
    mat = cv::imread("img.JPG");
    cvNamedWindow("hello");
    cv::imshow("hello",mat);

    cvWaitKey(0);

    return 0;
}

推荐答案

我终于开始开心了.在调整这个问题时,我不得不尝试各种方法,如何定义LIBS.手动列出它们有帮助,起初我写错了.

Finally I am starting to be happy. When adjusting this question I had to try all ways, how to define LIBS. Listing them manually helped, at first I wrote them somehow wrongly.

最后是这样的:

LIBS += -LC:\Programs\opencv24\opencv_bin2\bin 
    libopencv_core240d 
    libopencv_highgui240d 
    libopencv_imgproc240d 
    libopencv_features2d240d 
    libopencv_calib3d240d 

顺便说一句,如果我犯了任何语法错误,我很抱歉我的英语.:)

Btw if I've made any grammar mistakes, I am sorry for my english. :)

这篇关于如何在 QtCreator 中链接 opencv 并使用 Qt 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何将 OpenCV cv::Mat 转换为 QImage 下一篇:非阻塞工作者 - 中断文件复制

相关文章

最新文章