Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像间的互相关_Python_Image Processing_Fft_Cross Correlation - Fatal编程技术网

Python 图像间的互相关

Python 图像间的互相关,python,image-processing,fft,cross-correlation,Python,Image Processing,Fft,Cross Correlation,我想使用去快速傅里叶变换计算互相关,以便按照下图的步骤进行云运动跟踪 我有点困惑,因为我从未使用过这种技术。?应用程序是否准确 提示:等式(1)是:R(p,q)=Cov(p,q)/(sigma_t0*sigma_t1)。如果需要更多信息,论文是:“一种自动化技术,或使用互相关从地球静止卫星数据中获取云运动” 我找到了源代码,但我不知道是否做了我正在尝试的事情。如果您尝试做类似于cv2.matchTemplate(),可以在以下位置找到标准化互相关(NCC)方法的python实现: ######

我想使用去快速傅里叶变换计算互相关,以便按照下图的步骤进行云运动跟踪

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

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


我找到了源代码,但我不知道是否做了我正在尝试的事情。

如果您尝试做类似于
cv2.matchTemplate()
,可以在以下位置找到标准化互相关(NCC)方法的python实现:

########################################################################################
作者:Ujash Joshi,多伦多大学,2017
#基于倍频程实施:Benjamin Eltzner,2014#
#python 3.5中Octave/Matlab normxcorr2的实现#
#详情:#
#归一化互相关。类似结果最多3个有效数字#
# https://github.com/Sabrewarrior/normxcorr2-python/master/norxcorr2.py                #
# http://lordsabre.blogspot.ca/2017/09/matlab-normxcorr2-implemented-in-python.html    #
########################################################################################
将numpy作为np导入
从scipy.signal导入fftconvolve
def normxcorr2(模板、图像、模式=“完整”):
"""
输入数组应该是浮点数。
:param template:N-D数组,是用于互相关的模板或过滤器的数组。
必须小于或等于图像的尺寸。
每个维度的长度必须小于图像的长度。
:param image:N-D数组
:参数模式:选项“完整”、“有效”、“相同”
full(默认):fftconvolve的输出是输入的完全离散线性卷积。
输出大小将是图像大小+每个维度中模板大小的1/2。
有效:输出仅由不依赖零填充的元素组成。
相同:输出与图像大小相同,相对于“完整”输出居中。
:return:N-D与图像尺寸相同的数组。大小取决于模式参数。
"""
#如果发生这种情况,可能是一个错误
如果np.ndim(模板)>np.ndim(图像)或\
如果template.shape[i]>image.shape[i]]>0:
打印(“normxcorr2:模板大于IMG。参数可以交换。”)
模板=模板-np.平均值(模板)
图像=图像-np.平均值(图像)
a1=np.ones(模板形状)
#上下左右翻转更快,然后使用fftconvolve而不是scipy的correlate
ar=np.flipud(np.fliplr(模板))
out=fftconvolve(图像,ar.conj(),mode=mode)
图像=fftconvolve(np.正方形(图像),a1,模式=模式)-\
np.square(fftconvolve(image,a1,mode=mode))/(np.prod(template.shape))
#消除减法后的小机床精度误差
图像[np.其中(图像<0)]=0
模板=np.和(np.平方(模板))
out=out/np.sqrt(图像*模板)
#删除0或非常接近0的任何分区
out[np.where(np.logical_not(np.isfinite(out))]=0
返回

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

这是一个示例,是的,它是正确的。不确定OpenCV,但Scipy已经实现了这一点:。我注意到您在Stackoverflow上问了几个问题,但忘记了所有问题,请单击答案旁边的复选框,将其选为正式问题解决方案。这种机制的存在是为了帮助未来的访问者更快地找到答案。请花点时间检查您的所有问题,并执行以下操作:单击帮助您解决特定问题的答案旁边的复选框。通过保持网站的运作方式,你不仅可以帮助保持网站的组织性,还可以增加其他人在未来再次帮助你的机会。谢谢你的回答,只是为了确保
out
返回(在我的例子中)R(p,q),对吗?然后我需要得到demax[R(p,q)],这是用
np.max()
来知道最大的互相关系数,但是“p”和“q”到底是什么?该点的坐标?我假设
max[R(p,q)]
表示数组中的最大值。这就是我们在结果相关矩阵中寻找最佳分数(或相关性)时所需要的。如果答案对您有帮助,请随意向上投票,或者单击旁边的复选框,将其选为正式问题解决方案。上述实现与OpenCV“cv.TM\u cceeff\u NORMED”相同?最后…我的图像和模板是200 x 200,为什么
out
返回一个399 x 399数组?它不一样。。。这是相似的!如果希望它与OpenCV相同,则必须将
模式
更改为
“有效”
。相关性映射的大小将与输入图像的大小不同。在确定相关图的适当大小之后,有一个小数学。
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)) 
########################################################################################
# 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