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

发布于 2024-11-09 07:15:29 字数 68 浏览 3 评论 0 原文

我目前正在学习图像处理。在SciPy中,我知道scipy.signal中有一个中值滤波器。有没有类似于高通滤波器的滤波器?

I am currently studying image processing. In SciPy, I know there is a median filter in scipy.signal. Is there a filter similar to a high pass filter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滴情不沾 2024-11-16 07:15:29

“高通滤波器”是一个非常通用的术语。有无数种不同的“高通滤波器”,它们可以做非常不同的事情(例如,边缘检测滤波器,如前所述,在技术上是高通(大多数实际上是带通)滤波器,但具有与您可能的效果非常不同的效果)无论如何

,根据您提出的大多数问题,您可能应该研究 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()

在此处输入图像描述

"High pass filter" is a very generic term. There are an infinite number of different "highpass filters" that do very different things (e.g. an edge dectection filter, as mentioned earlier, is technically a highpass (most are actually a bandpass) filter, but has a very different effect from what you probably had in mind.)

At any rate, based on most of the questions you've been asking, you should probably look into scipy.ndimage instead of scipy.filter, especially if you're going to be working with large images (ndimage can preform operations in-place, conserving memory).

As a basic example, showing a few different ways of doing things:

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

enter image description here

) plt.show()

enter image description here

绝情姑娘 2024-11-16 07:15:29

以下是我们如何使用 scipy fftpack 设计 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 后的输出图像

在此处输入图像描述

Here is how we can design a HPF with 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()

Original Image

enter image description here

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()

Frequency Spectrum with FFT

enter image description here

(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()

Block low Frequencies in the Spectrum

enter image description here

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

Output Image after applying the HPF

enter image description here

离去的眼神 2024-11-16 07:15:29

一个简单的高通滤波器是:

-1 -1 -1
-1  8 -1
-1 -1 -1

Sobel 运算符 是另一个简单的示例。

在图像处理中,这类过滤器通常称为“边缘检测器” - 维基百科页面 没问题我上次检查过这一点。

One simple high-pass filter is:

-1 -1 -1
-1  8 -1
-1 -1 -1

The Sobel operator is another simple example.

In image processing these sorts of filters are often called "edge-detectors" - the Wikipedia page was OK on this last time I checked.

謸气贵蔟 2024-11-16 07:15:29

scipy.filter 包含大量通用过滤器。类似于 iirfilter 类可以配置为产生典型的切比雪夫或巴特沃斯数字或模拟高通滤波器。

scipy.filter contains a large number of generic filters. Something like the iirfilter class can be configured to yield the typical Chebyshev or Buttworth digital or analog high pass filters.

夕色琉璃 2024-11-16 07:15:29

您可以使用高斯滤波器,因为它比纯 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 通过高通滤波器进行图像锐化

You can use a Gaussian filter as it gives much sharpness than a pure HPF, for using a simple HPF you can use the following code

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()

To use the Gaussian filter just add the Gaussian blur to your image

blurred = cv2.GaussianBlur(image, (11, 11), 0)

Then minus it from the original image

g_hpf = image - blurred

Original code taken from : Image Sharpening by High Pass Filter using Python and OpenCV

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文