我的线性滤波器互相关实现有什么问题?

问题描述

我需要为分配实现线性滤波器和卷积。我没有得到正确的答案(MCQ,所以我的输出不匹配),导师也没有帮助。

对于某些问题,我应该在结果中查找最小/最大值的索引。这些问题有多个,但我都没有得到答案。

我在代码中找不到任何错误,所以我不知道该怎么办。

我应该只写for循环部分。其余的已经在那里。我也有一些说明,相信我已经正确地遵循了。

结果形状的大小为->((((W1-F1 + 2P)/ S)+1)x(((W2-F2 + 2P)/ S)+1),其中'P'为填充长度S是步幅,如果您不了解它们,不用担心,您将在接下来的讲座中学习。现在,我们将使用最简单的设置P = 0,S = 1。参见下一行。

代码

def linear_filter(image,filter_):
    image = np.array(image.convert('L')) 
    image_height,image_width = image.shape[0],image.shape[1]

    filter_ = np.array(filter_.convert('L'))

    filter_height,filter_width = filter_.shape[0],filter_.shape[1]
    
    result_height,result_width = (image_height - filter_height) + 1,(image_width - filter_width) + 1
    result = np.array([[0 for j in range(result_width)] for i in range(result_height)])

    for y in range(result_width):
        for x in range(result_height):
            result[x,y] = np.multiply(filter_,image[x: x + filter_height,y: y + filter_width]).sum()
    
    
    return result

def convolution2D(image,kernel):
    image = np.array(image.convert('L'))
    image_height,image.shape[1]

    kernel = np.array(kernel.convert('L'))

    kernel = np.flipud(np.fliplr(kernel))
    kernelheight,kernelwidth = kernel.shape[0],kernel.shape[1]

    result_height,result_width = (image_height - kernelheight) + 1,(image_width - kernelwidth) + 1
    result = np.array([[0 for j in range(result_width)] for i in range(result_height)])

    for y in range(result_width):
        for x in range(result_height):
            result[x,y] = np.multiply(kernel,image[x: x + kernelheight,y: y + kernelwidth]).sum()
            
    
    return result

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)