1
votes

When I used python to do the arnold scrambling algorithm, I found that the running time of the for loop was not good enough, and the time reached more than 1 second. I wanted to use numpy to speed up, but I don't know how to start. The arnold algorithm mainly uses the pixel coordinates of the original image to do a mapping transformation to obtain new coordinates, and fills the pixel values into the new coordinates.

def arnold(img):
    r, c = img.shape
    pic = np.zeros((r, c), np.uint8)
    for i in range(r):
        for j in range(c):
            x = (i + j) % r
            y = (i + 2 * j) % c
            pic[x, y] = img[i, j]
    return pic
1

1 Answers

1
votes

This repo seems to be what you are looking for. arnold.py has the scrambling as applyTransformTo, line 24.

Without going too further on the analysis, I would say doing the loop inside the other loop is what is messing the speed.

Python is good to organize stuff, but if you need mathematically intensive operations, it is best to use something like numpy to do it for you.

You are right about that part, but the problem is that you are only using numpy to instantiate the object, mostly. Python is still doing most of the job of the flow.