Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import matplotlib.image as mpimg
- import numpy as np
- import cv2
- def f_imagem(img):
- #Imagem
- imagem = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- imagem = cv2.resize(imagem, (256,256))
- return imagem
- def f_limiar(img):
- #Aplicação do limite (Limiar) de imagem
- gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
- _,thresh = cv2.threshold(gray, np.mean(gray), 255, cv2.THRESH_BINARY_INV)
- # plt.axis('off')
- # plt.imshow(thresh)
- # plt.show()
- return thresh
- def f_detecao_bordas(thresh):
- #Detecção de Bordas
- edges = cv2.dilate(cv2.Canny(thresh,0,255),None)
- # plt.axis('off')
- # plt.imshow(edges)
- # plt.show()
- return edges
- def f_deteccao_contornos(edges):
- #Detectando Contornos para Criar Máscara
- cnt = sorted(cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2], key=cv2.contourArea)[-1]
- mask = np.zeros((256,256), np.uint8)
- masked = cv2.drawContours(mask, [cnt],-1, 255, -1)
- # plt.axis('off')
- # plt.imshow(masked)
- # plt.show()
- return masked
- def f_segmentacao(img,mask):
- #Segmentação das Regiões
- dst = cv2.bitwise_and(img, img, mask=mask)
- segmented = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
- x, y, z = segmented.shape
- for i in range(x):
- for j in range(y):
- if (segmented[i][j][0] != 0 and segmented[i][j][1] != 0 and segmented[i][j][2] != 0):
- segmented[i][j] = [0,255,0]
- # plt.imshow(segmented)
- # plt.show()
- return segmented
- def f_filtro(imagem):
- img = f_imagem(imagem)
- thresh = f_limiar(img)
- edges = f_detecao_bordas(thresh)
- mask = f_deteccao_contornos(edges)
- segmented = f_segmentacao(img,mask)
- dst = cv2.addWeighted(img, 0.5, segmented, 0.5, 0)
- return dst, mask
- '''-------------------------------------'''
- video = cv2.VideoCapture(r'C:\Users\Alexandre\PycharmProjects\pythonProject\image\video_adubo.mp4')
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
- saida = cv2.VideoWriter('video.mp4',fourcc,30.0, (256,256))
- if(video.isOpened() != True):
- print('Erro ao abrir o arquivo')
- while(video.isOpened()):
- ret, frame = video.read()
- if ret == True:
- frame_filtrado, mask = f_filtro(frame)
- plt.figure()
- plt.imshow(frame_filtrado)
- frame_filtrado = cv2.cvtColor(frame_filtrado, cv2.COLOR_BGR2RGB)
- saida.write(frame_filtrado)
- plt.close()
- else:
- break
- video.release()
- saida.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement