TPL data stream: save previous data

I am using a TPL data stream for an image processing pipeline using a producer / consumer pattern.

I am trying to develop the best approach to use algorithms that require either a previous frame or a permanent object.

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

One of the ideas on how to achieve this is to create a loop in the pipeline:

Is there a standard way to approach this type of pipeline?

Are there any problems with data sequencing or asynchronous operation?

+5
source share
1 answer

You do not need to complicate the work using a loop in the pipeline, as in your image, all you need is to save constant data in a variable that is stored between calls to the processing function.

If you use lambda, this variable can be the outisde lambda local variable:

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

If you use the instance method for some object, this variable can be an instance field on this 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) 
+3
source

Source: https://habr.com/ru/post/1200095/


All Articles