python - 2D mutable iterator/generator -


i have nxn matrix want split non-overlap kxk block. each block, want assign new values elements.

since looks place generator, implemented:

def extracted_patches(im, top_left, patch_size, grid_size):     '''extract patches in row-major order following specific configuration      parameters     ----------     im : input image (2d numpy array)     top_left : (y,x) coordinate of top left point (e.g. (3,5))     grid_size : (cy, cx) how many patches in y-direction , in x-direction     patch_size : (h, w) how many pixels size of each patch      returns     -------     generator goes through each patch (a numpy array view) in row-major order     '''     in xrange(grid_size[0]):         j in xrange(grid_size[1]):             yield im[top_left[0] + patch_size[0]*i : top_left[0] + patch_size[0]*(i+1)                     ,top_left[1] + patch_size[1]*j : top_left[1] + patch_size[1]*(j+1)] 

then when try change value of each patch, assignment change variable value instead of value generator gives

output_im = np.zeros((patch_size[0]*grid_size[0], patch_size[1]*grid_size[1]))         output_im_it = extracted_patches(output_im, (0,0), patch_size, grid_size)  in xrange(grid_size[0]*grid_size[1]):     output_im_it = np.random.random(patch_size) 

can generator mutable?

as variables holding numpy array, change value "pointed to" want avoid assigning variable assign slice of it. try this:

for submat in output_im_it:      submat[:] = np.random.random(patch_size) 

as response edit: seems have confused generator object values yields. can't assign slices of generator object itself. can assign slices of numpy arrays, can e.g. output_im_it.next() or loop, above.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -