【QT】【Opencv】在ubuntu中利用opencv 在QT的Mainwindow窗口显示图片

11 篇文章 2 订阅

环境 :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()
{

}

运行结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GLL_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值