public IObservable <Mat> Process(IObservable <Mat> source) { return(Observable.Defer(() => { // subscribe action (i.e. starting bonsai) var decoder = LoadMatrix(DecoderFileName, rows: 6); var dynamics = LoadMatrix(DynamicsFileName, rows: 6); var state = Mat.Zeros(6, BufferSize, Depth.F32, 1); // return our processed sequence (with decoder captured in the closure) return source.Select(input => { var control = new Mat(decoder.Rows, input.Cols, decoder.Depth, decoder.Channels); // s_t+1 = As_t + M*e_t // (state_dim,num_channels)*(num_channels,acquisition_buffer_length) // + // (state_dim,state_dim)*(state_dim,1) // Console.WriteLine(input.Size); // Console.WriteLine(decoder.Size); // Console.WriteLine(dynamics.Size); DotProduct(decoder, input, control); DotProduct(dynamics, state, state); CV.Add(control, state, state); // have to copy this for the outside world return state.Clone(); }); })); }
public void Add_InplaceMat_MatElementsAreDoubled() { var mat = Mat.FromArray(new byte[, ] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }); CV.Add(mat, mat, mat); Assert.AreEqual(2, mat.GetReal(1, 1)); }
public override IObservable <Mat> Process(IObservable <Mat> source) { return(source.Select(input => { var channels = Channels; var output = new Mat(input.Size, input.Depth, input.Channels); var reference = new Mat(1, input.Cols, input.Depth, input.Channels); if (channels == null || channels.Length == 0) { if (input.Depth != Depth.F32) { var temp = new Mat(reference.Rows, reference.Cols, Depth.F32, reference.Channels); CV.Reduce(input, temp, 0, ReduceOperation.Avg); CV.Convert(temp, reference); } else { CV.Reduce(input, reference, 0, ReduceOperation.Avg); } } else if (channels.Length == 1) { CV.Copy(input.GetRow(channels[0]), reference); } else { var sum = input.Depth != Depth.F32 ? new Mat(reference.Rows, reference.Cols, Depth.F32, reference.Channels) : reference; sum.SetZero(); for (int i = 0; i < channels.Length; i++) { using (var referenceChannel = input.GetRow(channels[i])) { CV.Add(sum, referenceChannel, sum); } } CV.ConvertScale(sum, reference, 1f / channels.Length); } CV.Repeat(reference, output); CV.Sub(input, output, output); return output; })); }
public override IObservable <IplImage> Process(IObservable <IplImage> source) { return(Observable.Defer(() => { var count = 0; IplImage mean = null; return source.Select(input => { if (mean == null) { mean = new IplImage(input.Size, input.Depth, input.Channels); mean.SetZero(); } var output = new IplImage(input.Size, input.Depth, input.Channels); CV.Sub(input, mean, output); CV.ConvertScale(output, output, 1f / ++count, 0); CV.Add(mean, output, mean); CV.Copy(mean, output); return output; }); })); }
public override IObservable <TArray> Process <TArray>(IObservable <TArray> source) { return(Observable.Defer(() => { var count = 0; TArray mean = null; var outputFactory = ArrFactory <TArray> .TemplateFactory; return source.Select(input => { if (mean == null) { mean = outputFactory(input); mean.SetZero(); } var output = outputFactory(input); CV.Sub(input, mean, output); CV.ConvertScale(output, output, 1f / ++count, 0); CV.Add(mean, output, output); mean = output; return output; }); })); }
public override IObservable <Mat> Process(IObservable <Mat> source) { return(Observable.Create <Mat>(observer => { var carry = 0; var index = 0; var offset = 0; var lottery = 0; var scaleFactor = 0.0; var currentFactor = 0; var buffer = default(Mat); var carryBuffer = default(Mat); var downsampling = Downsampling; var random = downsampling == DownsamplingMethod.Dithering ? new Random() : null; var reduceOp = (ReduceOperation)(downsampling - DownsamplingMethod.Sum); if (reduceOp == ReduceOperation.Avg) { reduceOp = ReduceOperation.Sum; } var downsample = downsampling == DownsamplingMethod.LowPass ? filter.Process(source) : source; return downsample.Subscribe(input => { try { var bufferLength = BufferLength; if (bufferLength == 0) { bufferLength = input.Cols; } if (buffer == null || buffer.Rows != input.Rows || currentFactor != factor) { index = 0; currentFactor = factor; if (downsampling >= DownsamplingMethod.Sum) { carry = currentFactor; carryBuffer = new Mat(input.Rows, 1, input.Depth, input.Channels); if (downsampling == DownsamplingMethod.Avg) { scaleFactor = 1.0 / currentFactor; } else { scaleFactor = 0; } } else if (random != null) { lottery = random.Next(currentFactor); offset = lottery; } else { offset = 0; } buffer = CreateBuffer(bufferLength, input); } while (offset < input.Cols) { // Process decimation data on this buffer Rect outputRect; if (downsampling > DownsamplingMethod.LowPass) { outputRect = new Rect(index, 0, 1, input.Rows); } else { var samples = input.Cols - offset; var whole = samples / currentFactor; outputRect = new Rect(index, 0, Math.Min(buffer.Cols - index, whole), input.Rows); } if (downsampling >= DownsamplingMethod.Sum) { // Reduce decimate var inputSamples = Math.Min(input.Cols - offset, carry); var inputRect = new Rect(offset, 0, inputSamples, input.Rows); using (var inputBuffer = input.GetSubRect(inputRect)) using (var outputBuffer = buffer.GetCol(index)) { if (carry < currentFactor) { CV.Reduce(inputBuffer, carryBuffer, 1, reduceOp); switch (reduceOp) { case ReduceOperation.Sum: CV.Add(outputBuffer, carryBuffer, outputBuffer); break; case ReduceOperation.Max: CV.Max(outputBuffer, carryBuffer, outputBuffer); break; case ReduceOperation.Min: CV.Min(outputBuffer, carryBuffer, outputBuffer); break; } } else { CV.Reduce(inputBuffer, outputBuffer, 1, reduceOp); } offset += inputRect.Width; carry -= inputSamples; if (carry <= 0) { index++; carry = currentFactor; if (scaleFactor > 0) { CV.ConvertScale(outputBuffer, outputBuffer, scaleFactor); } } } } else if (outputRect.Width > 1) { // Block decimate var inputRect = new Rect(offset, 0, outputRect.Width * currentFactor, input.Rows); using (var inputBuffer = input.GetSubRect(inputRect)) using (var outputBuffer = buffer.GetSubRect(outputRect)) { CV.Resize(inputBuffer, outputBuffer, SubPixelInterpolation.NearestNeighbor); } index += outputRect.Width; offset += inputRect.Width; } else { // Decimate single time point using (var inputBuffer = input.GetCol(offset)) using (var outputBuffer = buffer.GetCol(index)) { CV.Copy(inputBuffer, outputBuffer); } index++; if (random != null) { offset += currentFactor - lottery; lottery = random.Next(currentFactor); offset += lottery; } else { offset += currentFactor; } } if (index >= buffer.Cols) { index = 0; observer.OnNext(buffer); buffer = CreateBuffer(bufferLength, input); } } offset -= input.Cols; } catch (Exception ex) { observer.OnError(ex); } }, observer.OnError, () => { // Emit pending buffer if (index > 0) { observer.OnNext(buffer.GetCols(0, index)); } buffer = null; observer.OnCompleted(); }); })); }