Qt OpenCV 学习(六):kmeans聚类算法实现背景替换

2023-12-14 17:30:18

1. mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>

#include <QString>
#include <QPixmap>
#include <QDebug>
#include <QImage>
#include <QFileDialog>

using namespace cv;
using namespace std;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void showImage(Mat &sendMat);

private slots:
    void on_btnOpn_clicked();

    void on_btnProcess_clicked();

private:
    Ui::MainWindow *ui;

    Mat srcImg;
};
#endif // MAINWINDOW_H

1. mainwindow.cpp

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

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

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

void MainWindow::showImage(Mat &sendMat) {
    Mat showImg = sendMat.clone();
    if (showImg.channels() == 1) {
        QImage showPic = QImage((const unsigned char*)(showImg.data), showImg.cols, showImg.rows, showImg.step, QImage::Format_Indexed8);
        ui->labelShow->setPixmap(QPixmap::fromImage(showPic.scaled(ui->labelShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    } else if (showImg.channels() == 3) {
        cvtColor(showImg, showImg, COLOR_BGR2RGB);
        QImage showPic = QImage((const unsigned char*)(showImg.data), showImg.cols, showImg.rows, showImg.step, QImage::Format_RGB888);
        ui->labelShow->setPixmap(QPixmap::fromImage(showPic.scaled(ui->labelShow->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    }
}

void MainWindow::on_btnOpn_clicked() {
    ui->lineEdit->clear();
    ui->labelShow->clear();
    QString filePath = QFileDialog::getOpenFileName(this, "open picture", ".", "picture (*.png *.jpg *.bmp)); All files (*.*)");
    ui->lineEdit->setText(filePath);

    srcImg = imread(filePath.toStdString());
    ui->labelShow->clear();
    showImage(srcImg);
}

void MainWindow::on_btnProcess_clicked() {
    Mat transfImg = srcImg.clone();
    Mat transfData = transfImg.reshape(3, transfImg.cols * transfImg.rows);

    transfData.convertTo(transfData, CV_32F);
    Mat bestLabels, centers;

    // 聚类的类别数,类别数越大则结果越接近原图像
    int K = 3;
    TermCriteria criteria = TermCriteria(TermCriteria::Type::COUNT + TermCriteria::Type::EPS, 10, 0.01);
    kmeans(transfData, K, bestLabels, criteria, 3, KMEANS_PP_CENTERS, centers);

    Mat kmeansImg = Mat::zeros(srcImg.size(), srcImg.type());
    Mat mask = Mat::zeros(srcImg.size(), CV_8UC1);

    // 获取左上角像素点的标签,作为背景像素点的标签
    int flag_label = bestLabels.at<int>(0, 0);
    for (int row = 0; row < transfImg.rows; row++) {
        for (int col = 0; col < transfImg.cols; col++) {
            int dataIndex = row * transfImg.cols + col;
            // 获取该像素点所属的类别标签
            int label = bestLabels.at<int>(dataIndex, 0);

            // 输出结果图中的每一个像素点,按照其所属的类别来改变其像素值
            if (label == flag_label) {
                mask.at<uchar>(row, col) = 255;
            }

            if (label == 0) {
                Vec3b bgr = {255, 0, 0};
                kmeansImg.at<Vec3b>(row, col) = bgr;
            } else if (label == 1) {
                Vec3b bgr = {0, 255, 0};
                kmeansImg.at<Vec3b>(row, col) = bgr;
            } else if (label == 2) {
                Vec3b bgr = {0, 0, 255};
                kmeansImg.at<Vec3b>(row, col) = bgr;
            }
        }
    }

    // 对图像进行闭操作:先膨胀后腐蚀,作用:消除 “闭合” 物体里面的孔洞
    // 开操作:先腐蚀后膨胀 作用:分离物体,消除小区域 特点:消除噪点,去除小的干扰块,而不影响原来的图像
    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
    morphologyEx(mask, mask, MORPH_CLOSE, kernel, Point(-1, -1), 1, 0);

    Mat dstImg;
    dstImg = srcImg.clone();
    for (int row = 0; row < transfImg.rows; row++) {
        for (int col = 0; row < transfImg.cols; col++) {
            int flag = mask.at<uchar>(row, col);
            if (flag == 255) {
                Vec3b bgr = {255, 255, 255};
                dstImg.at<Vec3b>(row, col) = bgr;
            }
        }
    }

    showImage(dstImg);
}

文章来源:https://blog.csdn.net/qq_42994487/article/details/134875884
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。