计算2D输入图像的2D卷积时断言误差

发布于 2025-01-25 16:47:21 字数 1258 浏览 2 评论 0原文

计算2D图像的2D卷积时,我的断言错误。我正在努力分配ij的正确范围,然后计算farter查看的关键字。这给我带来了断言错误。有人可以告诉我我犯了什么错误?在使用yart> tart关键字时,我应该评估2D卷积? ij的正确范围应该是什么?

import numpy as np


def convolve_2d(image, kernel_size=(3, 3)):
    """ yields (view, i, j) of a 2D convolution of
            the 2D input image

    """
    h, w = image.shape[0:2]  # it should work for gray input images, as well as for colored
    for i in range(0, 2):  
        for j in range(0, 2):  
            # view is the current "kernel" wide view of the image in the convolution
            view = image.copy()  

            yield view, i, j


res = list(convolve_2d(
    np.array([
        [11, 12, 13, 14],
        [21, 22, 23, 24],
        [31, 32, 33, 34],
        [41, 42, 43, 44]
    ]),
    kernel_size=(3, 3)
)
)

assert (len(res) == 4)
assert ([x[0].sum() for x in res] == [198, 207, 288, 297])
assert ((res[0][0] == [[11, 12, 13], [21, 22, 23], [31, 32, 33]]).all())
assert ((res[1][0] == [[12, 13, 14], [22, 23, 24], [32, 33, 34]]).all())
assert ((res[2][0] == [[21, 22, 23], [31, 32, 33], [41, 42, 43]]).all())
assert ((res[3][0] == [[22, 23, 24], [32, 33, 34], [42, 43, 44]]).all())

I am having assertion error while computing 2D convolution of an 2d image. I am struggling to assign correct range of i and j and then computing Yield keyword for view. It's giving me assertion error. Can somebody tell me what mistake I am committing and what should I evaluate 2D convolution while using Yield keyword? what should be the correct range of i and j?

import numpy as np


def convolve_2d(image, kernel_size=(3, 3)):
    """ yields (view, i, j) of a 2D convolution of
            the 2D input image

    """
    h, w = image.shape[0:2]  # it should work for gray input images, as well as for colored
    for i in range(0, 2):  
        for j in range(0, 2):  
            # view is the current "kernel" wide view of the image in the convolution
            view = image.copy()  

            yield view, i, j


res = list(convolve_2d(
    np.array([
        [11, 12, 13, 14],
        [21, 22, 23, 24],
        [31, 32, 33, 34],
        [41, 42, 43, 44]
    ]),
    kernel_size=(3, 3)
)
)

assert (len(res) == 4)
assert ([x[0].sum() for x in res] == [198, 207, 288, 297])
assert ((res[0][0] == [[11, 12, 13], [21, 22, 23], [31, 32, 33]]).all())
assert ((res[1][0] == [[12, 13, 14], [22, 23, 24], [32, 33, 34]]).all())
assert ((res[2][0] == [[21, 22, 23], [31, 32, 33], [41, 42, 43]]).all())
assert ((res[3][0] == [[22, 23, 24], [32, 33, 34], [42, 43, 44]]).all())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文