Setting an array element with a Sequence

Asked

Viewed 48 times

3

I’m with a program in python and opencv that reads an image and then checks the colors of the image at each position of two for nestled. How I’m making a threshold in the image to facilitate the verification will return a binary image. And I can’t find anything that helps me in this context of image processing. The error happens the moment I execute the if, in which the following message appears:

Setting an array element with a Sequence.

Code:

asfaltoMax = np.array([108, 109, 111]) 
asfaltoMin = np.array([175, 175, 175])
terraMax = np.array([33, 62, 123])
terraMin = np.array([163, 169, 206])

img = cv2.imread("IF24-6-2015.jpg")
imgThresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

largura = int(img.shape[1])
altura = int(img.shape[0])
qntPixels = largura * altura

auxCor = np.empty([1,3])
auxAsfalto = np.empty([1,3])
auxTerra = np.empty([1,3])

contAsfalto = 0
contTerra = 0
linhas = 0
colunas = 0
cont = 0

for i in range(largura):
    for j in range(altura):
        #auxCor em B, G, R
        auxCor = imgThresh
        print("Imagem na posição i,j: {}".format(auxCor))
        -->if auxCor >= asfaltoMin and auxCor <= asfaltoMax:
            print("Cor BGR do pixel: {}, posição i: {}, j: {}".format(auxCor, i, j))
        print("Iteração: {}, auxCor: {}".format(cont, auxCor))
        break
    cont += 1




cv2.imshow("Imagem", imgThresh)
cv2.waitKey(0)

EDIT

...Código anterior...
for i in range(largura):
    for j in range(altura):
        #auxCor em B, G, R

        print("Imagem na posição i,j: {}".format(auxCor))
        -->if cv2.inRange(imgThresh, asfaltoMin, asfaltoMax, auxCor): #The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
            print("Cor BGR do pixel: {}, posição i: {}, j: {}".format(auxCor, i, j))
        print("Iteração: {}, auxCor: {}".format(cont, auxCor))
        break
    cont += 1

EDIT 2

somenteAsfalto = cv2.inRange(img, asfaltoMin, asfaltoMax)
somenteTerra = cv2.inRange(img, terraMin, terraMax)
for i in range(altura):
    for j in range(largura):
        if somenteAsfalto[i, j] == 1: #Nunca entra
            print("Tem asfalto em i: {}, j: {}".format(i,j))
        if somenteTerra[i, j] == 1: #Nunca entra
            print("Tem terra em i: {}, j: {}".format(i, j))
    cont += 1

Imagery

Imagem usada para teste

1 answer

3


Images in Opencv are arrays, Voce is forgetting to use the indices of the array to catch the color

 #auxCor em B, G, R
 auxCor = imgThresh[i, j]

I don’t know if your job is just an exercise, but you can use the function cv2.inRange opencv to know if a color is within a certain range

Edit: Updating the answer. Threshold returns 2 values (a retval and the binarized image). In the line of Threshold you must ignore the retval to get only the image:

_, imgThresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

Edit2: The inRange is in the whole image, if you use it, you don’t need the Threshold:

asfaltoMax = np.array([108, 109, 111]) 
asfaltoMin = np.array([175, 175, 175])
terraMax = np.array([33, 62, 123])
terraMin = np.array([163, 169, 206])

img = cv2.imread("IF24-6-2015.jpg")
somenteAsfalto = cv2.inRange(img, asfaltoMin, asfaltoMax)
somenteTerra = cv2.inRange(img, terraMin, terraMax)

for i in range(img.shape[0]):
   for j in range(img.shape[1]):
       if somenteAsfalto[i, j] == 1:
           print ("tem asfalto em %d - %d" % (i, j))
  • When I use indexes like imgThresh[i,j] the following error appears: "tuple indices must be integers or Slices, not tuple". And when I use as imgThresh[i][j] this error appears: "'float' Object is not subscriptable". cv2.inRange I imagined it was for video only.

  • The opencv returns a tuple, by default, the first position is a number and the second is the image itself, I will update the answer to take this into account.

  • After this, the following error appears: "The Truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". I see no point using these methods within inRange. What can be done? Note: I will update the question

  • The question is that in the binarized image the pixel has only one value (between 0 and 255), and Oce is testing with an array of 3 positions (asfaltoMax, aslfatoMin) and the numpy does not know how to compare 1 value with 3

  • In EDIT 2 is not entering if. for executes but never enters if

  • Ae has to see its minimum and maximum values. No way to share the image?

  • I’ll ask the question

  • Max and min are inverted, asphaltMax should be asphaltMin and vice versa. Also for terraMin and terraMax. An error of mine is that inRange returns 255 when it is within the range, not 1. So if should be somenteAsfalto[i, j] == 255

Show 4 more comments

Browser other questions tagged

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