Configuring opencv with Qt Creator for Ubuntu

1. First, record how to make an interface program with Qt Creator and create a new project

  After the above selection

  Select the path, enter the project name, and click continue

  Select Desktop Qt 5.12.1 GCC 65bit option as follows, and click Details to expand and view

Continue to the following interface

You can refer to my previous blog to design the program interface

 pcharm configures pyqt5(Anaconda3 python environment) for interface development_ jiugeshao's column - CSDN blog

  Run the program, click the show button on the interface, and the text will appear in the text box

 

  2. Next, record the process of configuring Opencv

https://github.com/opencv/opencv   Download the opencv source code package from the website, and the version 3.4 is selected here

Select to download

  After downloading, unzip and create a build folder under the folder of CmakeLists.txt

mkdir build

 

Install the necessary environment libraries with the following command

apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

The command line goes to the build folder

cd build

Enter the following command to automatically generate a makefile through the cmakelist file

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

  After that, use the make command to read the corresponding instructions from the makefile, and then compile

make

 

If an error message appears during the process:

 failed to create symbolic link '../../lib/libopencv_core.so.3.4': operation not supported on socket

You can check whether your library is placed in the mount shared directory. If so, you can try to change it to a new location (bloggers have encountered it, just change a non mount folder, and bloggers use the virtual machine environment).

Then execute the following command to install the file generated by make into the corresponding directory of the system

make install

  After completion, the following configuration is required:

Enter the following command:

gedit /etc/ld.so.conf.d/opencv.conf

After opening, enter the following

Enter the following command to make the configuration path effective

sudo ldconfig

  Then perform bash configuration

sudo gedit /etc/bash.bashrc

Add at the end

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

After saving, execute the following commands to make the configuration effective

source /etc/bash.bashrc

Enter the following command to view the version of opencv

pkg-config opencv --modversion

 

  This completes the configuration.

3. In the first step, add the reference of opencv library to the configuration GUI program

Add the reference path of the header file and lib library to the. pro file

NCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_imgproc.so \
        /usr/local/lib/libopencv_imgcodecs.so

ui interface design is as follows:

 

The code in manwindow.h is as follows:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

void MainWindow::slot1()
{
   ui->textEdit->setText("hello world!");
   Mat img = imread("/home/icecreamshao/108.bmp");
   imshow("image", img);
   waitKey(0);

   Mat temp;
   cvtColor(img, temp, CV_BGR2RGB);
   QImage Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
   ui->label->setPixmap(QPixmap::fromImage(Qtemp));
   ui->label->resize(Qtemp.size());
   ui->label->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

  Run the program and the results are as follows:

 

 

 

 

 

 

 

Tags: OpenCV Qt Ubuntu

Posted on Fri, 01 Oct 2021 16:56:15 -0400 by sn202