【问题标题】:Cross-correlation between images图像之间的互相关
【发布时间】:2020-06-17 20:04:23
【问题描述】:

我想使用 de Fast Fourier Transform 计算互相关,以便按照下图的步骤进行云运动跟踪。

def roi_image(image):
    image = cv.imread(image, 0)
    roi = image[700:900, 1900:2100]
    return roi

def FouTransf(image):
    img_f32 = np.float32(image)
    d_ft = cv.dft(img_f32, flags = cv.DFT_COMPLEX_OUTPUT)
    d_ft_shift = np.fft.fftshift(d_ft)

    rows, cols = image.shape
    opt_rows = cv.getOptimalDFTSize(rows)
    opt_cols = cv.getOptimalDFTSize(cols)
    opt_img = np.zeros((opt_rows, opt_cols))
    opt_img[:rows, :cols] = image 
    crow, ccol = opt_rows / 2 , opt_cols / 2
    mask = np.zeros((opt_rows, opt_cols, 2), np.uint8)
    mask[int(crow-50):int(crow+50), int(ccol-50):int(ccol+50)] = 1

    f_mask = d_ft_shift*mask
    return f_mask


def inv_FouTransf(image):

    f_ishift = np.fft.ifftshift(image)
    img_back = cv.idft(f_ishift)
    img_back = cv.magnitude(img_back[:, :, 0], img_back[:, :, 1])

    return img_back

def rms(sigma):
    rms = np.std(sigma)
    return rms

# Step 1: Import images
a = roi_image(path_a)
b = roi_image(path_b)

# Step 2: Convert the image to frequency domain
G_t0 = FouTransf(a)
G_t0_conj = G_t0.conj()
G_t1 = FouTransf(b)

# Step 3: Compute C(m, v)
C = G_t0_conj * G_t1

# Step 4: Convert the image to space domain to obtain Cov (p, q)
c_w = inv_FouTransf(C)

# Step 5: Compute Cross correlation
R_pq = c_w / (rms(a) * rms(b)) 

我有点困惑,因为我从未使用过这种技术。 ¿ 应用程序准确吗?

提示:eq (1) 是:R(p,q) = Cov(p,q) / (sigma_t0 * sigma_t1)。如果需要更多信息,请参阅论文:“一种自动化技术或使用互相关从地球静止卫星数据中获取云运动”。

我找到了this 来源,但我不知道我是否正在尝试做某事。

【问题讨论】:

  • 这是convolution theorem 的示例,是的,它是正确的。不确定 OpenCV,但 Scipy 已经实现了这个:scipy.signal.correlate
  • 我注意到您在 Stackoverflow 上提出了几个问题,但都忘记了单击答案附近的复选框以将其选为官方问题解决者。这种机制的存在是为了帮助未来的访问者更快地找到他们的答案。请花时间检查您的所有问题并执行此操作:单击帮助您解决特定问题的答案附近的复选框。通过跟上网站的运作方式,您不仅可以帮助网站保持井井有条,还可以增加其他人在未来再次帮助您的机会。

标签: python image-processing fft cross-correlation


【解决方案1】:

如果您尝试执行类似于cv2.matchTemplate() 的操作,可以在this repository 中找到Normalized Cross-Correlation (NCC) 方法的工作 python 实现:

########################################################################################
# Author: Ujash Joshi, University of Toronto, 2017                                     #
# Based on Octave implementation by: Benjamin Eltzner, 2014 <b.eltzner@gmx.de>         #
# Octave/Matlab normxcorr2 implementation in python 3.5                                #
# Details:                                                                             #
# Normalized cross-correlation. Similiar results upto 3 significant digits.            #
# https://github.com/Sabrewarrior/normxcorr2-python/master/norxcorr2.py                #
# http://lordsabre.blogspot.ca/2017/09/matlab-normxcorr2-implemented-in-python.html    #
########################################################################################

import numpy as np
from scipy.signal import fftconvolve


def normxcorr2(template, image, mode="full"):
    """
    Input arrays should be floating point numbers.
    :param template: N-D array, of template or filter you are using for cross-correlation.
    Must be less or equal dimensions to image.
    Length of each dimension must be less than length of image.
    :param image: N-D array
    :param mode: Options, "full", "valid", "same"
    full (Default): The output of fftconvolve is the full discrete linear convolution of the inputs. 
    Output size will be image size + 1/2 template size in each dimension.
    valid: The output consists only of those elements that do not rely on the zero-padding.
    same: The output is the same size as image, centered with respect to the ‘full’ output.
    :return: N-D array of same dimensions as image. Size depends on mode parameter.
    """

    # If this happens, it is probably a mistake
    if np.ndim(template) > np.ndim(image) or \
            len([i for i in range(np.ndim(template)) if template.shape[i] > image.shape[i]]) > 0:
        print("normxcorr2: TEMPLATE larger than IMG. Arguments may be swapped.")

    template = template - np.mean(template)
    image = image - np.mean(image)

    a1 = np.ones(template.shape)
    # Faster to flip up down and left right then use fftconvolve instead of scipy's correlate
    ar = np.flipud(np.fliplr(template))
    out = fftconvolve(image, ar.conj(), mode=mode)

    image = fftconvolve(np.square(image), a1, mode=mode) - \
            np.square(fftconvolve(image, a1, mode=mode)) / (np.prod(template.shape))

    # Remove small machine precision errors after subtraction
    image[np.where(image < 0)] = 0

    template = np.sum(np.square(template))
    out = out / np.sqrt(image * template)

    # Remove any divisions by 0 or very close to 0
    out[np.where(np.logical_not(np.isfinite(out)))] = 0

    return out

normxcorr2() 返回的对象是互相关矩阵。

【讨论】:

  • 感谢您的回答,只是为了确保...out 返回(在我的情况下)R(p,q),对吗?然后我需要获得 de max[ R(p,q)] ,这是np.max() 以便知道最大互相关系数,但究竟什么是“p”和“q”?该点的坐标?
  • 我假设max[ R(p,q)] 代表数组中的最大值。这就是我们在生成的相关矩阵中寻找最佳分数(或相关性)时想要的。如果它对您有帮助,请随时为答案投票,或单击它附近的复选框将其选为官方问题解决者。
  • 上述实现和OpenCV“cv.TM_CCOEFF_NORMED”一样吗?最后...我的图像和模板是 200 x 200,为什么 out 返回一个 399 x 399 的数组?
  • 这不是相同...它是相似的!如果您希望它与 OpenCV 相同,则必须将 mode 更改为 'valid'
  • 相关图的大小与输入图像的大小不同。确定相关图的适当大小需要一些数学运算。
猜你喜欢
  • 2014-04-11
  • 1970-01-01
  • 2020-06-13
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
相关资源
最近更新 更多