0

我是初级程序员

最近,我使用 Halcon 库实现了图像抓取。

当我按下实时按钮时,定时器开始抓取图像。它可以工作,但主屏幕冻结到计时器周期。

所以,我正在使用 Thread 提高图像的性能抓取

首先我实现了这样的线程

[ImageUpdateWorker.h]

class ImageUpdateWorker : public QObject
{
    Q_OBJECT

public:
    explicit ImageUpdateWorker(QObject* parent = 0, QString strThreadName = "ImageUpdateWorker");
    ~ImageUpdateWorker();

signals:
    void finished();
    void grab();

public slots:
    void run();

private:
    bool m_bStop{ false };
};

[ImageUpdateWorker.cpp]

ImageUpdateWorker::ImageUpdateWorker(QObject* parent, QString strThreadName)
    : QObject(parent)
{
    setObjectName(strThreadName);
}

ImageUpdateWorker::~ImageUpdateWorker()
{
}

void ImageUpdateWorker::run()
{
    while (m_bStop == false)
    {
        emit grab();
    }

    emit finished();
}

第二,我实现了继承的 QWidget UI Widget,输出屏幕像这样

    m_pThread = new QThread();
    m_pUpdateWorker = new ImageUpdateWorker(nullptr, strName);
    m_pUpdateWorker->moveToThread(m_pThread); // UpdateWorker move to Thread

    connect(m_pThread, SIGNAL(started()), m_pUpdateWorker, SLOT(run()));
    connect(m_pThread, SIGNAL(finished()), m_pThread, SLOT(deleteLater()));
    connect(m_pUpdateWorker, SIGNAL(finished()), m_pThread, SLOT(quit()));
    connect(m_pUpdateWorker, SIGNAL(finished()), m_pUpdateWorker, SLOT(deleteLater()));
    connect(m_pUpdateWorker, SIGNAL(grab()), this, SLOT(onGrab()));

当我调用“m_pThread->start();” 屏幕开始阻塞:(

如果您有任何建议或信息,我将不胜感激。谢谢你的阅读。

4

2 回答 2

0

我不知道QT。我给你发了我在 C# 中使用的代码。

如果您不想冻结 GUI,主要是必须使用委托。

hdisplay 是 HalconDotNet:HWindowControlWPF 对象。

相机是我定义相机参数的类。

在camera.Grab里面有代码:

  HOperatorSet.GrabImage(out ho_Image, _AcqHandle);
  h_Image = new HImage(ho_Image);

在初始化有代码:

    // Initialise the delegate 
    updateLiveDelegate = new UpdateLiveDelegate(this.UpdateLive);

    HImage ho_Image = new HImage();

这是我使用的代码:

    // ==================
    // LIVE
    // ==================

    bool stopLive = true;

    // Declare the thread
    private Thread liveThread = null;

    // Declare a delegate used to communicate with the UI thread
    private delegate void UpdateLiveDelegate();
    private UpdateLiveDelegate updateLiveDelegate = null;


    private void btnLive_Click(object sender, RoutedEventArgs e)
    {

        try
        {
            stopLive = !stopLive;

            // if stopLive = false, live camera is activated
            if (!stopLive)
            {

                // Launch the thread
                liveThread = new Thread(new ThreadStart(Live));
                liveThread.Start();
            }


        }
        catch (Exception ex)
        {
            // Error

        }

    }


    private void Live()
    {

        try
        {

            while (stopLive == false)
            {

                if (camera.Grab(out ho_Image))
                {
                    // Show progress
                    Dispatcher.Invoke(this.updateLiveDelegate);
                }
                else
                {
                    // No grab
                    stopLive = true;

                }
            }

            // here stopLive is true


        }
        catch (Exception ex)
        {
            // Error
        }




    }

    private void UpdateLive()
    {

        try
        {
            int imageHeight;
            int imageWidth;
            string imageType;
            ho_Image.GetImagePointer1(out imageType, out imageWidth, out imageHeight);

            hDisplay.HalconWindow.SetPart(0, 0, imageHeight - 1, imageWidth - 1);

            // display
            hDisplay.HalconWindow.DispImage(ho_Image);
        }
        catch (Exception ex)
        {
            // Error
        }


    }
于 2020-09-15T17:21:32.603 回答
0

使用 m_pImageUpdateThread->moveToThread(m_pThread);

于 2020-09-15T18:28:25.073 回答