【问题标题】:Why is it that passing an image through a low pass filter yields values higher than the original image?为什么将图像通过低通滤波器会产生高于原始图像的值?
【发布时间】:2020-03-20 10:11:09
【问题描述】:

我有一张混合图像,它是通过将一幅图像的低频与另一幅图像的高频叠加而成的。我试图通过低通滤波器提取低频(两个图像之一)来分离(去混合)这个图像,然后从原始图像中减去它以产生另一个图像(高频率)。

**问题:**当我提取低频时,值都高于原始图像,所以当我从原始图像中减去低频时,剩下的是一堆负值。

有谁知道为什么我的低通滤波器会产生比原始图像更高的频率值?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from numpy.fft import fft2, ifft2, fftshift, ifftshift

# Make Gaussian filter
def makeGaussianFilter(numRows, numCols, sigma, highPass=True):
   centerI = int(numRows/2) + 1 if numRows % 2 == 1 else int(numRows/2)
   centerJ = int(numCols/2) + 1 if numCols % 2 == 1 else int(numCols/2)

   def gaussian(i,j):
      coefficient = np.exp(-1.0 * ((i - centerI)**2 + (j - centerJ)**2) / (2 * sigma**2))
      return 1 - coefficient if highPass else coefficient

   return np.array([[gaussian(i,j) for j in range(numCols)] for i in range(numRows)])

# Filter discrete Fourier transform
def filterDFT(imageMatrix, filterMatrix):
   shiftedDFT = fftshift(fft2(imageMatrix))
   filteredDFT = shiftedDFT * filterMatrix
   return ifft2(ifftshift(filteredDFT))

# Low-pass filter
def lowPass(imageMatrix, sigma):
   n,m = imageMatrix.shape
   return filterDFT(imageMatrix, makeGaussianFilter(n, m, sigma, highPass=False))

# Read in einsteinandwho.png and convert to format that can be displayed by plt.imshow
im3 = mpimg.imread('einsteinandwho.png')
rows = im3.shape[0]
cols = im3.shape[1]
img3 = np.ones((rows, cols, 4))
for i in range(rows):
    for j in range(cols):
        img3[i][j][0:3] = im3[i][j]
        img3[j][j][3] = 1

# Extract low frequencies and convert to format that can be displayed by plt.imshow
lowPassed = np.real(lowPass(im3, 10))
low = np.ones((rows, cols, 4))

for i in range(rows):
    for j in range(cols):
        low[i][j][0:3] = lowPassed[i][j]
        low[j][j][3] = 1

# Remove low frequencies from image
output = img3[:,:,0:3] - low[:,:,0:3]

【问题讨论】:

  • 您不需要为plt.imshow 显示图像的RGBA 版本。其实直接显示灰度图更简单。

标签: python image-processing gaussian lowpass-filter highpass-filter


【解决方案1】:

有谁知道为什么我的低通滤波器产生比原始图像更高的频率值?

请注意像素值和频率值之间的差异。您看到的是像素值更高,而不是频率值!


当我运行您的代码时,我看到高频分量同时具有负像素值和正像素值,而不是所有负值。预计该图像的均值为零。零频率分量(也称为 DC 分量)是设置平均像素值的分量。通过减去低通滤波图像,您将零频率设置为 0,从而将平均像素值设置为 0(低通滤波图像包含零频率的所有功率)。

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 2016-02-13
    • 2021-04-09
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多