sebko_iic
sebko_iic

Reputation: 340

Vectorization of two for-loops

I'm trying to vectorize a function and I'm almost there. All that's left is this

for x in range(0,img.shape[1]):
        for y in range(0,img.shape[0]):  

            if w00[y,x] > 0:
                total[y,x] += img.item((y0[y,x], x0[y,x])) * w00[y,x]
            if w01[y,x] > 0:
                total[y,x] += img.item((y1[y,x], x0[y,x])) * w01[y,x]
            if w10[y,x] > 0:
                total[y,x] += img.item((y0[y,x], x1[y,x])) * w10[y,x]
            if w11[y,x] > 0:
                total[y,x] += img.item((y1[y,x], x1[y,x])) * w11[y,x]

This now looks very ugly as I vectorized everything else already and introduce this for-loop to not break the code. I struggle to vectorize this part as the indexes switch every time. Some help would be nice.

Also y1 and x1 are just x0 + 1 and y0 + 1, which could simplify vectorization

Upvotes: 1

Views: 66

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Something like this would work

for w, x, y in zip([w00,w01,w10,w11], [y0,y1,y0,y1], [x0,x0,x1,x1]):
    total += img[y,x] * w * (w>0)

Edit: Some modification might be needed. I can't test whether the code runs properly due to lack of sample data.

Upvotes: 1

Related Questions