Identifying the most effective method of identifying the central element of a matrix

Asked

Viewed 45 times

0

I have the following algorithm:

import numpy as np

matriz = np.array(np.zeros(81).reshape(9, 9))

for i in range(1, matriz.shape[0], 3):
    for j in range(1, matriz.shape[1], 3):
        matriz[i, j] = 255

print(matriz)

It generates me the following matrix:

[[   0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.  255.    0.    0.  255.    0.    0.  255.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.  255.    0.    0.  255.    0.    0.  255.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.  255.    0.    0.  255.    0.    0.  255.    0.]
 [   0.    0.    0.    0.    0.    0.    0.    0.    0.]]

That is, this algorithm traverses this matrix, with a 3x3 mask, and includes the value 255 in its central position.

I am working with image processing and this operation has proved highly costly when using large images.

Would anyone know if there’s another way to do that, which I propose, faster?

1 answer

1


Man, when it comes to algorithm, you can’t do better. The reason is simple: you have to at least fill 255 each position you want. What you do in the algorithm is go, one by one, filling the matrix. Matrix processing really is heavy. If you want to improve performance, you can look at how to do the same algorithm multi-threaded, so that your processor can parallelize this processing and greatly shorten the excution time. If your cpu is 8 colors can focus up to 8 times faster +-.

  • Thanks @João Victor! I’ll search on multi thread.I think this solves my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.