4
I have a program that reads an image and checks if there are certain colors in that image, if it does paint each pixel a certain color. I couldn’t find materials on the Internet that could help me, just this one page that has a solution using histograms, but I did not understand how this is done even with the code available. Is it really possible to use histograms? Is there any other easier way to calculate the color percentage of the image? I tried to use a rule of three to calculate but returns absurd values.
Code snippet:
img = cv2.imread("IF23-1-2018.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, imgThresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
somenteAsfalto = cv2.inRange(img, asfaltoMax, asfaltoMin)
somenteTerra = cv2.inRange(img, terraMax, terraMin)
somenteVerde = cv2.inRange(img, verdeMax, verdeMin)
for i in range(0, altura):
for j in range(0, largura):
if somenteAsfalto[i,j] == imgThresh[i,j]:
#pinta de vermelho onde tem asfalto
img[i,j] = vermelho
contAsfalto += 1
if somenteTerra[i,j] == imgThresh[i,j]:
#pinta de amarelo onde tem terra
img[i,j] = amarelo
contTerra += 1
if somenteVerde[i,j] == imgThresh[i,j]:
#pinta de violeta onde tem verde
img[i,j] = violeta
contVerde += 1
if imgThresh[i,j] == 1 and imgThresh[i,j] == 0:
img[i,j] = verde
cont += 1
if imgThresh[i,j] != somenteTerra[i,j]:
img[i,j] = verdeCons
contNatural = contVerde + contTerra
contConstruido = contAsfalto + construido
porcVerde = (100*contVerde)/qntPixels
porcConst = (100*construido)/qntPixels
print("Pixels de asfalto: {}\nPixels de Terra: {}\nPixels onde tem verde: {}".format(contAsfalto, contTerra, contVerde))
print("Tamanho da imagem: {}".format(qntPixels))
print("Contador construido: {}\nContador contVerde: {}".format(construido, contVerde))
print("Porcentagem construída: {}%\nPorcentagem verde: {}%".format(porcConst, porcVerde))
print("DEU RUIM nº {}".format(cont))
Input image
Output image
As you defined the colors of the asphalt ceiling, would have like to find something half light brown?
– guilherme
I used an image manipulation program to pick up the minimum and maximum colors (in RGB) of the object I want to identify
– Marcos Paulo S. Rezende
got it, paid program?
– guilherme
No, I used Gimp, which is free. Follow its official website: https://www.gimp.org/
– Marcos Paulo S. Rezende
Shut up, thanks brother
– guilherme