def getOutputShape(inputShape, kernelShape, padding=0, stride=1):
    """
    Given an input shape, kernel shape, padding and stride, return the
    dimensions of the convoled image. Will throw an error if the
    dimensionalities do not match.

    The inputShape can have 2 or 3 dimensions.
    The kernelShape can 3 or 4 dimensions

    """
    assert kernelShape[0] == kernelShape[1]

    filterSize = kernelShape[0]
    outputWidth = (inputShape[1] - filterSize + 2 * padding) / stride + 1
    outputHeight = (inputShape[0] - filterSize + 2 * padding) / stride + 1
    outputWidth = int(outputWidth)
    outputHeight = int(outputHeight)

    if len(kernelShape) == 4:
        outputDepth = kernelShape[3]
    else:
        outputDepth = 1

    return (outputHeight, outputWidth, outputDepth)