16

我从网络摄像头捕捉图像,对它们进行一些繁重的处理,然后显示结果。为了保持高帧率,我想让不同帧的处理并行运行。

所以,我有一个“生产者”,它捕获图像并将它们添加到“inQueue”中;它还从“outQueue”中获取图像并显示它:

public class Producer
{
    Capture capture;
    Queue<Image<Bgr, Byte>> inQueue;
    Queue<Image<Bgr, Byte>> outQueue;
    Object lockObject;
    Emgu.CV.UI.ImageBox screen;
    public int frameCounter = 0;

    public Producer(Emgu.CV.UI.ImageBox screen, Capture capture, Queue<Image<Bgr, Byte>> inQueue, Queue<Image<Bgr, Byte>> outQueue, Object lockObject)
    {
        this.screen = screen;
        this.capture = capture;
        this.inQueue = inQueue;
        this.outQueue = outQueue;
        this.lockObject = lockObject;
    }

    public void produce()
    {
        while (true)
        {
            lock (lockObject)
            {
                inQueue.Enqueue(capture.QueryFrame());

                if (inQueue.Count == 1)
                {
                    Monitor.PulseAll(lockObject);
                }
                if (outQueue.Count > 0)
                {
                    screen.Image = outQueue.Dequeue();                      
                }
            }
            frameCounter++;
        }           
    }
}

有不同的“消费者”从 inQueue 中获取图像,进行一些处理,然后将它们添加到 outQueue:

public class Consumer
{
    Queue<Image<Bgr, Byte>> inQueue;
    Queue<Image<Bgr, Byte>> outQueue;
    Object lockObject;
    string name;

    Image<Bgr, Byte> image;

    public Consumer(Queue<Image<Bgr, Byte>> inQueue, Queue<Image<Bgr, Byte>> outQueue, Object lockObject, string name)
    {
        this.inQueue = inQueue;
        this.outQueue = outQueue;
        this.lockObject = lockObject;
        this.name = name;
    }

    public void consume()
    {
        while (true)
        {
            lock (lockObject)
            {
                if (inQueue.Count == 0)
                {
                    Monitor.Wait(lockObject);
                    continue;
                }                
                image = inQueue.Dequeue();   
            }

            // Do some heavy processing with the image

            lock (lockObject)
            {
                outQueue.Enqueue(image);
            }

        }
    }
}

其余重要代码是本节:

    private void Form1_Load(object sender, EventArgs e)
    {
        Consumer[] c = new Consumer[consumerCount];
        Thread[] t = new Thread[consumerCount];

        Object lockObj = new object();
        Queue<Image<Bgr, Byte>> inQueue = new Queue<Image<Bgr, Byte>>();
        Queue<Image<Bgr, Byte>> outQueue = new Queue<Image<Bgr, Byte>>();

        p = new Producer(screen1, capture, inQueue, outQueue, lockObj);

        for (int i = 0; i < consumerCount; i++)
        {
            c[i] = new Consumer(inQueue, outQueue, lockObj, "c_" + Convert.ToString(i));
        }
        for (int i = 0; i < consumerCount; i++)
        {
            t[i] = new Thread(c[i].consume);
            t[i].Start();
        }

        Thread pt = new Thread(p.produce);
        pt.Start();
    }

并行化实际上工作得很好,每个添加的线程都会增加线性速度(当然,直到某个点)。问题是我在输出中得到了工件,即使只运行一个线程。工件看起来像是图片的一部分不在正确的位置。

神器示例(这个是不做任何处理保持清晰,但效果是一样的)

任何想法是什么原因造成的?谢谢

4

4 回答 4

7

免责声明:这篇文章不应该完全描述答案,而是给出一些关于为什么显示工件的提示。

快速分析表明,该行为实际上是帧的部分垂直镜像片段。我复制了它,镜像,然后把它放回图像上,并添加了一个糟糕的标记来显示它的位置:

在此处输入图像描述

有两件事立即引起注意:

  • 工件大致定位在“正确”的位置上,只是该位置也是垂直镜像的;
  • 图像略有不同,表明它可能属于不同的帧。

自从我玩原始捕获并遇​​到类似问题以来已经有一段时间了,但我记得这取决于驱动程序的实现方式(或设置 - 这个特定问题发生在为隔行捕获设置特定成像设备时发生)它可能在“自上而下”和“自下而上”扫描之间交替填充其帧缓冲区 - 一旦帧已满,“光标”就会恢复方向。

在我看来,您遇到了竞态条件/缓冲区欠载情况,从帧缓冲区到您的应用程序的传输是在设备传输完整帧之前发生的。

在这种情况下,您将收到部分图像,并且仍未刷新的区域将显示一些先前传输的帧。

如果我必须打赌,我会说工件可能按顺序出现,而不是在同一位置,而是在特定方向(向上或向下)上“波动”,但总是作为镜像位。

于 2014-01-09T01:30:18.070 回答
1

好吧,我认为问题就在这里。这部分代码不能保证您将被两个队列之间的一个线程访问。图像被inQueue弹出实际上并没有在outQueue中按顺序接收

while (true)
{
        lock (lockObject)
        {
            if (inQueue.Count == 0)
            {
                Monitor.Wait(lockObject);
                continue;
            }                
            image = inQueue.Dequeue();   
        }

        // Do some heavy processing with the image

        lock (lockObject)
        {
            outQueue.Enqueue(image);
        }

}
于 2014-01-09T01:15:57.113 回答
1

与@OnoSendai 类似,我并不想解决所述的确切问题。我将不得不编写一个应用程序,而我只是没有时间。但是,我将立即更改的两件事是使用ConcurrentQueue该类,以便您具有线程安全性。而且,我会使用任务库函数在不同的处理器内核上创建并行任务。它们位于 System.Net 和 System.Net.Task 命名空间中。

此外,对我来说,垂直翻转这样的块看起来不仅仅是一件神器。如果您提到的在单个线程中执行时也会发生这种情况,那么我肯定会重新关注等式的“繁重处理”部分。

祝你好运!小心。

于 2014-01-14T02:33:18.890 回答
0

你可能有两个问题:

1) 平行并不能确保图像以正确的顺序添加到输出队列中。我想在图像 6 和 7 之前显示图像 8 会产生一些伪影。在消费者线程中,您必须等待前一个消费者将其图像发布到输出队列才能发布下一个图像。由于任务固有的同步机制,任务对此有很大帮助。

2)您可能在渲染代码中也有问题。

于 2014-01-16T00:27:35.457 回答