首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像/高斯滤波器阵列上的核矩阵3x3

图像/高斯滤波器阵列上的核矩阵3x3
EN

Stack Overflow用户
提问于 2017-07-08 03:20:55
回答 1查看 6K关注 0票数 2

我正在尝试实现一个高斯滤波器。为此,我使用一个内核3x3和一个image.The问题的数组,为数组的每个i,j元素定义一个子矩阵3x3。我在代码中写了详细信息。

代码语言:javascript
复制
import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt


imagen = scipy.misc.imread("C:\\Users\\Reymi\\Downloads\\imagen.png") #importing image of original size (1929, 1280)

imagen_real = scipy.pad(array=imagen, pad_width=[1, 1], mode='constant', constant_values=0) #add 1 column and 1 row of zeros to avoid the kernel of going outside the array. size is (1931, 1282)

imagen_nueva = np.empty((1931, 1282)) #the new image. the same size as the image I will filter

(dim_x,dim_y)=np.shape(imagen_real)


ker1 = np.array([[1/16, 1/8, 1/16],   #3x3 kernel
                [1/8, 1/4, 1/8],
                [1/16, 1/8, 1/16]])

def multiplicar_entero():

    global imagen_nueva
    for i in range(1,dim_x): #the range starts from 1 to avoid the column and row of zeros, and ends before the last col and row of zeros
        for j in range(1,dim_y):
            imagen_entry = np.empty((3, 3))      #Main problem here: how do I define a 3x3 matrix for each entry?
            valor = np.sum(imagen_entry*ker1)    #Matrix 3x3 is filled with the elements around each [i, j] entry of the array
            imagen_real[i, j] = valor
            imagen_nueva = np.append(imagen_real[i, j], (1931, 1282)) #This is supposed to each new [i, j] entry to the new image

print("La imagen con el blur effect es la siguiente:\n")

multiplicar_entero()   #calls function


plt.imshow(imagen_nueva)  #Show new image
plt.gray()
plt.show()

抱歉密码太长了。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-07-08 23:47:40

您的代码有几个问题如下:

  • 你使用的是一个我们无法访问的图像。在示例中始终使用免费可用的图像,这样我们就可以运行代码。我这里用的是scipy.misc.ascent
  • 除非必须使用全局变量,否则不要使用全局变量。
  • 始终用英文编码,并带有英文变量名和英文注释。这样的评论就容易多了。

我在下面对您的代码做了一些修正,我想您可以看到我是如何解决问题的。具体来说,您希望使用:索引,它允许提取数组的子集并使用array[i, j]分配结果:

代码语言:javascript
复制
import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt

imagen = scipy.misc.ascent()  # Freely available image

(dim_x, dim_y) = np.shape(imagen)

ker1 = np.array([[1/16, 1/8, 1/16],   #3x3 kernel
                [1/8, 1/4, 1/8],
                [1/16, 1/8, 1/16]])

def multiplicar_entero(imagen):
    imagen_nueva = np.zeros(imagen.shape) #the new image. the same size as the image I will filter
    for i in range(1,dim_x-1): #the range starts from 1 to avoid the column and row of zeros, and ends before the last col and row of zeros
        for j in range(1,dim_y-1):
            imagen_entry = imagen[i-1:i+2, j-1:j+2]
            valor = np.sum(imagen_entry*ker1)    #Matrix 3x3 is filled with the elements around each [i, j] entry of the array
            imagen_nueva[i, j] = valor
    return imagen_nueva

imagen_nueva = multiplicar_entero(imagen)

plt.imshow(imagen_nueva)  #Show new image
plt.gray()
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44982157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档