高通滤波器使用scipy/numpy在python中进行图像处理

Hol*_*ger 17 python numpy image-processing scipy

我目前正在研究图像处理.在Scipy中,我知道Scipy.signal中有一个中值过滤器.谁能告诉我是否有一个类似于高通滤波器的滤波器?

谢谢

Joe*_*ton 45

"高通滤波器"是一个非常通用的术语.有无数个不同的"高通滤波器"可以做很多不同的事情(例如,如前所述,边缘检测滤波器在技术上是高通(大多数实际上是带通)滤波器,但与你可能的效果有很大不同考虑到了.)

无论如何,基于你一直在问的大多数问题,你应该考虑scipy.ndimage而不是scipy.filter,特别是如果你要处理大型图像(ndimage可以在现场执行操作,节省内存).

作为一个基本的例子,展示一些不同的做事方式:

import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
import Image

def plot(data, title):
    plot.i += 1
    plt.subplot(2,2,plot.i)
    plt.imshow(data)
    plt.gray()
    plt.title(title)
plot.i = 0

# Load the data...
im = Image.open('lena.png')
data = np.array(im, dtype=float)
plot(data, 'Original')

# A very simple and very narrow highpass filter
kernel = np.array([[-1, -1, -1],
                   [-1,  8, -1],
                   [-1, -1, -1]])
highpass_3x3 = ndimage.convolve(data, kernel)
plot(highpass_3x3, 'Simple 3x3 Highpass')

# A slightly "wider", but sill very simple highpass filter 
kernel = np.array([[-1, -1, -1, -1, -1],
                   [-1,  1,  2,  1, -1],
                   [-1,  2,  4,  2, -1],
                   [-1,  1,  2,  1, -1],
                   [-1, -1, -1, -1, -1]])
highpass_5x5 = ndimage.convolve(data, kernel)
plot(highpass_5x5, 'Simple 5x5 Highpass')

# Another way of making a highpass filter is to simply subtract a lowpass
# filtered image from the original. Here, we'll use a simple gaussian filter
# to "blur" (i.e. a lowpass filter) the original.
lowpass = ndimage.gaussian_filter(data, 3)
gauss_highpass = data - lowpass
plot(gauss_highpass, r'Gaussian Highpass, $\sigma = 3 pixels$')

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


San*_*Dey 5

以下是我们如何设计 HPF scipy fftpack

from skimage.io import imread
import matplotlib.pyplot as plt
import scipy.fftpack as fp

im = np.mean(imread('../images/lena.jpg'), axis=2) # assuming an RGB image
plt.figure(figsize=(10,10))
plt.imshow(im, cmap=plt.cm.gray)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)

原图

在此处输入图片说明

F1 = fftpack.fft2((im).astype(float))
F2 = fftpack.fftshift(F1)
plt.figure(figsize=(10,10))
plt.imshow( (20*np.log10( 0.1 + F2)).astype(int), cmap=plt.cm.gray)
plt.show()
Run Code Online (Sandbox Code Playgroud)

带有 FFT 的频谱

在此处输入图片说明

(w, h) = im.shape
half_w, half_h = int(w/2), int(h/2)

# high pass filter
n = 25
F2[half_w-n:half_w+n+1,half_h-n:half_h+n+1] = 0 # select all but the first 50x50 (low) frequencies
plt.figure(figsize=(10,10))
plt.imshow( (20*np.log10( 0.1 + F2)).astype(int))
plt.show()
Run Code Online (Sandbox Code Playgroud)

阻止频谱中的低频

在此处输入图片说明

im1 = fp.ifft2(fftpack.ifftshift(F2)).real
plt.figure(figsize=(10,10))
plt.imshow(im1, cmap='gray')
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)

应用 HPF 后的输出图像

在此处输入图片说明