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
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.
– Marcos Paulo S. Rezende
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.
– Begnini
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
– Marcos Paulo S. Rezende
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
– Begnini
In EDIT 2 is not entering if. for executes but never enters if
– Marcos Paulo S. Rezende
Ae has to see its minimum and maximum values. No way to share the image?
– Begnini
I’ll ask the question
– Marcos Paulo S. Rezende
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
– Begnini
Let’s go continue this discussion in chat.
– Marcos Paulo S. Rezende