欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++ Qt之halcon读取像素项目过程详解

程序员文章站 2022-03-07 22:12:25
项目环境:win10,qt5.14,halcon20功能:1.读取指定图像2.读取指定目录下的图像项目配置文件qt += core gui greaterthan(qt_major_ver...

项目环境:win10,qt5.14,halcon20

功能:1.读取指定图像2.读取指定目录下的图像

C++ Qt之halcon读取像素项目过程详解

C++ Qt之halcon读取像素项目过程详解

项目配置文件

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读取像素项目内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C++ qt halcon