Home > Backend Development > C++ > How to Successfully Integrate OpenCV and Qt in Qt Creator?

How to Successfully Integrate OpenCV and Qt in Qt Creator?

Mary-Kate Olsen
Release: 2024-11-30 22:02:13
Original
773 people have browsed it

How to Successfully Integrate OpenCV and Qt in Qt Creator?

Integrating OpenCV and Qt in QtCreator

Introduction

Linking OpenCV and using the Qt library in QtCreator can be a challenging task. Various tutorials and resources exist, but finding a comprehensive solution can be difficult. This article aims to provide a clear and thorough guide to accomplish this integration.

Installation and Setup

  1. Install Qt 5.0.1 for Windows 32-bit (MinGW 4.7) with the following requirement: Avoid directories with white-spaces in their names. Consider creating a folder named "Programs" for all important program installations.
  2. Download and install Cmake-2.8.10.2-win32-x86.exe for all users.
  3. Download OpenCV-2.4.0.exe and extract to C:Programsopencv24, creating two subdirectories named "opencv" and "opencv_bin."
  4. Configure PATH environment variable to include the MinGW compiler: C:ProgramsQtQt5.0.1ToolsMinGWbin;

Creating OpenCV Binaries

  1. Launch cmake-gui.exe and configure the source code directory to C:Programsopencv24opencv and the binaries directory to C:Programscopencv24opencv_bin.
  2. Specify native compilers for C and C using the paths: C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/gcc.exe and C:/Programs/Qt/Qt5.0.1/Tools/MinGW/bin/g .exe.
  3. Select WITH_QT and unselect WITH_TBB, WITH_IPP, and WITH_CUDA.
  4. Set CMAKE_BUILD_TYPE to "Debug."
  5. Click "Configure" until all red lines disappear.
  6. Click "Generate" and close cmake-gui.exe.
  7. Open a command prompt, navigate to the opencv_bin directory, and execute "mingw32-make" and "mingw32-make install."

QtCreator Project Configuration

  1. Create a new QtCreator Console application.
  2. Configure the .pro file as follows:
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
Copy after login
  1. Define the main.cpp file as follows:
#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;
}
Copy after login
  1. Ensure that the path to QtCreator/bin is included in the PATH variable.

Alternative Library Linking

Instead of using "*.dll," you can manually list the necessary libraries in the LIBS variable:

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

This alternative method can also resolve any issues when listing the DLLs manually.

The above is the detailed content of How to Successfully Integrate OpenCV and Qt in Qt Creator?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template