Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对六边形采样的图像应用Gabor滤波器?_Python_Image Processing_Gabor Filter - Fatal编程技术网

Python 如何对六边形采样的图像应用Gabor滤波器?

Python 如何对六边形采样的图像应用Gabor滤波器?,python,image-processing,gabor-filter,Python,Image Processing,Gabor Filter,在将方形采样图像转换为六边形采样图像后,我想使用Gabor滤波器作为插值方法 我可以在六边形采样图像中使用普通Gabor滤波器实现吗?还是应该修改代码?如果是,那么对于六边形采样图像,应该修改Gabor函数的哪一部分 我试过实现这个算法,但就是做不好。 这里有一个Gabor滤波的代码 假设六边形网格的普通数据结构,您可能可以对六边形像素图像应用2d过滤器,但您需要创建在适当坐标处计算的过滤器。你看,2d过滤器只是通过计算网格中xy坐标上的函数而生成的值矩阵。因此,5x5 Gabor滤波器矩阵只是

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

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

我试过实现这个算法,但就是做不好。 这里有一个Gabor滤波的代码


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

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

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

然而,六边形像素不是这样排列的。六边形像素的中心按如下方式排列:

因此,为了对此应用过滤器,我们需要在这些点上评估适当的函数。因为库存过滤器是在矩形网格上评估的,所以我们不能使用它们(尽管它们可能会产生看起来合理的结果)

幸运的是,转型相对容易。如果我们假设两行之间的垂直距离为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
函数放在后面的行中,以获得六边形间隔的坐标

例如,有一个python实现来创建Gabor过滤器,如下所示:

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\u gabor
函数(注意,这与
gabor\u fn
函数95%相同):


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

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

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

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

然而,六边形像素不是这样排列的。六边形像素的中心按如下方式排列:

因此,为了对此应用过滤器,我们需要在这些点上评估适当的函数。因为库存过滤器是在矩形网格上评估的,所以我们不能使用它们(尽管它们可能会产生看起来合理的结果)

幸运的是,转型相对容易。如果我们假设两行之间的垂直距离为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
函数放在后面的行中,以获得六边形间隔的坐标

例如,有一个python实现来创建Gabor过滤器,如下所示:

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\u gabor
函数(注意,这与
gabor\u fn
函数95%相同):


您应该能够将生成的内核放入此行
cv2.filter2D(img,cv2.CV\u 8UC3,g\u kernel)

哦,对不起。我应该复习一下我的问题。谢谢你。嗯,你知道我的问题吗,先生??谢谢。我真的怀疑这会这么容易。你只需要自己动手。所有这些图像处理库都在进行矩阵乘法和切片。所有这些都不能转化为十六进制网格,而你只能“修补”。值得注意的是,卷积运算只是在正方形上做一些切片和乘法。说到这里,你的过滤器是什么形状的?你想要六边形还是正方形?@Scott我已经找到了一个将正方形图像转换为六边形的实现。我在那之后被卡住了,因为imsge在变换后需要插值,所以我想对它应用hex-gabor和小波去噪。那么,对六边形采样的图像应用方形过滤器是否合适?谢谢你,非常感谢你的回复。我一直在到处寻找,但我所知道的是我应该自己实现,因为没有六边形图像处理的开放库。六边形网格的方向是什么?(例如,六边形的平边是水平的还是垂直的?)因此,新的问题出现了:(1)如何使用Gabor滤波器进行插值?我从未听说过这样的事。您确定需要Gabor文件管理器吗?也许你把它和别的东西混淆了?(2) “我试过实现这个算法”,但你却发布了从别处复制的代码。你为什么不呢