通过使用scipy / numpy,在Python中进行图像处理的高通滤波器

|| 我目前正在研究图像处理。在Scipy中,我知道Scipy.signal中有一个中值过滤器。谁能告诉我是否有一个类似于高通滤波器的滤波器? 谢谢     
已邀请:
“高通滤波器”是一个非常通用的术语。有无数个不同的“高通滤波器”可以完成非常不同的工作(例如,如前所述,边缘检测滤波器在技术上是高通滤波器(大多数实际上是带通滤波器),但效果却与之完全不同。您可能已经想到了。) 无论如何,根据您提出的大多数问题,您可能应该查看
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()
    
一种简单的高通滤波器是:
-1 -1 -1
-1  8 -1
-1 -1 -1
Sobel运算符是另一个简单的示例。 在图像处理中,这些类型的过滤器通常称为“边缘检测器”-上次我检查时Wikipedia页面还可以。     
这是我们如何设计with4 design的HPF
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()
原始图片
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()
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()
阻止频谱中的低频
im1 = fp.ifft2(fftpack.ifftshift(F2)).real
plt.figure(figsize=(10,10))
plt.imshow(im1, cmap=\'gray\')
plt.axis(\'off\')
plt.show()
应用HPF后输出图像     
scipy.filter包含大量的通用过滤器。可以将iirfilter类之类的东西配置为产生典型的Chebyshev或Buttworth数字或模拟高通滤波器。     
您可以使用高斯滤镜,因为它比纯净的HPF清晰度高。要使用简单的HPF,可以使用以下代码
import numpy as np
import cv2
from scipy import ndimage

class HPF(object):
    def __init__(self, kernel, image):
        self.kernel = np.array(kernel)
        self.image = image

    def process(self):
        return ndimage.convolve(self.image, self.kernel)


if __name__ == \"__main__\":
    #enter ur image location
    image = cv2.imread(\"images/test2.jpg\", 0)
    kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]
    kernel5x5 = [[-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]]

    hpf1 = HPF(kernel3x3, image)
    hpfimage1 = hpf1.process()
    hpf2 = HPF(kernel5x5, image)
    hpfimage2 = hpf2.process()
    cv2.imshow(\"3x3\",hpfimage1)
    cv2.imshow(\"5x5\",hpfimage2)
    cv2.waitKey()
    cv2.destroyAllWindows()
要使用高斯滤镜,只需将高斯模糊添加到图像中
blurred = cv2.GaussianBlur(image, (11, 11), 0)
然后从原始图像减去它
g_hpf = image - blurred
原始代码摘自:使用Python和OpenCV通过高通滤波器对图像进行锐化     

要回复问题请先登录注册