小编Rod*_*lle的帖子

Python中的图像渐变矢量场

我试图使用Python 获取图像的渐变矢量场(类似于这个matlab问题).

这是原始图片: http:/ /dcc.fceia.unr.edu.ar/~rbaravalle/gradient/test.png

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import Image
from PIL import ImageFilter

I = Image.open('test.png').transpose(Image.FLIP_TOP_BOTTOM)
I = I.filter(ImageFilter.BLUR)
p = np.asarray(I)
w,h = I.size
y, x = np.mgrid[0:h:500j, 0:w:500j]

dy, dx = np.gradient(p)
skip = (slice(None, None, 3), slice(None, None, 3))

fig, ax = plt.subplots()
im = ax.imshow(I, extent=[x.min(), x.max(), y.min(), y.max()])
ax.quiver(x[skip], y[skip], dx[skip], dy[skip])

ax.set(aspect=1, title='Quiver Plot')
plt.show()
Run Code Online (Sandbox Code Playgroud)

这是结果: http://dcc.fceia.unr.edu.ar/~rbaravalle/gradient/result.png

问题是向量似乎是不正确的.放大图像时,此点变得更加清晰:

http://dcc.fceia.unr.edu.ar/~rbaravalle/gradient/result2.png

为什么有些向量指向中心的预期,而其他向量则没有?

也许调用的结果有问题np.gradient

python gradient numpy image image-processing

9
推荐指数
1
解决办法
6816
查看次数

标签 统计

gradient ×1

image ×1

image-processing ×1

numpy ×1

python ×1