如何创建自定义带通滤波器?

时间:2016-06-11 23:22:41

标签: image-processing filter

this research paper中,在4.1节(预处理)中,给出了带通滤波器的等式:

enter image description here

其中,

enter image description here

现在,我已经实现了以下内容:

https://dotnetfiddle.net/ZhucE2

但是,这段代码什么也没有产生。

2 个答案:

答案 0 :(得分:8)

您需要创建内核的图像,然后将其与图像进行卷积。 fft用于优化大图像的卷积。你可以使用filter2D函数让opencv为你做所有事情。

内核映像:
enter image description here
来源图片:
Source image
应用卷积:
enter image description here
Trhesholding:
enter image description here

请参阅以下代码:

import cv2
import math
import numpy as np

class Kernel(object):
    def H_Function(self, Dh, Dv, u, v, centerX, centerY, theta, n):
        return 1 / (1 + 0.414 * math.sqrt(math.pow(self.U_Star(u, centerX, centerY, theta) / Dh + self.V_Star(v, centerX, centerY, theta) / Dv, 2 * n)))

    def U_Star(self, u, centerX, centerY, theta):
        return math.cos(theta) * (u + self.Tx(centerX, theta)) + math.sin(theta) * (u + self.Ty(centerY, theta))

    def V_Star(self, u, centerX, centerY, theta):
        return (-math.sin(theta)) * (u + self.Tx(centerX, theta)) + math.cos(theta) * (u + self.Ty(centerY, theta))

    def Tx(self, center, theta):
        return center * math.cos(theta)

    def Ty(self, center, theta):
        return center * math.sin(theta)

K = Kernel()

size = 40, 40
kernel = np.zeros(size, dtype=np.float)
Dh=2
Dv=2
centerX = -size[0] / 2
centerY = -size[1] / 2
theta=0.9
n=4

for u in range(0, size[0]):
    for v in range(0, size[1]):
        kernel[u][v] = K.H_Function(Dh, Dv, u, v, centerX, centerY, theta, n) 
kernelNorm = np.copy(kernel)
cv2.normalize(kernel, kernel, 1.0, 0, cv2.NORM_L1)
cv2.normalize(kernelNorm, kernelNorm, 0, 255, cv2.NORM_MINMAX)
cv2.imwrite("kernel.jpg", kernelNorm)

imgSrc = cv2.imread('src.jpg',0)

convolved = cv2.filter2D(imgSrc,-1,kernel)
cv2.normalize(convolved, convolved, 0, 255, cv2.NORM_MINMAX)
cv2.imwrite("conv.jpg", convolved)
th, thresholded = cv2.threshold(convolved, 100, 255, cv2.THRESH_BINARY)
cv2.imwrite("thresh.jpg", thresholded)

答案 1 :(得分:3)

没有必要将过滤器存储在数组中。您可以对u,v组件执行双循环,以获得评估FFT的值,计算每对的滤波器响应H(u,v)并将其乘以相应的数组元素。在对已修改的数组进行反向转换后,您将获得已过滤的图像。