Identifying the most efficient method of identifying the central element of an array

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 the following array:

[[   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'm working with image processing and this operation has proven to be very costly when I use large images.

Can anyone tell me if there is another way to do this, which I propose, more quickly?

    
asked by anonymous 23.09.2017 / 16:52

1 answer

1

Dude, when it comes to algorithm, you can not do better. The reason is simple: you have to, at a minimum, fill in the value 255 each position you want. What you do in the algorithm is to go, one by one, filling the array. Matrix processing really is heavy. If you want to improve performance, you can look into how to do the same algorithm in a multi-threaded way, so that your processor can parallelize this processing and greatly decrease the runtime. If your cpu is 8 colors you can focus up to 8 times faster + -.

    
23.09.2017 / 17:47