Array always receiving the same value [Loop in two arrays simultaneously]

4

My program traverses an image and assigns the color values R, G, B to variables that were compared to matrices containing the R, G, B colors of asphalt and earth called auxAsfalto and auxTerra . The question is: when I run the image in the i, j position, which are the pixels of the horizontal and vertical, everything happens normal, but when I go through the matrices that have the asphalt and earth color values, two errors occur: 1st for that runs through the asphalt color matrix does not exit position 0. However 2nd is that which traverses the color matrix of earth always returns the first line of the matrix. That is, the repeat structures are not working. Thanks in advance.

Follow the code snippet:

for i in range(img.shape[1]): #percorre a linha
    for j in range(img.shape[0]): #percorre a coluna
        auxCor = img[i,j]
        for k in range(len(corAsfalto)): #percorre a matriz corAsfalto
            auxAsfalto = corAsfalto[k]
            print("TESTE auxAsfalto: {}".format(auxAsfalto))
            if np.all(auxAsfalto) == np.all(auxCor): #verifica se as cores são iguais
                print("Cor de Asfalto em auxAsfalto: {}".format(auxCor))
                contAsfalto +=1
                break
            else:
                print("Não possui asfalto")
                break
        for l in range(len(corTerra)): #percorre a matriz corTerra
            auxTerra = corTerra[l]
            print("TeSTe auxTerra: {}".format(auxTerra))
            if np.all(auxTerra) == np.all(auxCor): #verifica se as cores são iguais
                print("Cor terra em auxCor: {}, cor Terra em auxTerra: {}".format(auxCor, auxTerra))
                contTerra += 1
                break
            else:
                break

    print("Quantidade de pixels de asfalto: %d"%contAsfalto)
    print("Quantidade de pixels de terra: %d"%contTerra)
    print("Quantidade de pixels da imagem: %d"%qntPixels)

Return:

Cor terra em auxCor: [132 136 117], cor Terra em auxTerra: [128.  105.1  98.9]
TESTE auxAsfalto: [154.  172.1 173.9]
Cor de Asfalto em auxAsfalto: [141 147 128]
TeSTe auxTerra: [128.  105.1  98.9]
Cor terra em auxCor: [141 147 128], cor Terra em auxTerra: [128.  105.1  98.9]
TESTE auxAsfalto: [154.  172.1 173.9]
Cor de Asfalto em auxAsfalto: [153 159 140]
TeSTe auxTerra: [128.  105.1  98.9]
    
asked by anonymous 02.01.2019 / 13:44

1 answer

4

So I understand your "arrays" are common arrays of images that have the (n, n, 3) format correct? and you want to compare the occurrences of RGB channels within them, right? Since you did not create a complete and verifiable example , to understand the context, I created one, see if it's what you want:

TL; DR

import numpy as np

# Criação das imagens:
img_terra = np.random.randint(222, size=(10, 10,3)) 
img_asfalto = np.random.randint(222, size=(10, 10,3))

# Forçando uma igualdade
img_terra[5] = img_asfalto[5]

# Comparando as ocorrencias nas imagens:
for im1, im2 in zip(img_asfalto, img_terra):
    if np.array_equal(im1,im2):
        ln = '*'*18
        print(ln,'Ocorrencias iguais',ln, sep='\n')
    else:
        print('ocorrencias diferentes:')
        '''
        #(1) Aqui voce pode usar um outro for com zip para 
        tratar cada elemento nas duas ocorencias:
        '''    
else:
    print("Melhor do que 'for' aninhados. :-)")   

In the middle of the code where I wrote with #(1) you could do the treatment you want / need with each element of the two images when the occurrences are different, for example something like:

for e1, e2 in zip(im1,im2):
    # Process
    # ... 
    pass

See working at repl.it

  

Edited (according to comment)
  Example with zip

lst1  = [1, 2, 3]
lst2  = ['A', 2, 'B']
zipped = list(zip(lst1,lst2))  
print(zipped)

The output is a list of tuples with the pairs of elements of the two lists:

[(1, 'A'), (2, 2), (3, 'B')] # Lista de tuplas

Comparing the elements of the two lists:

for e1, e2 in zip(lst1, lst2):
    if e1==e2:
        idx = lst1.index(e1)
        print('elemento:',idx, 'de lst1 é igual ao elemento', idx, 'de lst2')

Output:

elemento 1 de lst1 é igual ao elemento 1 de lst2
    
02.01.2019 / 18:03