为什么通过低通滤波器传递图像会产生比原始图像更高的值?

2024-10-01 09:34:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个混合图像,它是由一个图像的低频与另一个图像的高频叠加而成的。我试图通过低通滤波器分离(去杂交)这幅图像,提取低频(两幅图像中的一幅),然后从原始图像中减去低频,得到另一幅图像(高频)

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

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

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]

Tags: in图像importfordefnprangesigma
1条回答
网友
1楼 · 发布于 2024-10-01 09:34:31

Does anyone know why my low pass filter is yielding higher frequency values than the original image?

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


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

相关问题 更多 >