- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在将方形采样图像转换为六边形采样图像后使用 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 函数,如下所示:
像素是“等距”的,因此我们可以简单地将网格中每个点之间的距离选择为 x 中的 1 和 y 中的 1,并且我们得到在以下位置计算的 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
函数放在后面的行中即可获取六边形间隔的坐标。
例如,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/
所以我试图增强我的图像的对比度,我发现一位绅士通过在线 Gamma 校正来做到这一点,代码如下: (im/255).^0.45*255 据我了解,1/gammavalue = 0.45,其中 gamm
我有一个包含简单时间序列数据的向量(从 deSolve 矩阵中提取),用于测试目的可以是: x x r for (n in 2:length(x)) r[n] (r) [1] NA 1 1
我有这段实现 Prewitt 边缘检测的代码。我需要做的是只用一个缓冲区来实现它,也就是说,我不会创建图像的拷贝,而是编辑原始图像。所以如果我想改变值 78 的像素,我不能把新值,例如100,直到所有
我想制作一个 FIR 滤波器。我有一个系数数组 (buffer[size]) 和一个数据数组 (filter[size_filter])。我必须在两个数组之间进行卷积: for(j = 0;j < s
我正在尝试制作 IIR 滤波器。我做了FIR滤波器,但是我觉得IIR比FIR难。 我认为 IIR 与 FIR 类似,但它让我感到困惑。 我觉得过滤器是这样的 FIR : y(n) = b0(x[n])
我想在 Python 中通过窗口创建一个基本的高通 FIR 滤波器。 我的代码在下面并且是故意惯用的 - 我知道你可以(很可能)用 Python 中的一行代码完成它,但我正在学习。我使用了一个带有矩形
我正在尝试用树莓派创建一个相机来检测在走廊中移动的人(这里我假设只有移动的东西是人),并识别那些在该区域花费太多时间的人(使用计时器),我使用背景减法来检测运动并尝试使用基于相关性的跟踪器(例如 MO
我正在研究用于特征提取的超像素。我已经成功地将超像素功能应用于图像。 A = imread('kobi.png'); [L,N] = superpixels(A,5); figure BW = bou
你好 我需要在应用中使用这个 Kolmogorov 过滤器。您将一些测量数据放入其中,并使用过滤器对其进行一些平滑处理。我试着用“nchoosek”来做,但是当我尝试为 50 或更多的 I 做这件事时
我正在尝试在具有静态掩码 5x5 并在 applyFilter() 函数中进行卷积编码的图像上实现 LoG 过滤器。然而,无论我使用什么面具,我都会得到奇怪的结果。保存图像而不通过函数传递它是有效的,
我已经在 Haskell 中实现了一个 FIR 滤波器。我不太了解 FIR 滤波器,我的代码很大程度上基于现有的 C# 实现。因此,我觉得我的实现有太多的 C# 风格,而不是真正的 Haskell 风
我需要制作一个简单的带通音频滤波器。现在我使用了这个简单的 C++ 类:http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass
CUDA NPP 库支持使用 nppiFilter_8u_C1R 命令过滤图像,但不断出现错误。我可以毫无问题地启动并运行 boxFilterNPP 示例代码。 eStatusNPP = nppiFi
我是 OpenCV 和 gabor 过滤器的新手,只想获得这样的 gabor 小波: 我在 Java 中使用这个 OpenCV 代码: double sigma_bar = 40; double th
我正在使用 FIR 滤波器对音频进行过采样。这是一个简单的典型窗口 sinc,即一个被截断和窗口化的 sinc 函数。像往常一样,它需要过去和“ future ”的样本才能工作。实际上,这意味着音频输
目前我正在尝试实现 FIR 低通滤波器。 FIR 系数在 MATLAB 中计算。现在我需要用 C++ 实现 FIR 算法。 我将一个类定义为过滤器,将 FIR 的一个函数定义为: double * F
我有一个用 C 语言实现 FIR 滤波器的家庭作业,我想知道您是否认为我理解正确。我认为解决问题的程序是: #include float FIRfloats[5]; void floatFIR(fl
我希望对图像的每条水平线应用频域滤波器,例如低通或带通。这可能使用 opencv 吗? 最佳答案 我认为您需要详细说明您的问题。也许,举一些具体的例子。 如果我将您的问题解释为: 你有一张 10 x
我的问题与 A. Levy 的解释相关: Analyze audio using Fast Fourier Transform 如何在这些复数上生成带通滤波器... [-636.00000000 +0
FIR 滤波器有一个算法,但它是 float : FIR filter implementation in C programming 如果我想要一个符合此规范的定点算法,我该怎么做? the FIR
我是一名优秀的程序员,十分优秀!