Rentonie - 29 days ago 31

Python Question

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(block_struct.data) * ones(size(block_struct.data));

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

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):
x.append(a[i][j])
if i+1 < len(a):
x.append(a[i+1][j])
if j+1 < len(a[0]):
x.append(a[i][j+1])
if i+1 < len(a) and j+1 < len(a[0]):
x.append(a[i+1][j+1])
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.