是否有适用于 Python 的图像相位相关库?

发布于 2024-08-31 04:54:11 字数 1539 浏览 13 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗化ㄋ丶相逢 2024-09-07 04:54:11

相位相关性如http://en.wikipedia.org/wiki/Phase_correlation所述,摘自https://github.com/michaelting/Phase_Correlation/blob/master/phase_corr.py

def phase_correlation(a, b):
    G_a = np.fft.fft2(a)
    G_b = np.fft.fft2(b)
    conj_b = np.ma.conjugate(G_b)
    R = G_a*conj_b
    R /= np.absolute(R)
    r = np.fft.ifft2(R).real
    return r

这是一个示例:我们拍摄两个相似的图像,但相位不同,并绘制相位相关性(黑色图像,在适当的相位差处有一个白点)。

from scipy import misc
from matplotlib import pyplot
import numpy as np

#Get two images with snippet at different locations
im1 = np.mean(misc.face(), axis=-1) #naive colour flattening  

im2 = np.zeros_like(im1)    
im2[:200,:200] = im1[200:400, 500:700]

corrimg = phase_correlation(im1, im2)
r,c = np.unravel_index(corrimg.argmax(), corrimg.shape)

pyplot.imshow(im1)
pyplot.plot([c],[r],'ro')
pyplot.show()

pyplot.imshow(im2)
pyplot.show()

pyplot.figure(figsize=[8,8])
pyplot.imshow(corrimg, cmap='gray')

pyplot.show()

输入图像描述这里

Phase correlation as described by http://en.wikipedia.org/wiki/Phase_correlation, taken from https://github.com/michaelting/Phase_Correlation/blob/master/phase_corr.py.

def phase_correlation(a, b):
    G_a = np.fft.fft2(a)
    G_b = np.fft.fft2(b)
    conj_b = np.ma.conjugate(G_b)
    R = G_a*conj_b
    R /= np.absolute(R)
    r = np.fft.ifft2(R).real
    return r

Here is an example: We take two similar images, but of different phases and plot the phase correlation (a black image with a single white dot at the appropriate phase difference).

from scipy import misc
from matplotlib import pyplot
import numpy as np

#Get two images with snippet at different locations
im1 = np.mean(misc.face(), axis=-1) #naive colour flattening  

im2 = np.zeros_like(im1)    
im2[:200,:200] = im1[200:400, 500:700]

corrimg = phase_correlation(im1, im2)
r,c = np.unravel_index(corrimg.argmax(), corrimg.shape)

pyplot.imshow(im1)
pyplot.plot([c],[r],'ro')
pyplot.show()

pyplot.imshow(im2)
pyplot.show()

pyplot.figure(figsize=[8,8])
pyplot.imshow(corrimg, cmap='gray')

pyplot.show()

enter image description here

只怪假的太真实 2024-09-07 04:54:11

使用 scipy 这应该是一行(尽管你可能可以避免使用 ndimage 包)

from scipy.fftpack import fftn, ifftn
corr = (ifftn(fftn(a)*ifftn(b))).real

假设你已经成功地将原始图像读入 numpy 数组 a & 中。 b.如果是 2D 图像,mayavi 可能有点大材小用,而且使用 matplotlib 可能比 chaco 更容易。如果使用 matplotlib,你可以用

from pylab import *
corr = (ifftn(fftn(a)*ifftn(b))).real
imshow(corr)

using scipy this should be a one-liner (although you can probably avoid the ndimage package)

from scipy.fftpack import fftn, ifftn
corr = (ifftn(fftn(a)*ifftn(b))).real

assuming you've managed to read your original images into numpy arrays a & b. If it's 2D images mayavi might be a bit overkill, and it would probably be easier to use matplotlib than chaco. If using matplotlib, you could do the whole lot with

from pylab import *
corr = (ifftn(fftn(a)*ifftn(b))).real
imshow(corr)
夏尔 2024-09-07 04:54:11

Scipy 在其 scipy.ndimage 包中包含许多图像处理例程。

Scipy contains many image processing routines in its scipy.ndimage package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文