Python 循环后变量发生变化

Python 循环后变量发生变化,python,loops,Python,Loops,我有以下代码: """ Parameters ---------- image : numpy.ndarray(dtype=np.uint8) A grayscale image represented in a numpy array. kernel : numpy.ndarray A kernel represented in a numpy array of size (k, k) where k is an odd

我有以下代码:

    """
    Parameters
    ----------
    image : numpy.ndarray(dtype=np.uint8)
        A grayscale image represented in a numpy array.

    kernel : numpy.ndarray
        A kernel represented in a numpy array of size (k, k) where k is an odd
        number strictly greater than zero.

    Returns
    -------
    output : numpy.ndarray(dtype=np.float64)
        The output image. The size of the output array should be smaller than
        the original image size by k-1 rows and k-1 columns, where k is the
        size of the kernel.
    """


    #kernel is of shape (K x K), square
    K = kernel.shape[0]

    #create a result array with K+1 rows and columns
    result = np.zeros([image.shape[0] - K + 1, image.shape[1] - K + 1], dtype=np.float64)

    #loop through the image
    for r in range(result.shape[0]):
        for c in range(result.shape[1]):
            avg = 0 #running average for this image pixel

            #loop through the kernel
            for kr in range(kernel.shape[0]):
                for kc in range(kernel.shape[1]):
                    avg += image[r][c] * kernel[kr][kc]
                    print avg #values are as expected

            print avg #values are rounded (i.e. no decimals)
            result[r][c] = avg

    return result
我正在努力表演。我不明白为什么我的数字被莫名其妙地四舍五入。我对Python有些陌生,所以可能我做错了什么

我将感谢所有的帮助

编辑:我希望我的输出与以下cv2函数调用的输出相同:

GAUSSIAN_KERNEL = np.array([[  1,  4,  6,  4,  1],
                                    [  4, 16, 24, 16,  4],
                                    [  6, 24, 36, 24,  6],
                                    [  4, 16, 24, 16,  4],
                                    [  1,  4,  6,  4,  1]], dtype=np.float64) / 256.

        N = GAUSSIAN_KERNEL.shape[0] // 2

        tested = a4.crossCorrelation2D(self.testImage, GAUSSIAN_KERNEL)

        goal = cv2.filter2D(self.testImage, cv2.CV_64F, GAUSSIAN_KERNEL)[N:-N, N:-N]
        assert np.testing.assert_array_equal(tested, goal, "Arrays were not equal")

请注意,在这个从代码中提取的循环中:

avg = 0
for kr in range(kernel.shape[0]):
    for kc in range(kernel.shape[1]):
        avg += image[r][c] * kernel[kr][kc]
平均值总是加在图像[r][c]上,因为您正在

image[r][c] * kernel[0][0] + image[r][c] * kernel[0][1] + image[r][c] * kernel[0][2]...
这等于

image[r][c] * (kernel[0][0] + kernel[0][1] + kernel[0][2]...)
image[r][c] * sum-of-all-kernel-elements
image[r][c] * 1.0
这等于

image[r][c] * (kernel[0][0] + kernel[0][1] + kernel[0][2]...)
image[r][c] * sum-of-all-kernel-elements
image[r][c] * 1.0
这等于

image[r][c] * (kernel[0][0] + kernel[0][1] + kernel[0][2]...)
image[r][c] * sum-of-all-kernel-elements
image[r][c] * 1.0
正确的循环应该是这样的:

for r in range(result.shape[0]):
    for c in range(result.shape[1]):
        avg = 0.0
        for kr in range(kernel.shape[0]):
            for kc in range(kernel.shape[1]):
                avg += kernel[kr][kc] * image[r+kr][c+kc]
        result[r][c] = np.uint(avg)

我还没有测试我的代码,但我认为您可能只需要一些小的调整

预期输出v/s是多少?您得到了什么?另外,这里存储的变量avg是什么?你是说打印平均值而不是打印平均值吗?哎呀,修正了。这一切都应该是avgWell,那么我也不知道如何做循环谢谢你提醒我这件事。如果你有什么想法,请告诉我——如果我发现了,我一定会发一些东西。k应该是什么?如果你的意思是我的OP中的K,那么所有这一切只是为了某些原因使每个值都相同,K是K,对不起,内核的宽度。。。顺便说一下,我注意到我的代码中还有一些错误。。。请在几分钟后查看我的更新!。。。我想我错过了一个细节。我应该在图像[r+kr-intK-1/2][c+kc-intK-1/2]中以固定的数量递增这些值。我想这些数字只是换了个位置。请再给我几分钟。