<jupyter_start><jupyter_text>**Processamento de Imagens e Imagens**Engenharia da Computação - 2021.01**<NAME>, 828.507**  Tarefa 05 - Segmentação GrabCut- Objetivo    - Aplicar o algoritmo GrabCut para segmentação de imagens.  Download das imagens<jupyter_code>!mkdir data
!wget 'https://images.unsplash.com/photo-1547038577-da80abbc4f19?fit=crop&w=400&q=80' -O 'data/car_desert.jpg'
!wget 'https://scontent.faqa1-1.fna.fbcdn.net/v/t31.18172-8/23157300_748608245335260_5143530780480619757_o.jpg?_nc_eui2=AeHLvKBam-3PeKF7mnU1KZ6kT7bm9nnoE7RPtub2eegTtChlWXZO0R7dyxPR6mRkP1E&_nc_ohc=m2ZErC2vXdgAX9VlLlW&_nc_ht=scontent.faqa1-1.fna&oh=44f8a7d94052a5ba07612be36e43c238&oe=60D30E6C' -O 'data/picture.jpg'<jupyter_output>--2021-05-26 02:31:20--  https://images.unsplash.com/photo-1547038577-da80abbc4f19?fit=crop&w=400&q=80
Resolving images.unsplash.com (images.unsplash.com)... 172.16.58.3, fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b
Connecting to images.unsplash.com (images.unsplash.com)|172.16.58.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 29106 (28K) [image/jpeg]
Saving to: ‘data/car_desert.jpg’


data/car_desert.jpg   0%[                    ]       0  --.-KB/s               
data/car_desert.jpg 100%[===================>]  28.42K  --.-KB/s    in 0s      

2021-05-26 02:31:20 (93.6 MB/s) - ‘data/car_desert.jpg’ saved [29106/29106]

--2021-05-26 02:31:20--  https://scontent.faqa1-1.fna.fbcdn.net/v/t31.18172-8/23157300_748608245335260_5143530780480619757_o.jpg?_nc_eui2=AeHLvKBam-3PeKF7mnU1KZ6kT7bm9nnoE7RPtub2eegTtChlWXZO0R7dyxPR6mRkP1E&_nc_ohc=m2ZErC2vXdgAX9VlLlW&_nc_ht=scontent.faqa1-1.fna&oh=44f8a7d94052a5ba07612be36e43c238&oe=60D30E6C
Resolving scontent.faqa1-1.fna.fbcdn.net (scontent.fa[...]<jupyter_text>Imports<jupyter_code>import math
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Code  Exercício 1Faça uma pesquisa na internet de uma imagem contendo um veículo com uma paisagem no fundo.Você pode escolher a imagem da sua preferência.Não esqueça que é necessário ter uma paisagem  no fundo.Aplique o algoritmo de segmentação GrabCut para realizar a separação entre o plano de fundo (background) e o plano da frente (foreground). Demonstre os resultados obtidos.<jupyter_code>exe1_img1 = cv.imread('data/car_desert.jpg')
exe1_img1 = cv.cvtColor(exe1_img1, cv.COLOR_BGR2RGB)
height, width = exe1_img1.shape[:2]
height, width
exe1_img2 = exe1_img1.copy()

# tamanho do retangulo
p1 = (100, 100)
p2 = (300, 200)

exe1_img2 = cv.rectangle(exe1_img2, p1, p2, (255, 0, 0), 2)

# parametros, background, foreground
mascara = np.zeros(exe1_img1.shape[:2], np.uint8)

bgModel = np.zeros((1, 65), np.float64)
fgModel = np.zeros((1, 65), np.float64)
retangulo = p1 + p2

# algoritmo GrabCut
cv.grabCut(exe1_img1, mascara, retangulo, bgModel, fgModel, 5, cv.GC_INIT_WITH_RECT)

# filtro dos pixels de background
filtro = np.where((mascara == 0) | (mascara == 2), 0, 1).astype('uint8')

exe1_img3 = exe1_img1.copy()
exe1_img3 = exe1_img3 * filtro[:, :, np.newaxis]
plt.figure(figsize=(20, 10))
plt.subplot(131), plt.imshow(exe1_img1)
plt.subplot(132), plt.imshow(exe1_img2)
plt.subplot(133), plt.imshow(exe1_img3)
plt.show()<jupyter_output><empty_output><jupyter_text>Exercício 2Escolha uma **foto** sua, ou uma **foto que contenha pessoas**, e aplique o  algoritmo de GrabCut para segmentar as pessoas contidas na foto. Demonstre os resultados obtidos.<jupyter_code>exe2_img1 = cv.imread('data/picture.jpg')
exe2_img1 = cv.resize(exe2_img1, (0, 0), fx=0.5, fy=0.5)
exe2_img1 = cv.cvtColor(exe2_img1, cv.COLOR_BGR2RGB)
height, width = exe2_img1.shape[:2]
height, width
exe2_img2 = exe2_img1.copy()

# tamanho do retangulo
p1 = (160, 30)
p2 = (750, 700)

exe2_img2 = cv.rectangle(exe2_img2, p1, p2, (255, 0, 0), 2)

# parametros, background, foreground
mascara = np.zeros(exe2_img1.shape[:2], np.uint8)

bgModel = np.zeros((1, 65), np.float64)
fgModel = np.zeros((1, 65), np.float64)
retangulo = p1 + p2

# algoritmo GrabCut
cv.grabCut(exe2_img1, mascara, retangulo, bgModel, fgModel, 5, cv.GC_INIT_WITH_RECT)

# filtro dos pixels de background
filtro = np.where((mascara == 0) | (mascara == 2), 0, 1).astype('uint8')

exe2_img3 = exe2_img1.copy()
exe2_img3 = exe2_img3 * filtro[:, :, np.newaxis]
plt.figure(figsize=(20, 10))
plt.subplot(131), plt.imshow(exe2_img1)
plt.subplot(132), plt.imshow(exe2_img2)
plt.subplot(133), plt.imshow(exe2_img3)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14123159303882196, 'fraction_numerical': 0.12427487728692548, 'mean_word_length': 5.534985422740525, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '294943', 'license': 'MIT', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2045, 'n_words': 349, 'path': 'work/work02_05_segmentacao_grabcut.ipynb', 'repo_name': 'WesGtoX/Signal-Image-Processing', 'size': '4487'}