C++ Qt之halcon讀取像素項目過程詳解

項目環境:win10,qt5.14,halcon20

功能:1.讀取指定圖像2.讀取指定目錄下的圖像

項目配置文件

QT       += core gui
 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
CONFIG += c++11
 
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
SOURCES += \
    main.cpp \
    widget.cpp
 
HEADERS += \
    widget.h
 
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
 
#win32和unix下的include實際上是一樣的
win32:{
   #halcon配置代碼,已經將include、lib拷貝到項目之下
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalcon
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconc
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcpp
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcppxl
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcxl
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconxl
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhdevenginecpp
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhdevenginecppxl
   LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -llibiomp5md
 
   #INCLUDEPATH += $$PWD/halcon/win/include
   #DEPENDPATH += $$PWD/halcon/win/include
 
   INCLUDEPATH += $$PWD/halcon/linux/include
   DEPENDPATH += $$PWD/halcon/linux/include
}
 
 
unix:{
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalcon
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconc
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconcpp
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconcppxl
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux/ -lhalconcxl
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconxl
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhdevenginecpp
   LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhdevenginecppxl
 
   INCLUDEPATH += $$PWD/halcon/linux/include
   DEPENDPATH += $$PWD/halcon/linux/include
}

頭文件

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QGroupBox>
#include <QRadioButton>
#include <QStackedWidget>
#include <QLineEdit>
#include <QStyle>
#include <QDebug>
#include <QStandardItemModel>
#include <QTabWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QFileDialog>
 
#include "halconcpp/HalconCpp.h"
#include "Halcon.h"
 
using namespace HalconCpp;
 
 
#pragma execution_character_set("utf-8") //支持中文
 
class Widget : public QWidget
{
    Q_OBJECT
 
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
 
private:
    QLabel *image_label;
 
    HTuple  hv_WindowHandle;
    //HTuple  hv_AcqHandle;
    HObject  ho_Image;
    HTuple  hv_Width, hv_Height;
    //Hlong windID;
    Hlong  MainWndID;
 
    QGroupBox *groupbox1;
 
    QRadioButton *radio1;
    QRadioButton *radio2;
 
    QStackedWidget *stackedwidget;
    QWidget *page1;
    QWidget *page2;
 
    QLineEdit *ledit1;
    QPushButton *btn1;
 
    QLineEdit *ledit2;
    QPushButton *btn2;
    QTableWidget *tableWidget;
    QTableWidgetItem *item0;
    QString file_path;
    QStringList filenameList;
    //QStringList fullfilename;
 
    QPushButton *btn3;
 
 
 
    void create_widget();
 
    void filelist_refresh();
    QStringList readfilelist;
 
    QString file_name1;
    QString file_name2;
 
    int imageNo=0;
 
 
 
 
    /*
    enum CellType{ct_isUsed,ct_rowNo};
    enum FieldColNum{col_isUsed,col_rowNo};
    QLabel *labCellIndex;
    QLabel *labCellType;
    QLabel *labID;
    void createItemsARow(bool isUsed,int rowNo,QString FileName);
    */
 
 
private slots:
    void on_radioButton1_clicked();
    void on_radioButton2_clicked();
 
    void on_btn1_clicked();
    void on_btn2_clicked();
    void on_btn3_clicked();
    //void on_item_clicked(QTableWidgetItem * item);
    void on_checkchangestate(int, int);
    //void slotHeaderClicked(int);
 
 
 
};
#endif // WIDGET_H

c++文件

#include "widget.h"
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    create_widget();
 
    connect(radio1,SIGNAL(clicked(bool)),this,SLOT(on_radioButton1_clicked()));
    connect(radio2,SIGNAL(clicked(bool)),this,SLOT(on_radioButton2_clicked()));
 
    connect(btn1,SIGNAL(clicked(bool)),this,SLOT(on_btn1_clicked()));
    connect(btn2,SIGNAL(clicked(bool)),this,SLOT(on_btn2_clicked()));
    connect(btn3,SIGNAL(clicked(bool)),this,SLOT(on_btn3_clicked()));
 
    connect(tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(on_checkchangestate(int, int)));
    //connect(tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(slotHeaderClicked(int)));
 
 
 
}
 
Widget::~Widget()
{
}
 
void Widget::create_widget()
{
    this->setFixedSize(916,547);
    this->setWindowTitle("圖像讀取");
 
    image_label =new QLabel(this);
    image_label->setGeometry(290,20,611,461);
    MainWndID = (Hlong)this->image_label->winId();
    SetWindowAttr("background_color","gray");//設置背景為灰色
    OpenWindow(0, 0, image_label->width(), image_label->height(), MainWndID,
               "visible", "", &hv_WindowHandle);
    HDevWindowStack::Push(hv_WindowHandle);
 
    groupbox1 = new QGroupBox(this);
    groupbox1->setGeometry(10,20,261,121);
    QLabel *label1 =new QLabel(this->groupbox1);
    label1->setText("采集模式");
    label1->setGeometry(0,0,261,21);
 
    radio1 = new QRadioButton(tr("指定圖像"),this->groupbox1);
    radio2 = new QRadioButton(tr("文件目錄"),this->groupbox1);
    radio1->setGeometry(50,40,100,20);
    radio2->setGeometry(50,70,100,20);
    radio1->setChecked(true);
 
 
    stackedwidget = new QStackedWidget(this);
    stackedwidget->setGeometry(20,160,251,301);
 
    page1 = new QWidget();
    QLabel *label2 =new QLabel(page1);
    label2->setGeometry(0,0,100,20);
    label2->setText("指定圖片");
    ledit1 = new QLineEdit(page1);
    //ledit1->setGeometry(30,130,150,30);
    ledit1->setGeometry(0,130,200,30);
    ledit1->setReadOnly(true);
    btn1 = new QPushButton(page1);
    btn1->setGeometry(200,130,30,30);
    btn1->setIcon(style()->standardIcon(QStyle::SP_FileDialogContentsView));
 
    page2 = new QWidget();
    QLabel *label3 =new QLabel(page2);
    label3->setGeometry(0,0,100,20);
    label3->setText("文件目錄");
 
 
    ledit2 = new QLineEdit(page2);
    ledit2->setGeometry(30,30,150,30);
    btn2 = new QPushButton(page2);
    btn2->setGeometry(180,30,30,30);
    btn2->setIcon(style()->standardIcon(QStyle::SP_FileDialogContentsView));
 
    //QTableWidget *tableWidget = new QTableWidget(10,3,page2);
    tableWidget = new QTableWidget(page2);
    //QTableWidget *tableWidget = new QTableWidget(page2);
    tableWidget->setRowCount(0);
    tableWidget->setColumnCount(3);
    //page2->addWidget();
    tableWidget->setGeometry(0,70,251,230);
    //tableWidget->resizeColumnsToContents();
    //tableWidget->resizeRowsToContents();
    //tableWidget->setColumnWidth(3,200);
    tableWidget->setColumnWidth(0,50);
    tableWidget->setColumnWidth(1,50);
    //tableWidget->setColumnWidth(2,QHeaderView::Stretch);
    tableWidget->setColumnWidth(2,120);
    //tableWidget->setRowHeight(3,60);
    tableWidget->verticalHeader()->setVisible(false); //隱藏列表頭
    //tableWidget->horizontalHeader()->setVisible(false); //隱藏行表頭
 
    tableWidget->show();
    QStringList headerText;
    headerText<<"選取"<<"索引"<<"文件名";
    tableWidget->setHorizontalHeaderLabels(headerText);
    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tableWidget->horizontalHeader()->setDisabled(true);//禁止用戶拖動改變列寬
    tableWidget->verticalHeader()->setDisabled(true);//設置行不能拖動
    //tableWidget->setEnabled(false);
 
    stackedwidget->addWidget(page1);
    stackedwidget->addWidget(page2);
 
    btn3 = new QPushButton(this);
    btn3->setGeometry(540,505,91,31);
    btn3->setText("執行");
 
 
}
 
void Widget::on_radioButton1_clicked()
{
    qDebug()<<"1";
    stackedwidget->setCurrentIndex(0);
}
 
void Widget::on_radioButton2_clicked()
{
    qDebug()<<"2";
    stackedwidget->setCurrentIndex(1);
}
 
void Widget::on_btn1_clicked()
{
    qDebug()<<"btn1";
    qDebug() << "按下瞭1號按鈕,讀取圖片";
    file_name1 = QFileDialog::getOpenFileName(this,tr("open  file"),".",tr("Video Files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm *.jpeg)"));
 
    ledit1->setText(file_name1);
 
 
}
 
void Widget::on_btn2_clicked()
{
    //imageNo = 0;
    qDebug()<<"btn2";
    qDebug() << "按下瞭2號按鈕,讀取圖片";
    try
    {
 
        file_path = QFileDialog::getExistingDirectory(this,tr("Open Directory"),"./");
        qDebug() << file_path;
        ledit2->setText(file_path);
 
        QDir dir(file_path);
 
        //  cout<<"bian li kai shi "<<endl;
        if(!dir.exists()){
            qDebug()<<"文件路徑錯誤";
            //return false;
        }
        dir.setFilter(QDir::Files | QDir::NoSymLinks);
        QStringList filters;
        filters <<"*.bmp"<<"*.jpg"<<"*.pbm"<<"*.pgm"<<"*.png"<<"*.ppm"<<"*.xbm"<<"*.xpm"<<"*.jpeg";
        dir.setNameFilters(filters);
 
        filenameList = dir.entryList();
        qDebug()<<filenameList;
 
 
        ///
        tableWidget->clearContents();
        tableWidget->setRowCount(filenameList.count());
 
        for(int i=0;i<filenameList.count();i++)
        {
            qDebug()<<filenameList[i];
            //QTableWidgetItem *item = new QTableWidgetItem();
            item0 = new QTableWidgetItem();
            item0->setCheckState(Qt::Checked);
 
            tableWidget->setItem(i, 0, item0);
            QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(i));
            tableWidget->setItem(i, 1, item1);
            QTableWidgetItem *item2 = new QTableWidgetItem(filenameList[i]);
            tableWidget->setItem(i, 2, item2);
 
        }
 
        filelist_refresh();
 
 
 
 
    }
    catch (HException &HDevExpDefaultException)
    {
        HTuple  hv_Exception;
        HDevExpDefaultException.ToHTuple(&hv_Exception);
    }
 
}
 
void Widget::on_btn3_clicked(){
    qDebug() << "按下瞭3號按鈕,讀取圖片";
 
    ClearWindow(hv_WindowHandle);
    if(radio1->isChecked())
    {
        //顯示指定圖片
        try
        {
            //QString file_name;
            QString image;
            if(file_name1.isNull()){
                image = "";
 
            }else{
                image=file_name1;
            }
            ReadImage(&ho_Image,image.toLatin1().data());
            GetImageSize(ho_Image, &hv_Width, &hv_Height);
            SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
            DispObj(ho_Image, HDevWindowStack::GetActive());
 
        }
        catch (HException &HDevExpDefaultException)
        {
            HTuple  hv_Exception;
            HDevExpDefaultException.ToHTuple(&hv_Exception);
        }
 
    }else if(radio2->isChecked())
    {
        //顯示文件目錄下選取圖像]
        if(imageNo>=readfilelist.count()){
            imageNo=0;
        }
        try
        {
            //QString file_name;
            QString image;
            if(readfilelist.isEmpty()){
                image="";
            }else{
                image = readfilelist[imageNo];
            }
            ReadImage(&ho_Image,image.toLatin1().data());
            GetImageSize(ho_Image, &hv_Width, &hv_Height);
            SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
            DispObj(ho_Image, HDevWindowStack::GetActive());
            imageNo++;
 
        }
        catch (HException &HDevExpDefaultException)
        {
            HTuple  hv_Exception;
            HDevExpDefaultException.ToHTuple(&hv_Exception);
        }
    }
 
 
 
 
 
}
 
 
void Widget::on_checkchangestate(int value1, int value2){
 
    if(value2==0&&tableWidget->item(value1, value2)->checkState() == Qt::Checked)
    {
        qDebug()<<"選中";
    }
    else if(value2==0&&tableWidget->item(value1, value2)->checkState() == Qt::Unchecked)
    {
        qDebug()<<"未選中";
    }
    filelist_refresh();
}
 
void Widget::filelist_refresh(){
    imageNo = 0;
    readfilelist.clear();
    for(int i=0;i<tableWidget->rowCount();i++){
        if(tableWidget->item(i, 0)->checkState() == Qt::Checked){
            readfilelist.append(file_path+"/"+filenameList[i]);
        }
    }
    qDebug()<<readfilelist;
 
    //readfilelist
}

到此這篇關於C++ Qt之halcon讀取像素項目過程詳解的文章就介紹到這瞭,更多相關C++ Qt之halcon讀取像素項目內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: