项目环境:win10,Qt5.14,halcon20 功能:1.读取指定图像2.读取指定目录下的图像 项目配置文件 QT += core gui greate
功能: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;
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读取像素项目内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C++ Qt之halcon读取像素项目过程详解
本文链接: https://lsjlt.com/news/135352.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0