setting an array element with a sequence

3

I have a program in python and opencv that reads an image and then checks the colors of the image in each position of two% nested%. Since I am doing for in the image to make it easier to verify, it will return a binary image. And I can not find anything to help me in this context of image processing. The error happens when I'm going to run threshold , 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

Image

    
asked by anonymous 05.01.2019 / 19:25

1 answer

2

OpenCV images are arrays, you are forgetting to use the array indices to get the color

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

I do not know if your work is just an exercise, but you can use the cv2.inRange of opencv to know if a color is within a given range

Edit: Updating the answer. The threshold returns 2 values (a retval and the binarized image). In the threshold line you should ignore the retval to get only the image:

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

Edit2: InRange is in the whole image, in case you are going to use it, you do not 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))
    
05.01.2019 / 19:35