Return the index of a row with the min function in Python

Asked

Viewed 334 times

1

I am trying to find the index of a numpy matrix (cut of a face detected in opencv) and draw a line where the sum of the pixels is the minimum value.

I can’t return the index.

Follow the code below

for (x, y, l, a) in facesDetectadas:
    # Recortar a região do rosto redimensionada em 200 x 200        
    rosto = cv2.resize(frame[y:y + a, x:x + l], (200, 200))             

    # Desenhar um retangulo na região do rosto      
    cv2.rectangle( frame, (x,y) , (x+l, y+a) , (0,0,255) , 2)

    rosto = rosto.astype(np.uint16)
    b,g,r = cv2.split(rosto)
    olhos = (b + g + r)

    linhaOlhos = np.sum(olhos, axis = 0)
    marcarOlhos = linhaOlhos.min(axis = 0)

    cv2.rectangle(rosto, (0,marcarOlhos) , (199,marcarOlhos), (0,0,255) , 2)

2 answers

3

The for in Python is always a for each: that is, it will always go through the elements of a sequence, in case its facesDetectadas, and not, as is necessary in most other languages, for which will cause an index variable to assume a value of 0 up to the length of the sequence, into the for have to obtain the sequence element (e.g. with x, y, l, a = facesDetectadas[i]).

Eventually, in addition to the elements themselves, we also need the elements' indices. In these cases, you can use the builtin function enumerate - that for each element of a sequence, returns a tuple with the index of the element and the element itself.

So it’s possible to do:

valor_min, index_min = None, -1

for index, (x, y, a, l) in  enumerate(facesDetectadas):
    ...
    linhaOlhos = np.sum(olhos, axis = 0)
    if valor_min is None or valor_min > linhaOlhos:
         index_min = index
         valor_min = linhaOlhos
    ...

Ready - at the end of for, index_min contains the minimum face index for which this value is.

I also leave a style hint: avoid using the name l for a variable - even in direct cases like this, where it is always used in conjunction with x, y, l , a, and so it’s about "width," that letter alone looks a lot like the digit 1, and, looking at the program a person has to stay a while wondering if he is watching a x + 1 or a x + l. If using variables in Portuguese, use a smaller abbreviation - larg same, or use the letters in English for this case.

1

Try using the function np.argmin as follows:

import numpy as np
for (x, y, l, a) in facesDetectadas:
    # Recortar a região do rosto redimensionada em 200 x 200        
    rosto = cv2.resize(frame[y:y + a, x:x + l], (200, 200))             

    # Desenhar um retangulo na região do rosto      
    cv2.rectangle( frame, (x,y) , (x+l, y+a) , (0,0,255) , 2)

    rosto = rosto.astype(np.uint16)
    b,g,r = cv2.split(rosto)
    olhos = (b + g + r)

    linhaOlhos = np.sum(olhos, axis = 0)
    marcarOlhos = np.argmin(linhaOlhos)

    cv2.rectangle(rosto, (0,marcarOlhos) , (199,marcarOlhos), (0,0,255) , 2)

Browser other questions tagged

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