Skip to content

Instantly share code, notes, and snippets.

@jmcalvomartin
Created July 30, 2020 15:22
Show Gist options
  • Save jmcalvomartin/18e8b5a08b8d18aeac7a6ab5cd78c8ee to your computer and use it in GitHub Desktop.
Save jmcalvomartin/18e8b5a08b8d18aeac7a6ab5cd78c8ee to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import time
cap = cv2.VideoCapture(0)
time.sleep(3)
background=0
#Memorizamos el fondo antes de comenzar
for i in range(30):
ret,background = cap.read()
background = np.flip(background,axis=1)
while(cap.isOpened()):
ret, img = cap.read()
img = np.flip(img,axis=1)
#Convertimos la imagen a HSV para su saturación
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
value = (35, 35)
blurred = cv2.GaussianBlur(hsv, value,0)
# Definimos el rango inferior del color que queremos saturar
# Recordar que son los valores en HSV
lower_color = np.array([0,120,70])
upper_color = np.array([10,255,255])
mask1 = cv2.inRange(hsv,lower_color,upper_color)
# Definimos el rango superior del color que queremos saturar
lower_color = np.array([170,120,70])
upper_color = np.array([180,255,255])
mask2 = cv2.inRange(hsv,lower_color,upper_color)
# Sumamos ambas mascaras
mask = mask1+mask2
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((5,5),np.uint8))
# Remplazamos los pixeles del color saturado con los del fondo anteriormente guardado
img[np.where(mask==255)] = background[np.where(mask==255)]
cv2.imshow('Display',img)
if cv2.waitKey(10)==27:
break
#Thanks Kaustubh Sadekar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment