Leopoldo
Leopoldo

Reputation: 845

Is it possible to accelerate python nested loop?

Is it possible to accelerate nested for loop in python?

I have to go through a whole image, pixel by pixel. Can I do something to speed it up?:

sz =  np.shape(img)
prob = np.zeros((sz[0],sz[1]))
for i in range(sz[0]):
    for j in range(sz[1]):
        prob[i, j] = rgbMtx[img[i, j, 0], img[i, j, 1], img[i, j, 2]]

Upvotes: 0

Views: 107

Answers (1)

Bi Rico
Bi Rico

Reputation: 25833

How about:

i, j, k = img[:, :, 0], img[:, :, 1], img[:, :, 2]
prob = rgMtx[i, j, k]

Upvotes: 1

Related Questions