Lab03 - Histograma e equalização
Objetivos da aula:
- Histograma e equalização de histograma
- Seguimentação com auxilio do histograma
- Webcam opencv
Histograma¶
Na ultima aula, tentamos fazer a segmentação de um objeto da imagem pelo metodo força bruta #GoHorse, pode ser que funcione mas não é a forma mais intessnte de ser feita. Um histograma pode nos ajudar, ele plota em um gráfico de frequência as componentes de cores (r,g,b ou gray) da imagem.
In [ ]:
Copied!
%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import numpy as np
print ("OpenCV Version : %s " % cv2.__version__)
%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import numpy as np
print ("OpenCV Version : %s " % cv2.__version__)
OpenCV Version : 4.6.0
In [ ]:
Copied!
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/bola.png" /content
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/bolinha.png" /content
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/fuca.png" /content
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/bola.png" /content
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/bolinha.png" /content
!wget "https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/PDI/lab03/fuca.png" /content
--2023-02-10 19:52:38-- https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/lab03/bola.png Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 10267 (10K) [image/png] Saving to: ‘bola.png’ bola.png 100%[===================>] 10.03K --.-KB/s in 0s 2023-02-10 19:52:38 (80.3 MB/s) - ‘bola.png’ saved [10267/10267] /content: Scheme missing. FINISHED --2023-02-10 19:52:38-- Total wall clock time: 0.3s Downloaded: 1 files, 10K in 0s (80.3 MB/s) --2023-02-10 19:52:38-- https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/lab03/bolinha.png Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 205355 (201K) [image/png] Saving to: ‘bolinha.png’ bolinha.png 100%[===================>] 200.54K --.-KB/s in 0.03s 2023-02-10 19:52:38 (7.73 MB/s) - ‘bolinha.png’ saved [205355/205355] /content: Scheme missing. FINISHED --2023-02-10 19:52:38-- Total wall clock time: 0.2s Downloaded: 1 files, 201K in 0.03s (7.73 MB/s) --2023-02-10 19:52:38-- https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/lab03/fuca.png Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 46677 (46K) [image/png] Saving to: ‘fuca.png’ fuca.png 100%[===================>] 45.58K --.-KB/s in 0.01s 2023-02-10 19:52:38 (4.13 MB/s) - ‘fuca.png’ saved [46677/46677] /content: Scheme missing. FINISHED --2023-02-10 19:52:38-- Total wall clock time: 0.3s Downloaded: 1 files, 46K in 0.01s (4.13 MB/s) --2023-02-10 19:52:39-- https://raw.githubusercontent.com/arnaldojr/cognitivecomputing/master/material/aulas/lab03/bola.png Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 10267 (10K) [image/png] Saving to: ‘bola.png.1’ bola.png.1 100%[===================>] 10.03K --.-KB/s in 0s 2023-02-10 19:52:39 (71.3 MB/s) - ‘bola.png.1’ saved [10267/10267] /content: Scheme missing. FINISHED --2023-02-10 19:52:39-- Total wall clock time: 0.05s Downloaded: 1 files, 10K in 0s (71.3 MB/s)
In [ ]:
Copied!
img = cv2.imread("fuca.png", cv2.IMREAD_GRAYSCALE)
plt.imshow(img, cmap="Greys_r", vmin=0, vmax=255)
img = cv2.imread("fuca.png", cv2.IMREAD_GRAYSCALE)
plt.imshow(img, cmap="Greys_r", vmin=0, vmax=255)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff0dcc88d90>
In [ ]:
Copied!
plt.hist(img.ravel(),256,[0,256]); plt.show()
plt.hist(img.ravel(),256,[0,256]); plt.show()
In [ ]:
Copied!
# normalização de histograma
img_eq = cv2.equalizeHist(img)
plt.imshow(3*img_eq, cmap="Greys_r", vmin=0, vmax=255)
# normalização de histograma
img_eq = cv2.equalizeHist(img)
plt.imshow(3*img_eq, cmap="Greys_r", vmin=0, vmax=255)
Out[ ]:
<matplotlib.image.AxesImage at 0x252ed7118d0>
In [ ]:
Copied!
plt.hist(3*img_eq.ravel(),256,[0,256]); plt.show()
plt.hist(3*img_eq.ravel(),256,[0,256]); plt.show()
Podemos fazer o mesmo para uma imgem colorida
In [ ]:
Copied!
imagem = cv2.imread("bola.png")
image = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB)
plt.imshow(image, vmin=0, vmax=255); plt.show()
plt.hist(image.ravel(),256,[0,256]); plt.show()
imagem = cv2.imread("bola.png")
image = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB)
plt.imshow(image, vmin=0, vmax=255); plt.show()
plt.hist(image.ravel(),256,[0,256]); plt.show()
In [ ]:
Copied!
#histograma Vermelho
plt.imshow(image[:,:,0], cmap="gray", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,0].ravel(),256,[0,256]); plt.show()
#histograma Vermelho
plt.imshow(image[:,:,0], cmap="gray", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,0].ravel(),256,[0,256]); plt.show()
In [ ]:
Copied!
# Histogrma Verde
plt.imshow(image[:,:,1], cmap="Greys_r", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,1].ravel(),256,[0,256]); plt.show()
# Histogrma Verde
plt.imshow(image[:,:,1], cmap="Greys_r", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,1].ravel(),256,[0,256]); plt.show()
In [ ]:
Copied!
# Histograma Azul
plt.imshow(image[:,:,2], cmap="Greys_r", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,2].ravel(),256,[0,256]); plt.show()
# Histograma Azul
plt.imshow(image[:,:,2], cmap="Greys_r", vmin=0, vmax=255); plt.show()
plt.hist(image[:,:,2].ravel(),256,[0,256]); plt.show()
In [ ]:
Copied!
image2 = image.copy()
gray_r = image2[:,:,0]
gray_g = image2[:,:,1]
gray_b = image2[:,:,2]
img_bola = image2.copy()
for y in range(0, image2.shape[0]):
for x in range(0, image2.shape[1]):
if gray_g[y][x] <= 230:
img_bola[y][x]= 0
if gray_b[y][x] >= 240:
img_bola[y][x]= 0
plt.imshow(img_bola, interpolation="none")
plt.show()
image2 = image.copy()
gray_r = image2[:,:,0]
gray_g = image2[:,:,1]
gray_b = image2[:,:,2]
img_bola = image2.copy()
for y in range(0, image2.shape[0]):
for x in range(0, image2.shape[1]):
if gray_g[y][x] <= 230:
img_bola[y][x]= 0
if gray_b[y][x] >= 240:
img_bola[y][x]= 0
plt.imshow(img_bola, interpolation="none")
plt.show()
Desafio 1¶
Faça a seguimentação da bolinha de cor laranja. Dica use 2 canais de cores para conseguir seguimentar.
In [ ]:
Copied!
# Implemente seu código
# Implemente seu código
Desafio 2¶
Faça a seguimentação da bolinha para a imagem "bolinha.png".
In [ ]:
Copied!
imagem = cv2.imread("bolinha.png")
image = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB)
plt.imshow(image, vmin=0, vmax=255); plt.show()
imagem = cv2.imread("bolinha.png")
image = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB)
plt.imshow(image, vmin=0, vmax=255); plt.show()
In [ ]:
Copied!
# Implemente seu código
# Implemente seu código
In [ ]:
Copied!
%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import numpy as np
import time as t
print ("OpenCV Version : %s " % cv2.__version__)
%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import numpy as np
import time as t
print ("OpenCV Version : %s " % cv2.__version__)
OpenCV Version : 4.5.5
In [ ]:
Copied!
webcam = cv2.VideoCapture(0) # pode acontecer do ID da camera não ser 0, ai precisa testar com outros numeros
webcam = cv2.VideoCapture(0) # pode acontecer do ID da camera não ser 0, ai precisa testar com outros numeros
In [ ]:
Copied!
t.sleep(3) # Espera a webcam ficar pronta
t.sleep(3) # Espera a webcam ficar pronta
In [ ]:
Copied!
val, image = webcam.read()
val, image = webcam.read()
In [ ]:
Copied!
val # Checa se um frame chegou
val # Checa se um frame chegou
Out[ ]:
True
In [ ]:
Copied!
webcam.release() # fecha a webcam
webcam.release() # fecha a webcam
In [ ]:
Copied!
plt.imshow(image)
plt.imshow(image)
Out[ ]:
<matplotlib.image.AxesImage at 0x252ed548df0>
Desafio 3¶
Faça uma foto sua.