Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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_Numpy_Vectorization_Array Broadcasting_Sliding Window - Fatal编程技术网

Python 如何在矢量化滑动窗口的切片上调用函数?

Python 如何在矢量化滑动窗口的切片上调用函数?,python,numpy,vectorization,array-broadcasting,sliding-window,Python,Numpy,Vectorization,Array Broadcasting,Sliding Window,我正在尝试对滑动窗口搜索进行矢量化,以便进行目标检测。到目前为止,我已经能够使用numpy广播将我的主图像切片为窗口大小的切片,这些切片存储在变量all_windows中,如下所示。我已经验证了实际值是否匹配,所以我很高兴看到这一点 下一部分是我遇到的问题。我想在调用patchCleanNPredict()函数时索引到all_windows数组中,这样我就可以以类似的矢量化格式将每个窗口传递到函数中 我试图创建一个名为new_indx的数组,该数组将包含2d数组中的切片索引,例如([0,0]、[

我正在尝试对滑动窗口搜索进行矢量化,以便进行目标检测。到目前为止,我已经能够使用numpy广播将我的主图像切片为窗口大小的切片,这些切片存储在变量
all_windows
中,如下所示。我已经验证了实际值是否匹配,所以我很高兴看到这一点

下一部分是我遇到的问题。我想在调用
patchCleanNPredict()
函数时索引到
all_windows
数组中,这样我就可以以类似的矢量化格式将每个窗口传递到函数中

我试图创建一个名为new_indx的数组,该数组将包含2d数组中的切片索引,例如([0,0]、[1,0]、[2,0]…),但遇到了一些问题

我希望最终得到每个窗口的置信值数组。下面的代码在python 3.5中工作。提前感谢您的帮助/建议

import numpy as np

def patchCleanNPredict(patch):
    # patch = cv2.resize()# shrink patches with opencv resize function
    patch = np.resize(patch.flatten(),(1,np.shape(patch.flatten())[0])) # flatten the patch
    print('patch: ',patch.shape) 
    # confidence = predict(patch) # fake function showing prediction intent
    return # confidence


window = (30,46)# window dimensions
strideY = 10
strideX = 10

img = np.random.randint(0,245,(640,480)) # image that is being sliced by the windows

indx = np.arange(0,img.shape[0]-window[1],strideY)[:,None]+np.arange(window[1])
vertical_windows = img[indx]
print(vertical_windows.shape) # returns (60,46,480)


vertical_windows = np.transpose(vertical_windows,(0,2,1))
indx = np.arange(0,vertical_windows.shape[1]-window[0],strideX)[:,None]+np.arange(window[0])
all_windows = vertical_windows[0:vertical_windows.shape[0],indx]
all_windows = np.transpose(all_windows,(1,0,3,2))

print(all_windows.shape) # returns (45,60,46,30)


data_patch_size = (int(window[0]/2),int(window[1]/2)) # size the windows will be shrunk to

single_patch = all_windows[0,0,:,:]
patchCleanNPredict(single_patch) # prints the flattened patch size (1,1380)

new_indx = (1,1) # should this be an array of indices? 
patchCleanNPredict(all_windows[new_indx,:,:]) ## this is where I'm having trouble

为了以矢量化的方式评估所有窗口上的函数,我不得不使用np.transpose进行大量的调整大小和重新排列,以使所有窗口都能正确广播。下面的代码可以工作,并用于循环显示和确认图像窗口没有被篡改/混淆。对于全速运行,它们将被删除/注释

一个小小的免责声明:我想一定有更干净的2D矩阵滑动窗口的实现,但由于我找不到,下面的例子可能会对其他人有所帮助。此外,一些频繁的重新排列和调整大小可能需要对广播语法有更透彻的理解

import numpy as np
import cv2


def Predict(flattened_patches):
    # taking the mean of the flattened windows and then returning the
    # index of the row (window) with the highest mean, a predicter would have the same syntax
    results = flattened_patches.mean(1) 
    max_index = results.argmax() 
    return results, max_index

## -------- image and sliding window setup -------------------------
AR = 1.45 # choose an aspect ratio to maintain throughout scaling steps
win_h = 200 # window height
win_w = int(win_h/AR) # window width
window = (win_w,win_h)# window dimensions
strideY = 100
strideX = 100

data_patch_size = (30,46) # size the windows will be shrunk to for object detection

img = cv2.imread('picture6.png') # load an image to slide over

cv2.namedWindow('image',cv2.WINDOW_NORMAL) 
cv2.resizeWindow("image",int(img.shape[1]/2),int(img.shape[0]/2)) # shrink the image viewing window if you have large images

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## -------- end of, image and sliding window setup --------------------

## -------- sliding window vectorization steps --------------------------
num_vert_windows = len(np.arange(0,img.shape[0]-window[1],strideY)) # number of vertical windows that will be created
indx = np.arange(0,img.shape[0]-window[1],strideY)[:,None]+np.arange(window[1]) # index that will be broadcasted across image
vertical_windows = img[indx] # array of windows win_h tall and the full width of the image

vertical_windows = np.transpose(vertical_windows,(0,2,1)) # transpose to prep for broadcasting
num_horz_windows = len(np.arange(0,vertical_windows.shape[1]-window[0],strideX)) # number of horizontal windows that will be created
indx = np.arange(0,vertical_windows.shape[1]-window[0],strideX)[:,None]+np.arange(window[0]) # index for broadcasting across vertical windows
all_windows = vertical_windows[0:vertical_windows.shape[0],indx] # array of all the windows
## -------- end of, sliding window vectorization ------------------------

## ------- The below code rearranges and flattens the windows into a single matrix of pixels in columns and each window
## ------- in a row which makes evaluating a function over every window in a vectorized manner easier

total_windows = num_vert_windows*num_horz_windows

all_windows = np.transpose(all_windows,(3,2,1,0)) # rearrange for resizing and intuitive indexing

print('all_windows shape as stored in 2d matrix:', all_windows.shape)
for i in range(all_windows.shape[2]): # display windows for visual confirmation
    for j in range(all_windows.shape[3]):
        cv2.imshow('image',all_windows[:,:,i,j])
        cv2.waitKey(100)

all_windows = np.resize(all_windows,(win_h,win_w,total_windows))
print('all_windows shape after folding into 1d vector:', all_windows.shape)
for i in range(all_windows.shape[2]): # display windows for visual confirmation
    cv2.imshow('image',all_windows[:,:,i])
    cv2.waitKey(100)

# shrinking all the windows down to the size needed for object detect predictions
small_windows = cv2.resize(all_windows[:,:,0:all_windows.shape[2]],data_patch_size,0,0,cv2.INTER_AREA)
print('all_windows shape after shrinking to evaluation size:',small_windows.shape)
for i in range(small_windows.shape[2]): # display windows for vis. conf.
    cv2.imshow('image',small_windows[:,:,i])
    cv2.waitKey(100)

# flattening and rearranging the window data so that the pixels are in columns and each window is a row
flat_windows = np.resize(small_windows,(data_patch_size[0]*data_patch_size[1],total_windows))
flat_windows = np.transpose(flat_windows)
print('shape of the window data to send to the predicter:',np.shape(flat_windows))

results, max_index = Predict(flat_windows) # get predictions on all the windows