【问题标题】:Convolution on PythonPython 上的卷积
【发布时间】:2021-04-05 13:01:12
【问题描述】:

所以我想计算傅里叶变换图像和掩码之间的卷积。

from scipy import fftpack
import numpy as np
import imageio
from PIL import Image, ImageDraw
import cv2
import matplotlib.pyplot as plt
import math
from scipy.ndimage.filters import convolve


input_image = Image.open('....image....')
input_image=np.array(input_image)
M,N = input_image.shape[0],input_image.shape[1]

FT_img = fftpack.fftshift(fftpack.fft2(input_image))

n = 2; # order value can change this value accordingly 


D0 = 60; # cut-off frequency can change this value accordingly 

# Designing filter 

u = np.arange(0, M)
idx = u > M/2
u[idx] = u[idx] - M
v = np.arange(0, N)
idy = v > N/2
v[idy] = v[idy] - N

V,U = np.meshgrid(v,u)

# Calculating Euclidean Distance 
D=np.linalg.norm(V-U) 

# determining the filtering mask 
H = 1/(1 + (D0/D)**(2*n)); 

# Convolution between the Fourier Transformed image and the mask 
G = convolve(H, FT_img)

当我运行此代码 sn-p 时,最后一行出现“运行时错误:过滤器权重数组的形状不正确。”错误。我的理解是H 是浮点数,FT_img 是数组,所以我不能对这些进行卷积。但我不知道如何解决。 我该如何解决这个问题?

【问题讨论】:

  • 时域的卷积变成频域的矩阵乘法,那么你应该在哪个域做卷积??
  • @Bilal,我应该执行高通巴特沃斯滤波器,所以我假设它在频域中?
  • @Bilal,感谢您的回答,但老实说,我不知道如何为 (u,v) 计算它们以创建数组。

标签: python arrays numpy image-processing convolution


【解决方案1】:

计算距离 D,并为每个 (u, v) 过滤 H,这将产生一个与输入图像大小相同的数组,将该数组(H 过滤器)与傅里叶域中的图像相乘将等效于卷积时域,结果如下:

import numpy as np
import cv2
import matplotlib.pyplot as plt

# Read Image as Grayscale
img = cv2.imread('input.png', 0)

# Designing filter 
#------------------------------------------------------
def butterworth_filter(shape, n=2, D0=60):
    '''
    n = 2; # order value can change this value accordingly 
    D0 = 60; # cut-off frequency can change this value accordingly 
    '''
    M, N = shape
    # Initialize filter with zeros
    H = np.zeros((M, N))

    # Traverse through filter
    for u in range(0, M):
        for v in range(0, N):
            # Get euclidean distance from point D(u,v) to the center
            D_uv = np.sqrt((u - M / 2) ** 2 + (v - N / 2) ** 2)
            # determining the filtering mask 
            H[u, v] = 1/(1 + (D0/D_uv)**(2*n))
    return H
#-----------------------------------------------------

f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
phase_spectrumR = np.angle(fshift)
magnitude_spectrum = 20*np.log(np.abs(fshift))
 
# Generate Butterworth Filter
H = butterworth_filter(img.shape)
# Convolution between the Fourier Transformed image and the mask
G = H * fshift
# Obtain the Result
result = np.abs(np.fft.ifft2(np.fft.ifftshift((G))))


plt.subplot(222)
plt.imshow(img, cmap='gray')
plt.title('Original')
plt.axis('off')

plt.subplot(221)
plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('magnitude spectrum')
plt.axis('off')

plt.subplot(223)
plt.imshow(H, "gray") 
plt.title("Butterworth Filter")
plt.axis('off')

plt.subplot(224)
plt.imshow(result, "gray") 
plt.title("Result")
plt.axis('off')

plt.show()

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2013-06-08
    • 2017-01-06
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多