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