gpt4 book ai didi

image-processing - 是否有计算图像方向场的标准方法?

转载 作者:行者123 更新时间:2023-12-02 01:22:45 25 4
gpt4 key购买 nike

我想在 2D 图像上计算一种方向场,正如这个 photoshop 模型中的(很差)所示。注意:这不是您在微分方程中学到的矢量场。相反,这是沿着人们在计算图像的级别集时会看到的线条绘制的东西。

Example

是否有已知的方法来获得图像的这种类型的方向场(红线)?看起来它几乎表现得像梯度的法线,但这也不完全是这样,因为有些地方梯度为零,我也希望这些位置的方向场。

最佳答案

我找到了一篇关于如何为指纹处理执行此操作的论文,其中详细介绍了他们的结果是可重复的。不幸的是,它位于付费专区后面,但这里适合任何有兴趣并能够访问全文的人:

Systematic methods for the computation of the directional fields and singular points of fingerprints

编辑:根据要求,这里有一个关于如何在上述论文中实现的快速而肮脏的总结(在 Python 中)。

一种简单的方法是对目标像素周围的小正方形邻域中的梯度进行平均,就像问题中图像上的叠加网格一样,然后计算法线。但是,如果您只是简单地平均梯度,则该区域中相反的梯度可能会相互抵消(例如,在计算沿脊的方向时)。因此,通常使用平方梯度进行计算,因为指向相反方向的梯度会对齐。有一个基于原始梯度的平方梯度的巧妙公式。我不会给出推导,但这是公式:



现在,取该区域上的梯度平方和(对角度工作方式的一些分段定义的补偿进行模数)。最后,通过一些反正切魔法,您将获得方向场。

如果在平滑的灰度位图图像上运行以下代码,并将网格尺寸与正确选择的网格尺寸绘制,然后将方向字段o与原始图像一起绘制,您将会看到方向字段或多或少地给出我在我的角度提出的角度原始问题。

from scipy import misc
import numpy as np
import math

# Import the grayscale image
bmp = misc.imread('path/filename.bmp')

# Compute the gradient - VERY important to convert to floats!
grad = np.gradient(bmp.astype(float))

# Set the block size (superimposed grid on the sample image in the question)
blockRadius=5

# Compute the orientation field. Result will be a matrix of angles in [0, \pi), one for each pixel in the original (grayscale) image.

O = np.zeros(bmp.shape)

for x in range(0,bmp.shape[0]):
for y in range(0,bmp.shape[1]):
numerator = 0.
denominator = 0.
for i in range(max(0,x-blockRadius),min(bmp.shape[0],x+blockRadius)):
for j in range(max(0,y-blockRadius),min(bmp.shape[0],y+blockRadius)):
numerator = numerator + 2.*grad[0][i,j]*grad[1][i,j]
denominator = denominator + (math.pow(grad[0][i,j],2.) - math.pow(grad[1][i,j],2.))
if denominator==0:
O[x,y] = 0
elif denominator > 0:
O[x,y] = (1./2.)*math.atan(numerator/denominator)
elif numerator >= 0:
O[x,y] = (1./2.)*(math.atan(numerator/denominator)+math.pi)
elif numerator < 0:
O[x,y] = (1./2.)*(math.atan(numerator/denominator)-math.pi)

for x in range(0,bmp.shape[0]):
for y in range(0,bmp.shape[1]):
if O[x,y] <= 0:
O[x,y] = O[x,y] + math.pi
else:
O[x,y] = O[x,y]

干杯!

关于image-processing - 是否有计算图像方向场的标准方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38928353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com