美文网首页
【车牌识别】part1先设计个“好看”的界面

【车牌识别】part1先设计个“好看”的界面

作者: llooRice | 来源:发表于2017-11-24 15:11 被阅读0次

    用c++写界面,用MFC库和Qt库的比较多。Qt相比MFC而言,更加的简易,更加容易上手,所以博主选择了用Qt写界面(其实是我MFC太生疏了,逃。。。)

    首先得装上个Qt,就Qt5.8好了,我选择了MSVC版本的,因为我要在VS上调用Qt。另外下载qs-vs-tools工具,装好之后,稍加配置,就可以愉快地写代码了。VS配合Qt使用,美滋滋。。。

    下面是我设计的界面,觉得怎么样呢?是不是一股浓重的乡村重金属风格的画风迎面而来。。。哈哈,不管了,先将就弄成这样,以后再更改。


    界面

    下面贴出代码,代码很简单。
    NumIdentify.h

    #pragma once
    #pragma execution_character_set("utf-8")
    #include <QtWidgets/QDialog>
    #include <ImageProcessor.h>
    //#include "ui_NumIdentify.h"
    #include <qlabel.h>
    #include <qpushbutton.h>
    #include <qtextedit.h>
    #include <qprogressbar.h>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <qgraphicsview.h>
    
    class NumIdentify : public QDialog
    {
        Q_OBJECT
    
    public:
        NumIdentify(QWidget *parent = Q_NULLPTR);
    
    private:
        //Ui::NumIdentifyClass ui;
        QLabel *ImageLabel1;
        QLabel *ImageLabel2;
        QLabel *TextLabel;
        QLabel *StatusLabel;
    
        QPushButton *OpenBtn;
        QPushButton *CleanBtn;
        QPushButton *IdentifyBtn;
    
        QGraphicsView *Image1;
        QGraphicsView *Image2;
    
        QProgressBar *Progress;
    
        QTextEdit *TextShow;
    
        //the followings are layout
        QVBoxLayout *LeftLayout;
        QVBoxLayout *RightLayout;
        QHBoxLayout *TopLayout;
        QHBoxLayout *BottomLayout;
        QVBoxLayout *MainLayout;
    
        //two scenes
        QGraphicsScene *scene1;
        QGraphicsScene *scene2;
    
        //一个ImageProcessor类的指针
        ImageProcessor *i=NULL;
    
    public:
        void showImage(QImage img,uint view)const;//this function 
        void init();
    
    private slots:
        void onOpenFile();
        void onClean();
        void onIdentify();
    };
    
    

    NumIdentify.cpp

    #include "NumIdentify.h"
    #include <qgraphicsscene.h>
    #include <qpixmap.h>
    #include <ImageProcessor.h>
    #include <qfiledialog.h>
    #include <qmessagebox.h>
    #include <qfont.h>
    
    NumIdentify::NumIdentify(QWidget *parent)
        : QDialog(parent)
    {
        //ui.setupUi(this);
        this->setGeometry(50, 50, 1200,500);
        init();//this function can create the ui
    
    }
    
    //INIT finish arranging layout and appearance
    void NumIdentify::init()
    {
        //create widgets and add them into layout
        //the Top Left Layout
        ImageLabel1 = new QLabel(tr("Original Image"));
        scene1 = new QGraphicsScene;
        Image1 = new QGraphicsView(scene1);
    
        LeftLayout = new QVBoxLayout;
    
        LeftLayout->addWidget(ImageLabel1);
        LeftLayout->addWidget(Image1);
        LeftLayout->setAlignment(Qt::AlignCenter);
    
        //set the right layout
        ImageLabel2 = new QLabel(tr("Cut Image"));
        scene2 = new QGraphicsScene;
        Image2 = new QGraphicsView(scene2);
        //
        TextLabel = new QLabel(tr("Identified Text"));
        TextShow = new QTextEdit();
        RightLayout = new QVBoxLayout;
        RightLayout->addWidget(ImageLabel2);
        RightLayout->addWidget(Image2);
        RightLayout->addWidget(TextShow);
        RightLayout->setAlignment(Qt::AlignCenter);
    
        //set the bottom layout
        OpenBtn = new QPushButton(tr("Open File"));
        CleanBtn = new QPushButton(tr("Clean"));
        IdentifyBtn = new QPushButton(tr("Identify"));
        StatusLabel = new QLabel("Status:");
        Progress = new QProgressBar;
        BottomLayout = new QHBoxLayout;
        BottomLayout->addWidget(OpenBtn);
        BottomLayout->addWidget(CleanBtn);
        BottomLayout->addWidget(IdentifyBtn);
        BottomLayout->addWidget(StatusLabel);
        BottomLayout->addWidget(Progress);
    
        //set the top layout
        TopLayout = new QHBoxLayout;
        TopLayout->addLayout(LeftLayout);
        TopLayout->addLayout(RightLayout);
    
        //set the general layout
        MainLayout = new QVBoxLayout(this);
        MainLayout->addLayout(TopLayout);
        MainLayout->addLayout(BottomLayout);
        setLayout(MainLayout);
    
        //上面的语句完成了UI的初始化
        //以下语句将控件和函数联系起来,主要是一些connect函数
        connect(OpenBtn, SIGNAL(clicked()), this, SLOT(onOpenFile()));
        connect(CleanBtn, SIGNAL(clicked()), this, SLOT(onClean()));
        connect(IdentifyBtn, SIGNAL(clicked()), this, SLOT(onIdentify()));
    }
    
    //SHOWIMAGE show image designated view
    //when view=0,show image in scene1
    //when view=1,show image in scene2
    //this function must be used after scene1 and scene2 were created.
    void NumIdentify::showImage(QImage img, uint view)const
    {
        QPixmap temp = QPixmap::fromImage(img,Qt::ColorOnly);
        if (view == 0)
        {
            scene1->addPixmap(temp);
        }
        else if (view == 1)
        {
            scene2->addPixmap(temp);
        }
    }
    
    void NumIdentify::onOpenFile()
    {
        QString path0 = QFileDialog::getOpenFileName(this, tr("Open..."), tr("D:\\"), tr("Images (*.jpg *.jpeg)"));
        if (path0.length() == 0) {
            QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
            return;//退出
        }
        string path = path0.toStdString();//QString to string
        i = new ImageProcessor(path);
        
        //显示原始图形
        showImage(i->Mat2QImage(i->originalImage), 0);
    }
    
    void NumIdentify::onIdentify()
    {
        if (i != NULL)//存在ImagegProcessor对象才能识别啊
        {
            i->action();
        }
        else if (i == NULL)
        {
            return;
        }
        //显示切割图像accurateImage
        //Mat temp;
        //cvtColor(i->accurateImage, temp, CV_GRAY2BGR);
        showImage(i->Mat2QImage(i->accurateImage), 1);
    
        //显示切割出来的字符
        string tmp="";
        for (int k = 0; k < i->recognizedCharacters.size(); k++)
            tmp = tmp + i->recognizedCharacters[k];
    
        QString tmp1 = QString::fromStdString(tmp);
        TextShow->setFontWeight(QFont::Bold);//粗体显示
        TextShow->setFont(QFont("Timers", 28, QFont::Bold));
        TextShow->setPlainText(tmp1.toUtf8());
    }
    
    void NumIdentify::onClean()
    {
        //QMessageBox::information(this, tr("just try"), tr("test"));
        //先把所有的显示给清空
        scene1->clear();
        scene2->clear();
        TextShow->clear();
        i = NULL;
    }
    

    完毕

    相关文章

      网友评论

          本文标题:【车牌识别】part1先设计个“好看”的界面

          本文链接:https://www.haomeiwen.com/subject/eqoxbxtx.html