Search code examples
pythonfor-loopimage-processingnumpynested-loops

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]]

Solution

  • How about:

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