Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TPL Dataflow: Persist previous data

I have been using TPL dataflow for an image processing pipeline using the producer/consumer pattern.

I'm trying to work out the best approach to allow for algorithms that require either the previous frame or a persistent object.

An example of one of these processes is background subtraction where the foreground mask is found by comparing the current frame to a dynamic background model.

One idea of how to achieve this is to create a cycle in the pipeline:

Is there a standard way of approaching this sort of pipeline?

Are there any issues related to the ordering of data or the asynchronous operation?

like image 349
John Redmond Avatar asked Aug 12 '14 08:08

John Redmond


1 Answers

You don't need to complicate things by using a cycle in the pipeline, like in your image, all you need is to keep the persistent data in a variable that persists between calls to the processing function.

If you're using a lambda, that variable can be a local variable outisde of the lambda:

IPropagatorBlock<InputImage, OutputImage> CreateProcessingBlock()
{
    InputImage previousImage = null;

    return new TransformBlock<InputImage, OutputImage>(
        inputImage =>
        {
            var result = Process(inputImage, previousImage);
            previousImage = inputImage;
            return result;
        })
}

If you're using an instance method on some object, that variable can be an instance field on that object:

class Processor
{
    InputImage previousImage;

    public OutputImage Process(InputImage inputImage)
    {
        var result = Process(inputImage, previousImage);
        previousImage = inputImage;
        return result;
    }
}

…

new TransformBlock<InputImage, OutputImage>(new Processor().Process)
like image 104
svick Avatar answered Sep 28 '22 00:09

svick