openCV结合QTcreater实现图像的灰度化和二值化处理并输出

前奏如图所示。
在这里插入图片描述自行定义
在这里插入图片描述在这里插入图片描述在这里插入图片描述
环境配置不懂的可以参看之前的文章。
传送门

传送门传送门传送门

环境配置不懂的可以。有案例
https://blog.csdn.net/dongbao520/article/details/124612542

话不多说。结果展示
在这里插入图片描述

代码直接上。
对于头文件,配置如下。

头文件mainwindow.h

mainwindow.h
代码如下

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

//ctrl+i快捷键自动对齐==声明函数
void originalpicture(Mat &image);
void grayShow(Mat &image);
void Threshold2(Mat &gray);

//原始图像
void originalpicture(Mat &image)
{
    Mat img;
    img=image;
    namedWindow( "Origin window");
    imshow("Origin window", img);
}

//处理为灰度图像
void grayShow(Mat &image)
{
    Mat gray;
    cvtColor(image, gray, COLOR_RGBA2GRAY);
    namedWindow("gray picture");
    imshow("gray picture", gray);
    //    namedWindow( "Display window", WINDOW_AUTOSIZE );
    Threshold2(gray);
    //logTransfer(gray);
    //gammaTranfer(gray);
}

//二值化变换
void Threshold2(Mat &gray)
{
    Mat result;
    threshold(gray, result, 120,255,THRESH_BINARY);
    namedWindow("binary picture");
    imshow("binary picture", result);
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
};
#endif // MAINWINDOW_H

关键的文件注意。

这里直接默认了,什么都没有,不需要

mainwindow.cpp文件

mainwindow.cpp文件

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}


MainWindow::~MainWindow()
{
}



工程pro文件

xc00256.pro文件

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp

HEADERS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

INCLUDEPATH += D:\applearn\Opencv-VC\opencv\newbuild-me\install\include
LIBS += D:\applearn\Opencv-VC\opencv\newbuild-me\lib\libopencv_*.a


在这里插入图片描述

主程序main

main.cpp文件

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

//    MainWindow w;
//    w.show();

    Mat img=imread("C:/Users/57281/Pictures/002.jpg");

//    namedWindow( "Display window", WINDOW_AUTOSIZE );
//    imshow("Display window", img);

    originalpicture(img);
    grayShow(img);

    return a.exec();
}

最终结果如图
在这里插入图片描述在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海宝7号

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

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

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

打赏作者

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

抵扣说明:

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

余额充值