Advertisement
Alexandre_MS

Código PDI

Oct 5th, 2022
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | Source Code | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.image as mpimg
  3. import numpy as np
  4. import cv2
  5.  
  6.  
  7. def f_imagem(img):
  8.     #Imagem
  9.     imagem = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  10.     imagem = cv2.resize(imagem, (256,256))
  11.     return imagem
  12.  
  13. def f_limiar(img):
  14.     #Aplicação do limite (Limiar) de imagem
  15.     gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
  16.     _,thresh = cv2.threshold(gray, np.mean(gray), 255, cv2.THRESH_BINARY_INV)
  17.     # plt.axis('off')
  18.     # plt.imshow(thresh)
  19.     # plt.show()
  20.     return thresh
  21.  
  22. def f_detecao_bordas(thresh):
  23.     #Detecção de Bordas
  24.     edges = cv2.dilate(cv2.Canny(thresh,0,255),None)
  25.     # plt.axis('off')
  26.     # plt.imshow(edges)
  27.     # plt.show()
  28.     return edges
  29.  
  30. def f_deteccao_contornos(edges):
  31.     #Detectando Contornos para Criar Máscara
  32.     cnt = sorted(cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2], key=cv2.contourArea)[-1]
  33.     mask = np.zeros((256,256), np.uint8)
  34.     masked = cv2.drawContours(mask, [cnt],-1, 255, -1)
  35.     # plt.axis('off')
  36.     # plt.imshow(masked)
  37.     # plt.show()
  38.     return masked
  39.  
  40. def f_segmentacao(img,mask):
  41.     #Segmentação das Regiões
  42.     dst = cv2.bitwise_and(img, img, mask=mask)
  43.     segmented = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
  44.     x, y, z = segmented.shape
  45.     for i in range(x):
  46.         for j in range(y):
  47.             if (segmented[i][j][0] != 0 and segmented[i][j][1] != 0 and segmented[i][j][2] != 0):
  48.                 segmented[i][j] = [0,255,0]
  49.     # plt.imshow(segmented)
  50.     # plt.show()
  51.     return segmented
  52.  
  53. def f_filtro(imagem):
  54.     img = f_imagem(imagem)
  55.     thresh = f_limiar(img)
  56.     edges = f_detecao_bordas(thresh)
  57.     mask = f_deteccao_contornos(edges)
  58.     segmented = f_segmentacao(img,mask)
  59.     dst = cv2.addWeighted(img, 0.5, segmented, 0.5, 0)
  60.     return dst, mask
  61.  
  62. '''-------------------------------------'''
  63. video = cv2.VideoCapture(r'C:\Users\Alexandre\PycharmProjects\pythonProject\image\video_adubo.mp4')
  64.  
  65. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  66. saida = cv2.VideoWriter('video.mp4',fourcc,30.0, (256,256))
  67.  
  68. if(video.isOpened() != True):
  69.     print('Erro ao abrir o arquivo')
  70.  
  71. while(video.isOpened()):
  72.     ret, frame = video.read()
  73.     if ret == True:
  74.         frame_filtrado, mask = f_filtro(frame)
  75.         plt.figure()
  76.         plt.imshow(frame_filtrado)
  77.         frame_filtrado = cv2.cvtColor(frame_filtrado, cv2.COLOR_BGR2RGB)
  78.         saida.write(frame_filtrado)
  79.         plt.close()
  80.     else:
  81.         break
  82. video.release()
  83. saida.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement