Rentonie Rentonie -4 years ago 520
Python Question

Blockproc like function for Python image processing

edit: it's an image so the suggested (How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function) isn't really working for me

I have the following matlab code

fun = @(block_struct) ...
std2( * ones(size(;

B=blockproc(im2double(Icorrected), [4 4], fun);

I want to remake my code, but this time in Python. I have installed Scikit and i'm trying to work around it like this

b = np.std(a, axis = 2)

The problem of course it's that i'm not applying the std for a number of blocks, just like above.

How can i do something like this? Start a loop and try to call the function for each X*X blocks? Then i wouldn't keep the size the it was.

Is there another more efficient way?

Answer Source

I did the following

io.use_plugin('pil', 'imread')
a = io.imread('C:\Users\Dimitrios\Desktop\polimesa\\arizona.jpg')

B = np.zeros((len(a)/2 +1, len(a[0])/2 +1))

for i in xrange(0, len(a), 2):
    for j in xrange(0, len(a[0]), 2):
        if i+1 < len(a):
        if j+1 < len(a[0]):
        if i+1 < len(a) and j+1 < len(a[0]):
        B[i/2][j/2] = np.std(x)
        x[:] = []         

and i think it's correct. Iterating over the image by 2 and taking each neighbour node, adding them to a list and calculating std.

edit* later edited for 4x4 blocks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download