/// <summary> /// x-direction convolute with an 1D double kernel /// mirror expand source matrix before convolution /// </summary> /// <param name="kernel">1D double kernel</param> /// <returns></returns> private PZMath_matrix ConvoluteRowMirrorExpand(PZMath_vector kernel) { // kernel info int kernelSize = kernel.Size; int halfKernelSize = (kernelSize - 1) / 2; // matrix info int height = row; int width = col; // expand src matrix PZMath_matrix expandSrc = this.BoundMirrorExpand(kernelSize, kernelSize); PZMath_matrix expandDst = new PZMath_matrix(expandSrc); // spatial convolute int xStart = halfKernelSize; int xEnd = width + halfKernelSize; int yStart = halfKernelSize; int yEnd = height + halfKernelSize; for (int x = xStart; x < xEnd; x++) { for (int y = yStart; y < yEnd; y++) { double localSum = 0.0; // inside kernel for (int k = -1 * halfKernelSize; k <= halfKernelSize; k ++) { localSum += expandSrc[y, x + k] * kernel[k + halfKernelSize]; } expandDst[y, x] = localSum; } } // shrink dst matrix PZMath_matrix dstMatrix = expandDst.BoundMirrorShrink(kernelSize, kernelSize); return dstMatrix; }
/// <summary> /// kernel spatial convolution, mirror expand before convolution /// </summary> /// <param name="kernel"></param> /// <returns></returns> private PZMath_matrix Convolute2DMirrorExpand(PZMath_matrix kernel) { // kernel info int kernelHeight = kernel.RowCount; int kernelWidth = kernel.ColumnCount; int halfKernelHeight = (kernelHeight - 1) / 2; int halfKernelWidth = (kernelHeight - 1) / 2; // matrix info int height = row; int width = col; // expand src matrix PZMath_matrix expandSrc = this.BoundMirrorExpand(kernelHeight, kernelWidth); PZMath_matrix expandDst = new PZMath_matrix(expandSrc); // spatial convolute //int xStart = halfKernelWidth - 1; //int xEnd = width - halfKernelWidth; //int yStart = halfKernelHeight - 1; //int yEnd = height - halfKernelHeight; int xStart = halfKernelWidth; int xEnd = width + halfKernelWidth; int yStart = halfKernelHeight; int yEnd = height + halfKernelHeight; for (int x = xStart; x < xEnd; x++) { for (int y = yStart; y < yEnd; y++) { double localSum = 0.0; // inside kernel for (int kx = -1 * halfKernelWidth; kx <= halfKernelWidth; kx++) { for (int ky = -1 * halfKernelHeight; ky <= halfKernelHeight; ky++) { //localSum += expandSrc[y, x] * kernel[ky + halfKernelHeight, kx + halfKernelWidth]; localSum += expandSrc[y + ky, x + kx] * kernel[ky + halfKernelHeight, kx + halfKernelWidth]; } } expandDst[y, x] = localSum; } } // shrink dst matrix PZMath_matrix dstMatrix = expandDst.BoundMirrorShrink(kernelHeight, kernelWidth); return dstMatrix; }