基于Qt,OpenCV去图像背景,去图像黑边软件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35759050/article/details/54237469

开发环境:Qt5.5.1  msvc2012  opencv2.4.9

开发此软件目的:从导师那淘汰一台旧的相机式扫描仪,相机照出来的图片效果不佳,有暗黄色背景,有黑边,软件作用就是除去这些噪声。

软件UI:

软件处理效果:

源图像:


处理后图像:


源代码:

mainwindow.cpp:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //禁止窗口设置大小
    setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(), this->height());
    //this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());//去掉窗口标题栏方式

    ui->pushButton->setIcon(QIcon(":/icon/folder.png"));  //打开图片按键
    ui->pushButton->setIconSize(QSize(37,37));
    ui->pushButton->setStyleSheet("QPushButton{border-radius: 10px;}"
                                    "QPushButton:hover{background-color:white; color: black;}"
                                    "QPushButton:pressed{background-color:rgb(85, 170, 255);}");

    ui->pushButton_2->setIcon(QIcon(":/icon/refresh.png"));  //转换图片按键
    ui->pushButton_2->setIconSize(QSize(40,40));
    ui->pushButton_2->setStyleSheet("QPushButton{border-radius: 10px;}"
                                    "QPushButton:hover{background-color:white; color: black;}"
                                    "QPushButton:pressed{background-color:rgb(85, 170, 255);}");

    ui->pushButton_3->setIcon(QIcon(":/icon/save.png"));  //另存为按键
    ui->pushButton_3->setIconSize(QSize(37,37));
    ui->pushButton_3->setStyleSheet("QPushButton{border-radius: 10px;}"
                                    "QPushButton:hover{background-color:white; color: black;}"
                                    "QPushButton:pressed{background-color:rgb(85, 170, 255);}");

    scrollArea = new QScrollArea(this);
    scrollArea->setGeometry(11,24,422,610);

    scrollArea_2 = new QScrollArea(this);
    scrollArea_2->setGeometry(446,24,422,610);
}

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

///打开文件
void MainWindow::on_pushButton_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("选择图像"),"",tr("Images (*.png *.bmp *.jpg)"));
    QImage img;
    img.load(fileName);

    string strFileName = fileName.toStdString();
    imgMat = imread(strFileName,0);

    //QScrollArea *scrollArea = new QScrollArea(this);
    QLabel *label = new QLabel(scrollArea);
    label->setPixmap(QPixmap::fromImage(img));

    //scrollArea->setBackgroundRole(QPalette::Dark);
    //scrollArea->autoFillBackground();
    /*scrollArea->setStyleSheet("QScrollBar:vertical{width:8px;background:rgb(0,0,0);margin:0px,0px,0px,0px;padding-top:9px;padding-bottom:9px;}"
                              "QScrollBar::handle:vertical{width:8px;background:rgba(0,0,0,25%);border-radius:4px;min-height:20;}"
                              "QScrollBar::handle:vertical:hover{width:8px;background:rgba(0,0,0,50%);border-radius:4px;min-height:20;}"
                              "QScrollArea{border-color: rgb(0, 0, 0);}"
                              );*/
    //scrollArea->setGeometry(11,54,422,585);
    //label->setGeometry(10,10,500,500);
    scrollArea->setWidget(label);
    scrollArea->show();
}

///转换图片
void MainWindow::on_pushButton_2_clicked()
{
    //imgMat = imgread("D:/1.jpg",0);
    if(imgMat.empty())
    {
        QMessageBox::information(this, tr("警告信息"),tr("请先读取图片!"));
        return;
    }
    for(int i=0;i<imgMat.rows;i++)
        for(int j=0;j<imgMat.cols;j++)
        {
            if(imgMat.at<uchar>(i,j)<100)
                imgMat.at<uchar>(i,j) = 0;
            else
                imgMat.at<uchar>(i,j) = 255;
        }

    //去黑边
    for(int i=0;i<imgMat.rows/30;i++)
        for(int j=0;j<imgMat.cols;j++)
            imgMat.at<uchar>(i,j) = 255;

    for(int i=0;i<imgMat.rows;i++)
        for(int j=0;j<imgMat.cols/30;j++)
            imgMat.at<uchar>(i,j) = 255;

    for(int i=imgMat.rows*29/30;i<imgMat.rows;i++)
        for(int j=0;j<imgMat.cols;j++)
            imgMat.at<uchar>(i,j) = 255;

    for(int i=0;i<imgMat.rows;i++)
        for(int j=imgMat.cols*29/30;j<imgMat.cols;j++)
            imgMat.at<uchar>(i,j) = 255;

    /*cv::Point2f center = cv::Point2f(imgMat.cols / 2, imgMat.rows / 2);  // 旋转中心
    double angle = 90;  // 旋转角度
    double scale = 0.7; // 缩放尺度

    Mat rotateMat;
    rotateMat = cv::getRotationMatrix2D(center, angle, scale);

    Mat rotateImg;
    cv::warpAffine(imgMat, rotateImg, rotateMat, imgMat.size());*/

    imwrite("imgMat.jpg",imgMat);
    QImage img;
    img.load("imgMat.jpg");

    //QScrollArea *scrollArea_2 = new QScrollArea(this);
    QLabel *label_2 = new QLabel(scrollArea_2);
    label_2->setPixmap(QPixmap::fromImage(img));
    scrollArea_2->autoFillBackground();
    //scrollArea_2->setGeometry(446,54,422,585);
    scrollArea_2->setWidget(label_2);
    scrollArea_2->show();
}

///另存为
void MainWindow::on_pushButton_3_clicked()
{
    if(imgMat.empty())
    {
        QMessageBox::information(this, tr("警告信息"),tr("请先读取图片!"));
        return;
    }
    QString fileAsSave = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)"));
    string strfileName = fileAsSave.toStdString();
    imwrite(strfileName,imgMat);
    imgMat.release();
}
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <QScrollArea>
#include <QLabel>

#include "highgui/highgui.hpp"
#include "cv.h"
#include "core/core.hpp"
#include "opencv.hpp"

using namespace cv;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;

    Mat imgMat;
    QScrollArea *scrollArea;
    QScrollArea *scrollArea_2;
};

#endif // MAINWINDOW_H



猜你喜欢

转载自blog.csdn.net/qq_35759050/article/details/54237469
今日推荐