QT+QT creator+OpenCV图像灰度化

QT+QT creator+OpenCV图像灰度化

大家好,又见面了,我是全栈君。

1).pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2014-05-18T12:56:52
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myQTDemo
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui
INCLUDEPATH+=d:\opencv249\include\opencv\
                    d:\opencv249\include\opencv2\
                    d:\opencv249\include

LIBS+=d:\opencv249\lib\libopencv_calib3d249.dll.a\
        d:\opencv249\lib\libopencv_contrib249.dll.a\
        d:\opencv249\lib\libopencv_core249.dll.a\
        d:\opencv249\lib\libopencv_features2d249.dll.a\
        d:\opencv249\lib\libopencv_flann249.dll.a\
        d:\opencv249\lib\libopencv_gpu249.dll.a\
        d:\opencv249\lib\libopencv_highgui249.dll.a\
        d:\opencv249\lib\libopencv_imgproc249.dll.a\
        d:\opencv249\lib\libopencv_legacy249.dll.a\
        d:\opencv249\lib\libopencv_ml249.dll.a\
        d:\opencv249\lib\libopencv_objdetect249.dll.a\
        d:\opencv249\lib\libopencv_video249.dll.a

2).h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    cv::Mat image,image2;
      QImage img;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

3).cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <opencv/cv.h>
#include <QTextCodec>
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),"",tr("Image File(*.bmp *.jpg *.jpeg *.png)"));
    QTextCodec *code = QTextCodec::codecForName("gb18030");
    std::string name = code->fromUnicode(filename).data();
    image = cv::imread(name);

    if(!image.data)
    {
        QMessageBox msgBox;
        msgBox.setText(tr("image data is null"));
        msgBox.exec();
    }
    else
    {
        cv::cvtColor(image,image2,CV_BGR2RGB);
        img = QImage((const unsigned char*)(image2.data),image.cols,image.rows, image.step,  QImage::Format_RGB888);
        ui->label->clear();
        ui->label->setPixmap(QPixmap::fromImage(img));
        //ui->processPushButton->setEnabled(true);
        ui->label->resize(ui->label->pixmap()->size());
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    // cv::flip(image,image,1);
    Mat gray;

    cvtColor(image,gray,CV_BGR2GRAY);
    // cvtColor(gray,gray,CV_bg)
  //  imshow("li",gray);

    // cv::cvtColor(image,image,CV_BGR2RGB);
    //      img = QImage((const unsigned char*)(image.data),image.cols,image.rows,image.cols*image.channels(),  QImage::Format_RGB888);
    img = QImage((const unsigned char*)(gray.data),gray.cols,gray.rows,gray.step,  QImage::Format_Indexed8);

    ui->label->setPixmap(QPixmap::fromImage(img));
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115569.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号