我怎样才能创建一个自定义的带通滤波器? [英] How can I create a custom band-pass filter?

查看:178
本文介绍了我怎样才能创建一个自定义的带通滤波器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



其中,



现在,我已经实施如下所示:





源图像:



应用卷积:



Trhesholding:


请参阅下面的代码:
$





$ b

  import cv2 
import math
import numpy as np

class内核(对象):
def H_Function(self,Dh,Dv,u,v,centerX,centerY,theta,n):
返回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) ))
$ b $ 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θ(u + self.Ty(centerY,theta))

def Tx(self,中心,theta):
返回中心* 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
(0,size [0])为

在范围内(0,size [1]):
kernel [u] [v] = K.H_Function d h,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(卷积,卷积,0,255,cv2.NORM_MINMAX)
cv2.imwrite(conv.jpg,卷积)
th thresholded = cv2.threshold(卷积,100,255,cv2.THRESH_BINARY)
cv2.imwrite(thresh.jpg,thresholded )


In this research paper, in the Section 4.1(Preprocessing), an equation of a Bandpass filter is given:

Where,

Now, I have implemented this like the following:

https://dotnetfiddle.net/ZhucE2

But, this code produces nothing.

解决方案

You need to create image of your kernel, then to convolve it with your image. fft is used for optimization of convolution for large images. You can use filter2D function to make opencv do everything for you.

Kernel image:

Source image:

Convolution applied:

Trhesholding:

Please see code below:

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)

这篇关于我怎样才能创建一个自定义的带通滤波器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆