日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

【机器视觉】Qt联合Halcon编程之显示多图片

發(fā)布時間:2024/4/24 ChatGpt 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【机器视觉】Qt联合Halcon编程之显示多图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 概述
    • 02. 編寫Halcon程序
    • 03. Halcon程序?qū)С鯟++文件
    • 04. 創(chuàng)建Qt圖形界面項目
    • 05. Qt集成Halcon程序
    • 06. 附錄

01. 概述

QT與Halcon聯(lián)合編程。將Halcon中代碼集成到Qt程序中。

開發(fā)環(huán)境

Qt:Qt5.15.2

Halcon: Halcon 19.11

02. 編寫Halcon程序

Halcon程序示例

* 從本地磁盤讀取一張圖片 read_image (Carb, 'E:/CarB.jpg')* 獲取圖片大小 get_image_size (Carb, Width, Height)* 關(guān)閉當前窗口 dev_close_window ()* 打開新窗口 dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)* 顯示圖片 dev_display (Carb)

Halcon中執(zhí)行結(jié)果

03. Halcon程序?qū)С鯟++文件

將上述Halcon程序?qū)С鰹镃++源程序

導出內(nèi)容如下:

/// // File generated by HDevelop for HALCON/C++ Version 19.11.0.0 // Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936). // Ensure that the interface encoding is set to locale encoding by calling // SetHcppInterfaceStringEncodingIsUtf8(false) at the beginning of the program. // // Please note that non-ASCII characters in string constants are exported // as octal codes in order to guarantee that the strings are correctly // created on all systems, independent on any compiler settings. // // Source files with different encoding should not be mixed in one project. ///#ifndef __APPLE__ # include "HalconCpp.h" # include "HDevThread.h" #else # ifndef HC_LARGE_IMAGES # include <HALCONCpp/HalconCpp.h> # include <HALCONCpp/HDevThread.h> # include <HALCON/HpThread.h> # else # include <HALCONCppxl/HalconCpp.h> # include <HALCONCppxl/HDevThread.h> # include <HALCONxl/HpThread.h> # endif # include <stdio.h> # include <CoreFoundation/CFRunLoop.h> #endifusing namespace HalconCpp;// Procedure declarations // Local procedures void blobImage (HObject *ho_Image, HObject *ho_Region);// Procedures // Local procedures void blobImage (HObject *ho_Image, HObject *ho_Region) {ReadImage(&(*ho_Image), "E:/test.jpg.tif");Threshold((*ho_Image), &(*ho_Region), 128, 255);if (HDevWindowStack::IsOpen())DispObj((*ho_Image), HDevWindowStack::GetActive());if (HDevWindowStack::IsOpen())DispObj((*ho_Region), HDevWindowStack::GetActive());return; }#ifndef NO_EXPORT_MAIN // Main procedure void action() {// Local iconic variablesHObject ho_Carb;// Local control variablesHTuple hv_Width, hv_Height, hv_WindowHandle;//從本地磁盤讀取一張圖片ReadImage(&ho_Carb, "E:/CarB.jpg");//獲取圖片大小GetImageSize(ho_Carb, &hv_Width, &hv_Height);//關(guān)閉當前窗口if (HDevWindowStack::IsOpen())CloseWindow(HDevWindowStack::Pop());//打開新窗口SetWindowAttr("background_color","black");OpenWindow(0,0,hv_Width/2,hv_Height/2,0,"visible","",&hv_WindowHandle);HDevWindowStack::Push(hv_WindowHandle);//顯示圖片if (HDevWindowStack::IsOpen())DispObj(ho_Carb, HDevWindowStack::GetActive());}#ifndef NO_EXPORT_APP_MAIN#ifdef __APPLE__ // On OS X systems, we must have a CFRunLoop running on the main thread in // order for the HALCON graphics operators to work correctly, and run the // action function in a separate thread. A CFRunLoopTimer is used to make sure // the action function is not called before the CFRunLoop is running. // Note that starting with macOS 10.12, the run loop may be stopped when a // window is closed, so we need to put the call to CFRunLoopRun() into a loop // of its own. HTuple gStartMutex; H_pthread_t gActionThread; HBOOL gTerminate = FALSE;static void timer_callback(CFRunLoopTimerRef timer, void *info) {UnlockMutex(gStartMutex); }static Herror apple_action(void **parameters) {// Wait until the timer has fired to start processing.LockMutex(gStartMutex);UnlockMutex(gStartMutex);try{action();}catch (HException &exception){fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),(const char *)exception.ProcName(),(const char *)exception.ErrorMessage());}// Tell the main thread to terminate itself.LockMutex(gStartMutex);gTerminate = TRUE;UnlockMutex(gStartMutex);CFRunLoopStop(CFRunLoopGetMain());return H_MSG_OK; }static int apple_main(int argc, char *argv[]) {Herror error;CFRunLoopTimerRef Timer;CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };CreateMutex("type","sleep",&gStartMutex);LockMutex(gStartMutex);error = HpThreadHandleAlloc(&gActionThread);if (H_MSG_OK != error){fprintf(stderr,"HpThreadHandleAlloc failed: %d\n", error);exit(1);}error = HpThreadCreate(gActionThread,0,apple_action);if (H_MSG_OK != error){fprintf(stderr,"HpThreadCreate failed: %d\n", error);exit(1);}Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,CFAbsoluteTimeGetCurrent(),0,0,0,timer_callback,&TimerContext);if (!Timer){fprintf(stderr,"CFRunLoopTimerCreate failed\n");exit(1);}CFRunLoopAddTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);for (;;){HBOOL terminate;CFRunLoopRun();LockMutex(gStartMutex);terminate = gTerminate;UnlockMutex(gStartMutex);if (terminate)break;}CFRunLoopRemoveTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);CFRelease(Timer);error = HpThreadHandleFree(gActionThread);if (H_MSG_OK != error){fprintf(stderr,"HpThreadHandleFree failed: %d\n", error);exit(1);}ClearMutex(gStartMutex);return 0; } #endifint main(int argc, char *argv[]) {int ret = 0;try{ #if defined(_WIN32)SetSystem("use_window_thread", "true"); #endif// file was stored with local-8-bit encoding// -> set the interface encoding accordinglySetHcppInterfaceStringEncodingIsUtf8(false);// Default settings used in HDevelop (can be omitted)SetSystem("width", 512);SetSystem("height", 512);#ifndef __APPLE__action(); #elseret = apple_main(argc,argv); #endif}catch (HException &exception){fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),(const char *)exception.ProcName(),(const char *)exception.ErrorMessage());ret = 1;}return ret; }#endif#endif

04. 創(chuàng)建Qt圖形界面項目

4.1 簡單的GUI設(shè)計如下

集成halcon編譯需要用msvc,如果用minGW,會出錯,等有時間在去研究minGW編譯情形。

05. Qt集成Halcon程序

5.1 在pro項目配置文件中添加一下Halcon相關(guān)配置

# MacOS specific settings. Note that while dyld will search under # /Library/Frameworks by default, the preprocessor/compiler/linker will not # and need to be told explicitly. macx {QMAKE_CXXFLAGS += -F/Library/FrameworksQMAKE_LFLAGS += -F/Library/FrameworksLIBS += -framework HALCONCpp } else {#defineswin32:DEFINES += WIN32#includesINCLUDEPATH += "$$(HALCONROOT)/include"INCLUDEPATH += "$$(HALCONROOT)/include/halconcpp"#libsQMAKE_LIBDIR += "$$(HALCONROOT)/lib/$$(HALCONARCH)"unix:LIBS += -lhalconcpp -lhalcon -lXext -lX11 -ldl -lpthreadwin32:LIBS += "$$(HALCONROOT)/lib/$$(HALCONARCH)/halconcpp.lib" \"$$(HALCONROOT)/lib/$$(HALCONARCH)/halcon.lib" }

5.2 在mainwindow.h文件中添加Halcon相關(guān)頭文件

#ifndef __APPLE__ # include "HalconCpp.h" # include "HDevThread.h" #else # ifndef HC_LARGE_IMAGES # include <HALCONCpp/HalconCpp.h> # include <HALCONCpp/HDevThread.h> # include <HALCON/HpThread.h> # else # include <HALCONCppxl/HalconCpp.h> # include <HALCONCppxl/HDevThread.h> # include <HALCONxl/HpThread.h> # endif # include <stdio.h> # include <CoreFoundation/CFRunLoop.h> #endifusing namespace HalconCpp;

5.3 在MainWindow添加halcon相關(guān)的變量

源程序如下:

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>#ifndef __APPLE__ # include "HalconCpp.h" # include "HDevThread.h" #else # ifndef HC_LARGE_IMAGES # include <HALCONCpp/HalconCpp.h> # include <HALCONCpp/HDevThread.h> # include <HALCON/HpThread.h> # else # include <HALCONCppxl/HalconCpp.h> # include <HALCONCppxl/HDevThread.h> # include <HALCONxl/HpThread.h> # endif # include <stdio.h> # include <CoreFoundation/CFRunLoop.h> #endifusing namespace HalconCpp;QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACEclass MainWindow : public QMainWindow {Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_3_clicked();private:Ui::MainWindow *ui;// Local iconic variablesHObject ho_Carb;// Local control variablesHTuple hv_Width, hv_Height, hv_WindowHandle;}; #endif // MAINWINDOW_H

5.4 實現(xiàn)采集圖像槽函數(shù)

//采集圖像 void MainWindow::on_pushButton_clicked() {QString fileName = QFileDialog::getOpenFileName(this,tr("打開圖片"), "E://", tr("Image Files (*.png *.jpg *.bmp)"));qDebug() << fileName;//從本地磁盤讀取一張圖片//QString轉(zhuǎn)HTuple要通過StringReadImage(&ho_Carb, fileName.toStdString().c_str());//獲取圖片大小GetImageSize(ho_Carb, &hv_Width, &hv_Height);}

友情提示

//QString轉(zhuǎn)HTuple要通過String
ReadImage(&ho_Carb, fileName.toStdString().c_str());

5.5 實現(xiàn)顯示圖像槽函數(shù)

//顯示圖像 void MainWindow::on_pushButton_3_clicked() {//關(guān)閉當前窗口if (HDevWindowStack::IsOpen())CloseWindow(HDevWindowStack::Pop());//打開新窗口SetWindowAttr("background_color","black");OpenWindow(0,0,hv_Width/2,hv_Height/2, (Hlong)ui->label->winId(),"visible","",&hv_WindowHandle);HDevWindowStack::Push(hv_WindowHandle);//顯示圖片if (HDevWindowStack::IsOpen())DispObj(ho_Carb, HDevWindowStack::GetActive()); }

友情提示

Qt中winId()和Halcon中窗口句柄轉(zhuǎn)換

OpenWindow(0,0,hv_Width/2,hv_Height/2, (Hlong)ui->label->winId(),“visible”,"",&hv_WindowHandle);

5.6 編譯程序

運行結(jié)果如下:

06. 附錄

6.1 測試程序下載

下載:【機器視覺】Qt聯(lián)合Halcon編程之顯示多圖片.rar

總結(jié)

以上是生活随笔為你收集整理的【机器视觉】Qt联合Halcon编程之显示多图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。