gpt4 book ai didi

python - 如何将 Gabor 滤波器应用于六边形采样的图像?

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:49 26 4
gpt4 key购买 nike

我想在将方形采样图像转换为六边形采样图像后使用 Gabor 滤波器作为插值方法。

我可以在六角采样图像中使用普通的 Gabor 滤波器实现吗?或者我应该修改代码吗?如果是,那么我应该为六边形采样图像修改 Gabor 函数的哪一部分?

我尝试过实现该算法,但我就是无法正确执行。这是Gabor过滤的代码taken from GitHub .

import numpy as np
import cv2

# cv2.getGaborKernel(ksize, sigma, theta, lambda, gamma, psi, ktype)
# ksize - size of gabor filter (n, n)
# sigma - standard deviation of the gaussian function
# theta - orientation of the normal to the parallel stripes
# lambda - wavelength of the sunusoidal factor
# gamma - spatial aspect ratio
# psi - phase offset
# ktype - type and range of values that each pixel in the gabor kernel can hold

g_kernel = cv2.getGaborKernel((21, 21), 8.0, np.pi/4, 10.0, 0.5, 0, ktype=cv2.CV_32F)

img = cv2.imread('test.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
filtered_img = cv2.filter2D(img, cv2.CV_8UC3, g_kernel)

cv2.imshow('image', img)
cv2.imshow('filtered image', filtered_img)

h, w = g_kernel.shape[:2]
g_kernel = cv2.resize(g_kernel, (3*w, 3*h), interpolation=cv2.INTER_CUBIC)
cv2.imshow('gabor kernel (resized)', g_kernel)
cv2.waitKey(0)
cv2.destroyAllWindows()

最佳答案

假设六边形网格的普通数据结构,您可能可以将二维过滤器应用于六边形像素图像,但您需要创建一个在适当坐标处评估的过滤器。您会看到,2d 滤波器只是通过对网格中的 xy 坐标评估函数而生成的值矩阵。因此,5x5 Gabor 滤波器矩阵只是在 xy 坐标处计算的 Gabor 函数,如下所示:

enter image description here

像素是“等距”的,因此我们可以简单地将网格中每个点之间的距离选择为 x 中的 1 和 y 中的 1,并且我们得到在以下位置计算的 Gabor 函数的中心每个像素。

但是,六边形像素并不是这样排列的。六边形像素的中心排列如下:

enter image description here

因此,为了对此应用过滤器,我们需要在这些点评估适当的函数。由于库存过滤器已在矩形网格上进行评估,因此我们无法使用它们(尽管它们会产生看起来可能合理的东西)。

幸运的是,转变相对容易。如果我们假设两行之间的垂直距离为 1,那么坐标几乎只是一个np.arange

import numpy as np
import matplotlib.pyplot as plt

ALTERNATE_ROW_SHIFT = 0+np.sqrt(3)/3 # every other row is "offset" by half a hexagon. If the sides are len 2/3, the shift is root 3 over 3

def hex_grid(rect_grid):
rect_grid = np.copy(rect_grid)
rect_grid[0,:,1::2] += ALTERNATE_ROW_SHIFT
return rect_grid

如果您有权访问创建过滤器的函数,通常会有一些逻辑创建一个矩形网格,随后在该网格上评估该函数。将 hex_grid 函数放在后面的行中即可获取六边形间隔的坐标。

例如,the wikipedia page on Gabor filters有一个用于创建 Gabor 过滤器的 python 实现,如下所示:

def gabor_fn(sigma, theta, Lambda, psi, gamma):
sigma_x = sigma
sigma_y = float(sigma) / gamma

# Bounding box
nstds = 3 # Number of standard deviation sigma
xmax = max(abs(nstds * sigma_x * np.cos(theta)), abs(nstds * sigma_y * np.sin(theta)))
xmax = np.ceil(max(1, xmax))
ymax = max(abs(nstds * sigma_x * np.sin(theta)), abs(nstds * sigma_y * np.cos(theta)))
ymax = np.ceil(max(1, ymax))
xmin = -xmax
ymin = -ymax

(y,x) = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1))

# Rotation
x_theta = x * np.cos(theta) + y * np.sin(theta)
y_theta = -x * np.sin(theta) + y * np.cos(theta)

gb = np.exp(-.5 * (x_theta ** 2 / sigma_x ** 2 + y_theta ** 2 / sigma_y ** 2)) * np.cos(2 * np.pi / Lambda * x_theta + psi)
return gb

注意涉及np.meshgrid的行。这将创建一个间距为 1 的矩形网格,用于后续行。我们可以简单地转换这些坐标来创建一个新的 hex_gabor 函数(请注意,这与 gabor_fn 代码 95% 相同):

def hex_gabor_fn(sigma, theta, Lambda, psi, gamma):
sigma_x = sigma
sigma_y = float(sigma) / gamma

# Bounding box
nstds = 3 # Number of standard deviation sigma
xmax = max(abs(nstds * sigma_x * np.cos(theta)), abs(nstds * sigma_y * np.sin(theta)))
xmax = np.ceil(max(1, xmax))
ymax = max(abs(nstds * sigma_x * np.sin(theta)), abs(nstds * sigma_y * np.cos(theta)))
ymax = np.ceil(max(1, ymax))
xmin = -xmax
ymin = -ymax

yx = np.meshgrid(np.arange(ymin, ymax + 1), np.arange(xmin, xmax + 1))
(y,x) = hex_grid(yx)

# Rotation
x_theta = x * np.cos(theta) + y * np.sin(theta)
y_theta = -x * np.sin(theta) + y * np.cos(theta)

gb = np.exp(-.5 * (x_theta ** 2 / sigma_x ** 2 + y_theta ** 2 / sigma_y ** 2)) * np.cos(2 * np.pi / Lambda * x_theta + psi)
return gb

if __name__ == "__main__":
g = gabor_fn(4,np.pi/4,4,0,2)
hg = hex_gabor_fn(4,np.pi/4,4,0,2)
plt.imshow(g)
plt.show()
plt.imshow(hg)
plt.show()

您应该能够将生成的内核放入此行cv2.filter2D(img, cv2.CV_8UC3, g_kernel)

关于python - 如何将 Gabor 滤波器应用于六边形采样的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55013954/

26 4 0
文章推荐: html - 格式化表格中的列
文章推荐: html - 将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com