public override IObservable <IplImage> Process(IObservable <IplImage> source) { return(source.Select(input => { var targetSize = Size; if (targetSize.Width == 0) { targetSize.Width = input.Width; } if (targetSize.Height == 0) { targetSize.Height = input.Height; } Point offset; var offsetNullable = Offset; if (offsetNullable.HasValue) { offset = offsetNullable.Value; } else { offset.X = (targetSize.Width - input.Width) / 2; offset.Y = (targetSize.Height - input.Height) / 2; } var right = targetSize.Width - offset.X - input.Width; var bottom = targetSize.Height - offset.Y - input.Height; if (offset.X == 0 && offset.Y == 0 && right == 0 && bottom == 0) { return input; } var inputRect = new Rect(0, 0, input.Width, input.Height); AdjustRectangle(ref offset.X, right, ref inputRect.X, ref inputRect.Width); AdjustRectangle(ref offset.Y, bottom, ref inputRect.Y, ref inputRect.Height); if (offset.X <= 0 && offset.Y <= 0 && right <= 0 && bottom <= 0) { return input.GetSubRect(inputRect); } var output = new IplImage(targetSize, input.Depth, input.Channels); using (var inputHeader = input.GetSubRect(inputRect)) { CV.CopyMakeBorder(inputHeader, output, offset, BorderType, FillValue); } return output; })); }
public IObservable <IplImage> Process(IObservable <IplImage> source) { return(source.Select(input => { var offset = Offset; var copyOffset = new Point(Math.Max(0, offset.X), Math.Max(0, offset.Y)); var size = new Size(input.Width + Math.Abs(offset.X), input.Height + Math.Abs(offset.Y)); Arr output = new IplImage(size, input.Depth, input.Channels); CV.CopyMakeBorder(input, output, copyOffset, BorderType, FillValue); output = output.GetSubRect(new Rect( Math.Max(0, -offset.X), Math.Max(0, -offset.Y), input.Width, input.Height)); return output.GetImage(); })); }
public override IObservable <Mat> Process(IObservable <Mat> source) { return(source.Select(input => { var offset = Offset; var rows = input.Rows + Math.Abs(offset.Y); var cols = input.Cols + Math.Abs(offset.X); var copyOffset = new Point(Math.Max(0, offset.X), Math.Max(0, offset.Y)); var output = new Mat(rows, cols, input.Depth, input.Channels); CV.CopyMakeBorder(input, output, copyOffset, BorderType, FillValue); output = output.GetSubRect(new Rect( Math.Max(0, -offset.X), Math.Max(0, -offset.Y), input.Cols, input.Rows)); return output; })); }
public override IObservable <Mat> Process(IObservable <Mat> source) { return(Observable.Defer(() => { Mat kernel = null; Mat overlap = null; Mat overlapInput = null; Mat overlapEnd = null; Mat overlapStart = null; Mat overlapFilter = null; Rect overlapOutput = default(Rect); float[] currentKernel = null; return source.Select(input => { if (Kernel != currentKernel || currentKernel != null && (input.Rows != overlapOutput.Height || input.Cols != overlapOutput.Width)) { currentKernel = Kernel; if (currentKernel == null || currentKernel.Length == 0) { kernel = null; } else { kernel = new Mat(1, currentKernel.Length, Depth.F32, 1); Marshal.Copy(currentKernel, 0, kernel.Data, currentKernel.Length); var anchor = Anchor; if (anchor == -1) { anchor = kernel.Cols / 2; } overlap = new Mat(input.Rows, input.Cols + kernel.Cols - 1, input.Depth, input.Channels); overlapInput = overlap.GetSubRect(new Rect(kernel.Cols - 1, 0, input.Cols, input.Rows)); overlapFilter = new Mat(overlap.Rows, overlap.Cols, overlap.Depth, overlap.Channels); if (kernel.Cols > 1) { overlapEnd = overlap.GetSubRect(new Rect(overlap.Cols - kernel.Cols + 1, 0, kernel.Cols - 1, input.Rows)); overlapStart = overlap.GetSubRect(new Rect(0, 0, kernel.Cols - 1, input.Rows)); } overlapOutput = new Rect(anchor, 0, input.Cols, input.Rows); CV.CopyMakeBorder(input, overlap, new Point(kernel.Cols - 1, 0), IplBorder.Reflect); } } if (kernel == null) { return input; } else { CV.Copy(input, overlapInput); CV.Filter2D(overlap, overlapFilter, kernel, new Point(Anchor, -1)); if (overlapEnd != null) { CV.Copy(overlapEnd, overlapStart); } return overlapFilter.GetSubRect(overlapOutput).Clone(); } }); })); }