【QT】【OpenCV】利用QT+Opencv图像处理 灰度图像和位运算

操作系统:ubuntu16.04

opencv2.pro

#-------------------------------------------------
#
# Project created by QtCreator 2020-01-08T23:47:22
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencv2
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

FORMS += \
        mainwindow.ui
INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2


LIBS += /usr/local/lib/libopencv*  \
        /home/trust100/Downloads/opencv-3.4.1/build/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;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp 

#include "mainwindow.h"
#include "ui_mainwindow.h"

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



    Mat srcImage = imread("img2/6.jpg");
    cvtColor(srcImage,srcImage,COLOR_BGR2RGB);
    QImage disImage = QImage((const unsigned char *)(srcImage.data),srcImage.cols,srcImage.rows,QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(disImage.scaled(ui->label->size(),Qt::KeepAspectRatio)));
    Mat fp;
    srcImage.convertTo(fp,CV_32F);
    QImage disImage2 = QImage((const unsigned char *)(fp.data),fp.cols,fp.rows,QImage::Format_RGB888);
    ui->label_2->setPixmap(QPixmap::fromImage(disImage2.scaled(ui->label_2->size(),Qt::KeepAspectRatio)));

    Mat mask(srcImage.rows,srcImage.cols,CV_8UC3,Scalar(0,0,0));
    circle(mask,Point(srcImage.rows / 2,srcImage.cols / 2-35),220,Scalar(255,255,255),-1);
    QImage disImage3 = QImage((const unsigned char *)(mask.data),mask.cols,mask.rows,QImage::Format_RGB888);
    ui->label_3->setPixmap(QPixmap::fromImage(disImage3.scaled(ui->label_3->size(),Qt::KeepAspectRatio)));

    Mat r;
    bitwise_and(srcImage,mask,r);
    QImage disImage4 = QImage((const unsigned char *)(r.data),r.cols,r.rows,QImage::Format_RGB888);
    ui->label_4->setPixmap(QPixmap::fromImage(disImage4.scaled(ui->label_4->size(),Qt::KeepAspectRatio)));
}

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

mainwindow.ui

运行结果

发布了139 篇原创文章 · 获赞 24 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/rong11417/article/details/103910267