环境 :ubuntu16.04
QT+opencv的配置环境在之前的博客中有提到,此处不做解释
https://blog.csdn.net/rong11417/article/details/103905915
QT
opencv1.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-01-08T19:42:41
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opencv1
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv*
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QString>
#include <QImage>
#include <QPixmap>
#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel *label1=new QLabel(this);
this->setWindowTitle("this is opencv test");
this->setGeometry(0,0,500,500);
label1->setText("here");
label1->setGeometry(10,10,50,50);
label1->resize(400,400);
label1->move(20,20);
Mat srcImage = imread("img/girl2.jpg");
cvtColor(srcImage,srcImage,COLOR_BGR2RGB);
QImage disImage = QImage((const unsigned char *)(srcImage.data),srcImage.cols,srcImage.rows,QImage::Format_RGB888);
label1->setPixmap(QPixmap::fromImage(disImage.scaled(label1->size(),Qt::KeepAspectRatio)));
}
MainWindow::~MainWindow()
{
}
运行结果