public static DenseVector ConvKernel(StandardConvolutionKernels kernelType, int kernelHalfWidth = 10) { double maxL = ((double)kernelHalfWidth) * 2.0d; DenseVector dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, 1.0d); if (kernelType == StandardConvolutionKernels.cos) { dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, (idx) => { double curDist = (new PointD(idx - (double)kernelHalfWidth, 0.0d)).Distance( new PointD(0.0d, 0.0d)); return(Math.Cos(curDist * Math.PI / (2.0d * maxL))); }); } else if (kernelType == StandardConvolutionKernels.gauss) { dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, (idx) => { double curDist = (new PointD(idx - (double)kernelHalfWidth, 0.0d)).Distance( new PointD(0.0d, 0.0d)); return(Math.Exp(-curDist * curDist / (2.0d * (maxL * maxL / 9.0d)))); }); } else if (kernelType == StandardConvolutionKernels.flat) { dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, 1.0d); } else if (kernelType == StandardConvolutionKernels.linear) { dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, (idx) => { double curDist = (new PointD(idx - (double)kernelHalfWidth, 0.0d)).Distance( new PointD(0.0d, 0.0d)); return(Math.Max(1.0d - curDist * (1.0d / (double)kernelHalfWidth), 0.0d)); }); } else if (kernelType == StandardConvolutionKernels.bilinear) { dvKernel = DenseVector.Create(2 * kernelHalfWidth + 1, (idx) => { double curDist = (new PointD(idx - (double)kernelHalfWidth, 0.0d)).Distance( new PointD(0.0d, 0.0d)); return(Math.Max(1.0d - curDist * curDist * (1.0d / (double)(kernelHalfWidth * kernelHalfWidth)), 0.0d)); }); } double kernelSum = dvKernel.Values.Sum(); dvKernel.MapInplace(dval => dval / kernelSum); return(dvKernel); }
public static DenseVector ConvKernelAsymmetric(StandardConvolutionKernels kernelType, int kernelWidth = 10, bool centerToTheRight = true) { DenseVector dvKernel = ConvKernel(kernelType, kernelWidth - 1); if (centerToTheRight) { dvKernel = (DenseVector)dvKernel.SubVector(0, kernelWidth); } else { dvKernel = (DenseVector)dvKernel.SubVector(kernelWidth - 1, kernelWidth); } double kernelSum = dvKernel.Values.Sum(); dvKernel.MapInplace(dval => dval / kernelSum); return(dvKernel); }
public static DenseVector Conv(this DenseVector dvSource, StandardConvolutionKernels kernelType, int kernelHalfWidth = 10) { DenseVector dvKernel = ConvKernel(kernelType, kernelHalfWidth); return(dvSource.Conv(dvKernel)); }