在Python中查找最佳匹配的块/补丁

在Python中查找最佳匹配的块/补丁,python,arrays,window,patch,matching,Python,Arrays,Window,Patch,Matching,我希望在WxW窗口中定位最接近的匹配NxN块,该窗口位于较大2D阵列的位置(x,y)处。下面的代码工作正常,但对于我的需要来说速度非常慢,因为我需要多次运行此操作。有更好的方法吗?? 这里N=3,W=15,x=15,y=15,(bestx,besty)是最佳匹配块的中心 import numpy as np ## Generate some test data CurPatch = np.random.randint(20, size=(3, 3)) Data = np.random.rand

我希望在WxW窗口中定位最接近的匹配NxN块,该窗口位于较大2D阵列的位置(x,y)处。下面的代码工作正常,但对于我的需要来说速度非常慢,因为我需要多次运行此操作。有更好的方法吗?? 这里N=3,W=15,x=15,y=15,(bestx,besty)是最佳匹配块的中心

import numpy as np

## Generate some test data
CurPatch = np.random.randint(20, size=(3, 3))
Data = np.random.randint(20,size=(30,30))

# Current Location 
x,y = 15,15
# Initialise Best Match
bestcost = 999.0
bestx = 0;besty=0

for Wy in xrange(-7,8):
    for Wx in xrange(-7,8):
            Ywj,Ywi = y+Wy,x+Wx 

            cost = 0.0
            for py in xrange(3):
                for px in xrange(3):
                    cost += abs(Data[Ywj+py-1,Ywi+px-1] - CurPatch[py,px]) 

            if cost < bestcost:
                bestcost = cost
                besty,bestx = Ywj,Ywi

print besty,bestx
将numpy导入为np
##生成一些测试数据
CurPatch=np.random.randint(20,大小=(3,3))
Data=np.random.randint(20,size=(30,30))
#当前位置
x、 y=15,15
#初始化最佳匹配
最佳成本=999.0
最佳x=0;贝西=0
对于X范围内的Wy(-7,8):
对于X范围内的Wx(-7,8):
Ywj,Ywi=y+Wy,x+Wx
成本=0.0
对于X范围内的py(3):
对于X范围内的px(3):
成本+=abs(数据[Ywj+py-1,Ywi+px-1]-CurPatch[py,px])
如果成本<最佳成本:
最佳成本=成本
besty,bestx=Ywj,Ywi
打印最佳,最佳

正如我在评论中所说,您可以检查xrange(3)中px的
内部
成本是否大于或等于
最佳成本:
如果可以中断,这样可以节省大量不必要的迭代

示例(在较大的迭代中,灯光是否会改变以强调差异):

将numpy导入为np
导入时间
##生成一些测试数据
CurPatch=np.random.randint(100,大小=(3,3))
数据=np.random.randint(100,大小=(30003000))
#当前位置
x、 y=10,10
#初始化最佳匹配
最佳成本=999.0
最佳x=0;贝西=0
t0=时间。时间()
对于X范围内的Wy(-7,50):
对于X范围内的Wx(-7,50):
Ywj,Ywi=y+Wy,x+Wx
成本=0.0
对于X范围内的py(3):
对于X范围内的px(3):
成本+=abs(数据[Ywj+py-1,Ywi+px-1]-CurPatch[py,px])
如果成本>=最佳成本:
打破
如果成本<最佳成本:
最佳成本=成本
besty,bestx=Ywj,Ywi
打印最佳,最佳
打印“time:{}”。格式(time.time()-t0)
时间是26毫秒

时间:0.026999950489

没有中断的代码将输出37毫秒:

时间:0.0379998683929


此外,我还建议将此代码转换为函数。

为了感受速度,w与大窗口大小相同的子问题使用numpy更快(更简洁):

a= '''import numpy as np


## Generate some test data
CurPatch = np.random.randint(20, size=(3, 3))
Data = np.random.randint(20,size=(30,30))


def best(CurPatch,Data):

    # Current Location 
    x,y = 15,15
    # Initialise Best Match
    bestcost = 999.0
    bestx = 0;besty=0

    for Wy in xrange(-14,14):
        for Wx in xrange(-14,14):
                Ywj,Ywi = y+Wy,x+Wx 

                cost = 0.0
                for py in xrange(3):
                    for px in xrange(3):
                        cost += (Data[Ywj+py-1,Ywi+px-1] - CurPatch[py,px])**2 

                if cost < bestcost:
                    bestcost = cost
                    besty,bestx = Ywj,Ywi
    return besty,bestx,bestcost



def minimize(CurPatch,W):
    max_sum=999
    s= CurPatch.shape[0]
    S= W.shape[0]
    for i in range(0,S-s):
        for j in range(0,S-s):
            running= np.sum(np.square((W[i:i+3,j:j+3]-CurPatch)))
            if running<max_sum:
                max_sum=running
                x=i+1;y=j+1
    return x,y,max_sum

'''


import timeit
print min(timeit.Timer('minimize(CurPatch,Data)', a).repeat(7, 10))
print min(timeit.Timer('best(CurPatch,Data)', a).repeat(7, 10))     
a=''将numpy作为np导入
##生成一些测试数据
CurPatch=np.random.randint(20,大小=(3,3))
Data=np.random.randint(20,size=(30,30))
def最佳(CurPatch,数据):
#当前位置
x、 y=15,15
#初始化最佳匹配
最佳成本=999.0
最佳x=0;贝西=0
对于X范围内的Wy(-14,14):
对于X范围内的Wx(-14,14):
Ywj,Ywi=y+Wy,x+Wx
成本=0.0
对于X范围内的py(3):
对于X范围内的px(3):
成本+=(数据[Ywj+py-1,Ywi+px-1]-CurPatch[py,px])**2
如果成本<最佳成本:
最佳成本=成本
besty,bestx=Ywj,Ywi
返回最佳,最佳,最佳成本
def最小化(CurPatch,W):
最大和=999
s=CurPatch.shape[0]
S=W.形状[0]
对于范围(0,S-S)内的i:
对于范围(0,S-S)内的j:
running=np.sum(np.square((W[i:i+3,j:j+3]-CurPatch)))

如果运行,您可以检查xrange(3)中px的
成本是否大于或等于
最佳成本(3):
如果是这样,您可以
中断
,这样可以节省大量不必要的迭代。这个问题似乎与主题无关,因为它是关于改进工作代码的,这个问题可能更适合codereview.stackexchange.com
a= '''import numpy as np


## Generate some test data
CurPatch = np.random.randint(20, size=(3, 3))
Data = np.random.randint(20,size=(30,30))


def best(CurPatch,Data):

    # Current Location 
    x,y = 15,15
    # Initialise Best Match
    bestcost = 999.0
    bestx = 0;besty=0

    for Wy in xrange(-14,14):
        for Wx in xrange(-14,14):
                Ywj,Ywi = y+Wy,x+Wx 

                cost = 0.0
                for py in xrange(3):
                    for px in xrange(3):
                        cost += (Data[Ywj+py-1,Ywi+px-1] - CurPatch[py,px])**2 

                if cost < bestcost:
                    bestcost = cost
                    besty,bestx = Ywj,Ywi
    return besty,bestx,bestcost



def minimize(CurPatch,W):
    max_sum=999
    s= CurPatch.shape[0]
    S= W.shape[0]
    for i in range(0,S-s):
        for j in range(0,S-s):
            running= np.sum(np.square((W[i:i+3,j:j+3]-CurPatch)))
            if running<max_sum:
                max_sum=running
                x=i+1;y=j+1
    return x,y,max_sum

'''


import timeit
print min(timeit.Timer('minimize(CurPatch,Data)', a).repeat(7, 10))
print min(timeit.Timer('best(CurPatch,Data)', a).repeat(7, 10))