请选择 进入手机版 | 继续访问电脑版

[C 语言] C++ Qt之halcon读取像素项目过程详解

[复制链接]
查看120 | 回复21 | 2021-9-14 09:03:50 | 显示全部楼层 |阅读模式

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

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

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

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

项目设置 文件

  1. QT += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++11
  4. # The following define makes your compiler emit warnings if you use
  5. # any Qt feature that has been marked deprecated (the exact warnings
  6. # depend on your compiler). Please consult the documentation of the
  7. # deprecated API in order to know how to port your code away from it.
  8. DEFINES += QT_DEPRECATED_WARNINGS
  9. # You can also make your code fail to compile if it uses deprecated APIs.
  10. # In order to do so, uncomment the following line.
  11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  13. SOURCES += \
  14. main.cpp \
  15. widget.cpp
  16. HEADERS += \
  17. widget.h
  18. # Default rules for deployment.
  19. qnx: target.path = /tmp/$${TARGET}/bin
  20. else: unix:!android: target.path = /opt/$${TARGET}/bin
  21. !isEmpty(target.path): INSTALLS += target
  22. #win32和unix下的include实际上是一样的
  23. win32:{
  24. #halcon配置代码,已经将include、lib拷贝到项目之下
  25. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalcon
  26. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconc
  27. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcpp
  28. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcppxl
  29. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconcxl
  30. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhalconxl
  31. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhdevenginecpp
  32. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -lhdevenginecppxl
  33. LIBS += -L$$PWD/halcon/win/lib/x64-win64/ -llibiomp5md
  34. #INCLUDEPATH += $$PWD/halcon/win/include
  35. #DEPENDPATH += $$PWD/halcon/win/include
  36. INCLUDEPATH += $$PWD/halcon/linux/include
  37. DEPENDPATH += $$PWD/halcon/linux/include
  38. }
  39. unix:{
  40. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalcon
  41. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconc
  42. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconcpp
  43. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconcppxl
  44. LIBS += -L$$PWD/halcon/linux/lib/x64-linux/ -lhalconcxl
  45. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhalconxl
  46. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhdevenginecpp
  47. LIBS += -L$$PWD/halcon/linux/lib/x64-linux -lhdevenginecppxl
  48. INCLUDEPATH += $$PWD/halcon/linux/include
  49. DEPENDPATH += $$PWD/halcon/linux/include
  50. }
复制代码

头文件

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QLabel>
  5. #include <QPushButton>
  6. #include <QGroupBox>
  7. #include <QRadioButton>
  8. #include <QStackedWidget>
  9. #include <QLineEdit>
  10. #include <QStyle>
  11. #include <QDebug>
  12. #include <QStandardItemModel>
  13. #include <QTabWidget>
  14. #include <QTableWidget>
  15. #include <QHeaderView>
  16. #include <QTableWidgetItem>
  17. #include <QFileDialog>
  18. #include "halconcpp/HalconCpp.h"
  19. #include "Halcon.h"
  20. using namespace HalconCpp;
  21. #pragma execution_character_set("utf-8") //支持中文
  22. class Widget : public QWidget
  23. {
  24. Q_OBJECT
  25. public:
  26. Widget(QWidget *parent = nullptr);
  27. ~Widget();
  28. private:
  29. QLabel *image_label;
  30. HTuple hv_WindowHandle;
  31. //HTuple hv_AcqHandle;
  32. HObject ho_Image;
  33. HTuple hv_Width, hv_Height;
  34. //Hlong windID;
  35. Hlong MainWndID;
  36. QGroupBox *groupbox1;
  37. QRadioButton *radio1;
  38. QRadioButton *radio2;
  39. QStackedWidget *stackedwidget;
  40. QWidget *page1;
  41. QWidget *page2;
  42. QLineEdit *ledit1;
  43. QPushButton *btn1;
  44. QLineEdit *ledit2;
  45. QPushButton *btn2;
  46. QTableWidget *tableWidget;
  47. QTableWidgetItem *item0;
  48. QString file_path;
  49. QStringList filenameList;
  50. //QStringList fullfilename;
  51. QPushButton *btn3;
  52. void create_widget();
  53. void filelist_refresh();
  54. QStringList readfilelist;
  55. QString file_name1;
  56. QString file_name2;
  57. int imageNo=0;
  58. /*
  59. enum CellType{ct_isUsed,ct_rowNo};
  60. enum FieldColNum{col_isUsed,col_rowNo};
  61. QLabel *labCellIndex;
  62. QLabel *labCellType;
  63. QLabel *labID;
  64. void createItemsARow(bool isUsed,int rowNo,QString FileName);
  65. */
  66. private slots:
  67. void on_radioButton1_clicked();
  68. void on_radioButton2_clicked();
  69. void on_btn1_clicked();
  70. void on_btn2_clicked();
  71. void on_btn3_clicked();
  72. //void on_item_clicked(QTableWidgetItem * item);
  73. void on_checkchangestate(int, int);
  74. //void slotHeaderClicked(int);
  75. };
  76. #endif // WIDGET_H
复制代码

c++文件

  1. #include "widget.h"
  2. Widget::Widget(QWidget *parent)
  3. : QWidget(parent)
  4. {
  5. create_widget();
  6. connect(radio1,SIGNAL(clicked(bool)),this,SLOT(on_radioButton1_clicked()));
  7. connect(radio2,SIGNAL(clicked(bool)),this,SLOT(on_radioButton2_clicked()));
  8. connect(btn1,SIGNAL(clicked(bool)),this,SLOT(on_btn1_clicked()));
  9. connect(btn2,SIGNAL(clicked(bool)),this,SLOT(on_btn2_clicked()));
  10. connect(btn3,SIGNAL(clicked(bool)),this,SLOT(on_btn3_clicked()));
  11. connect(tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(on_checkchangestate(int, int)));
  12. //connect(tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(slotHeaderClicked(int)));
  13. }
  14. Widget::~Widget()
  15. {
  16. }
  17. void Widget::create_widget()
  18. {
  19. this->setFixedSize(916,547);
  20. this->setWindowTitle("图像读取");
  21. image_label =new QLabel(this);
  22. image_label->setGeometry(290,20,611,461);
  23. MainWndID = (Hlong)this->image_label->winId();
  24. SetWindowAttr("background_color","gray");//设置背景为灰色
  25. OpenWindow(0, 0, image_label->width(), image_label->height(), MainWndID,
  26. "visible", "", &hv_WindowHandle);
  27. HDevWindowStack::Push(hv_WindowHandle);
  28. groupbox1 = new QGroupBox(this);
  29. groupbox1->setGeometry(10,20,261,121);
  30. QLabel *label1 =new QLabel(this->groupbox1);
  31. label1->setText("采集模式");
  32. label1->setGeometry(0,0,261,21);
  33. radio1 = new QRadioButton(tr("指定图像"),this->groupbox1);
  34. radio2 = new QRadioButton(tr("文件目录"),this->groupbox1);
  35. radio1->setGeometry(50,40,100,20);
  36. radio2->setGeometry(50,70,100,20);
  37. radio1->setChecked(true);
  38. stackedwidget = new QStackedWidget(this);
  39. stackedwidget->setGeometry(20,160,251,301);
  40. page1 = new QWidget();
  41. QLabel *label2 =new QLabel(page1);
  42. label2->setGeometry(0,0,100,20);
  43. label2->setText("指定图片");
  44. ledit1 = new QLineEdit(page1);
  45. //ledit1->setGeometry(30,130,150,30);
  46. ledit1->setGeometry(0,130,200,30);
  47. ledit1->setReadOnly(true);
  48. btn1 = new QPushButton(page1);
  49. btn1->setGeometry(200,130,30,30);
  50. btn1->setIcon(style()->standardIcon(QStyle::SP_FileDialogContentsView));
  51. page2 = new QWidget();
  52. QLabel *label3 =new QLabel(page2);
  53. label3->setGeometry(0,0,100,20);
  54. label3->setText("文件目录");
  55. ledit2 = new QLineEdit(page2);
  56. ledit2->setGeometry(30,30,150,30);
  57. btn2 = new QPushButton(page2);
  58. btn2->setGeometry(180,30,30,30);
  59. btn2->setIcon(style()->standardIcon(QStyle::SP_FileDialogContentsView));
  60. //QTableWidget *tableWidget = new QTableWidget(10,3,page2);
  61. tableWidget = new QTableWidget(page2);
  62. //QTableWidget *tableWidget = new QTableWidget(page2);
  63. tableWidget->setRowCount(0);
  64. tableWidget->setColumnCount(3);
  65. //page2->addWidget();
  66. tableWidget->setGeometry(0,70,251,230);
  67. //tableWidget->resizeColumnsToContents();
  68. //tableWidget->resizeRowsToContents();
  69. //tableWidget->setColumnWidth(3,200);
  70. tableWidget->setColumnWidth(0,50);
  71. tableWidget->setColumnWidth(1,50);
  72. //tableWidget->setColumnWidth(2,QHeaderView::Stretch);
  73. tableWidget->setColumnWidth(2,120);
  74. //tableWidget->setRowHeight(3,60);
  75. tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
  76. //tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
  77. tableWidget->show();
  78. QStringList headerText;
  79. headerText<<"选取"<<"索引"<<"文件名";
  80. tableWidget->setHorizontalHeaderLabels(headerText);
  81. tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  82. tableWidget->horizontalHeader()->setDisabled(true);//禁止用户拖动改变列宽
  83. tableWidget->verticalHeader()->setDisabled(true);//设置行不能拖动
  84. //tableWidget->setEnabled(false);
  85. stackedwidget->addWidget(page1);
  86. stackedwidget->addWidget(page2);
  87. btn3 = new QPushButton(this);
  88. btn3->setGeometry(540,505,91,31);
  89. btn3->setText("执行");
  90. }
  91. void Widget::on_radioButton1_clicked()
  92. {
  93. qDebug()<<"1";
  94. stackedwidget->setCurrentIndex(0);
  95. }
  96. void Widget::on_radioButton2_clicked()
  97. {
  98. qDebug()<<"2";
  99. stackedwidget->setCurrentIndex(1);
  100. }
  101. void Widget::on_btn1_clicked()
  102. {
  103. qDebug()<<"btn1";
  104. qDebug() << "按下了1号按钮,读取图片";
  105. file_name1 = QFileDialog::getOpenFileName(this,tr("open file"),".",tr("Video Files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm *.jpeg)"));
  106. ledit1->setText(file_name1);
  107. }
  108. void Widget::on_btn2_clicked()
  109. {
  110. //imageNo = 0;
  111. qDebug()<<"btn2";
  112. qDebug() << "按下了2号按钮,读取图片";
  113. try
  114. {
  115. file_path = QFileDialog::getExistingDirectory(this,tr("Open Directory"),"./");
  116. qDebug() << file_path;
  117. ledit2->setText(file_path);
  118. QDir dir(file_path);
  119. // cout<<"bian li kai shi "<<endl;
  120. if(!dir.exists()){
  121. qDebug()<<"文件路径错误";
  122. //return false;
  123. }
  124. dir.setFilter(QDir::Files | QDir::NoSymLinks);
  125. QStringList filters;
  126. filters <<"*.bmp"<<"*.jpg"<<"*.pbm"<<"*.pgm"<<"*.png"<<"*.ppm"<<"*.xbm"<<"*.xpm"<<"*.jpeg";
  127. dir.setNameFilters(filters);
  128. filenameList = dir.entryList();
  129. qDebug()<<filenameList;
  130. ///
  131. tableWidget->clearContents();
  132. tableWidget->setRowCount(filenameList.count());
  133. for(int i=0;i<filenameList.count();i++)
  134. {
  135. qDebug()<<filenameList[i];
  136. //QTableWidgetItem *item = new QTableWidgetItem();
  137. item0 = new QTableWidgetItem();
  138. item0->setCheckState(Qt::Checked);
  139. tableWidget->setItem(i, 0, item0);
  140. QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(i));
  141. tableWidget->setItem(i, 1, item1);
  142. QTableWidgetItem *item2 = new QTableWidgetItem(filenameList[i]);
  143. tableWidget->setItem(i, 2, item2);
  144. }
  145. filelist_refresh();
  146. }
  147. catch (HException &HDevExpDefaultException)
  148. {
  149. HTuple hv_Exception;
  150. HDevExpDefaultException.ToHTuple(&hv_Exception);
  151. }
  152. }
  153. void Widget::on_btn3_clicked(){
  154. qDebug() << "按下了3号按钮,读取图片";
  155. ClearWindow(hv_WindowHandle);
  156. if(radio1->isChecked())
  157. {
  158. //显示指定图片
  159. try
  160. {
  161. //QString file_name;
  162. QString image;
  163. if(file_name1.isNull()){
  164. image = "";
  165. }else{
  166. image=file_name1;
  167. }
  168. ReadImage(&ho_Image,image.toLatin1().data());
  169. GetImageSize(ho_Image, &hv_Width, &hv_Height);
  170. SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
  171. DispObj(ho_Image, HDevWindowStack::GetActive());
  172. }
  173. catch (HException &HDevExpDefaultException)
  174. {
  175. HTuple hv_Exception;
  176. HDevExpDefaultException.ToHTuple(&hv_Exception);
  177. }
  178. }else if(radio2->isChecked())
  179. {
  180. //显示文件目录下选取图像]
  181. if(imageNo>=readfilelist.count()){
  182. imageNo=0;
  183. }
  184. try
  185. {
  186. //QString file_name;
  187. QString image;
  188. if(readfilelist.isEmpty()){
  189. image="";
  190. }else{
  191. image = readfilelist[imageNo];
  192. }
  193. ReadImage(&ho_Image,image.toLatin1().data());
  194. GetImageSize(ho_Image, &hv_Width, &hv_Height);
  195. SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
  196. DispObj(ho_Image, HDevWindowStack::GetActive());
  197. imageNo++;
  198. }
  199. catch (HException &HDevExpDefaultException)
  200. {
  201. HTuple hv_Exception;
  202. HDevExpDefaultException.ToHTuple(&hv_Exception);
  203. }
  204. }
  205. }
  206. void Widget::on_checkchangestate(int value1, int value2){
  207. if(value2==0&&tableWidget->item(value1, value2)->checkState() == Qt::Checked)
  208. {
  209. qDebug()<<"选中";
  210. }
  211. else if(value2==0&&tableWidget->item(value1, value2)->checkState() == Qt::Unchecked)
  212. {
  213. qDebug()<<"未选中";
  214. }
  215. filelist_refresh();
  216. }
  217. void Widget::filelist_refresh(){
  218. imageNo = 0;
  219. readfilelist.clear();
  220. for(int i=0;i<tableWidget->rowCount();i++){
  221. if(tableWidget->item(i, 0)->checkState() == Qt::Checked){
  222. readfilelist.append(file_path+"/"+filenameList[i]);
  223. }
  224. }
  225. qDebug()<<readfilelist;
  226. //readfilelist
  227. }
复制代码

到此这篇关于C++ Qt之halcon读取像素项目过程详解的文章就先容 到这了,更多相干 C++ Qt之halcon读取像素项目内容请搜索 脚本之家从前 的文章或继续欣赏 下面的相干 文章渴望 大家以后多多支持脚本之家!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

avatar 漠河龙哥 | 2021-9-14 18:21:42 | 显示全部楼层
最近精神病院在打折,admin楼主去看看吧?
回复

使用道具 举报

avatar 宋751 | 2021-9-22 01:21:37 | 显示全部楼层
admin楼主人气很旺!
回复

使用道具 举报

avatar 无将大车 | 2021-9-22 18:08:44 | 显示全部楼层
这么经典的话只有admin楼主能想到!
回复

使用道具 举报

avatar aupdbe229 | 2021-9-26 20:34:03 | 显示全部楼层
学习雷锋,好好回帖!
回复

使用道具 举报

avatar 豆芽角角123 | 2021-10-4 10:07:13 | 显示全部楼层
我对admin楼主的敬仰犹如滔滔江水绵延不绝!
回复

使用道具 举报

avatar 潘多拉945 | 2021-10-7 13:45:39 | 显示全部楼层
兰州烧饼,鉴定完毕!
回复

使用道具 举报

avatar 123457080 | 2021-10-11 06:11:51 | 显示全部楼层
admin楼主,我告诉你一个你不知道的的秘密,有一个牛逼的源码论坛他的站点都是商业源码,还是免费下载的那种!特别好用。访问地址:http://www.mxswl.com 猫先森网络
回复

使用道具 举报

最近压力山大啊!
回复

使用道具 举报

avatar luly靓 | 5 天前 | 显示全部楼层
视死如归的架势啊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则