import cv2
import numpy as np 

def nothing(x):
	pass

cap = cv2.VideoCapture(0)
cv2.namedWindow('image')
cv2.createTrackbar('H_Low','image',0,500,nothing)
cv2.createTrackbar('H_High','image',500,500,nothing)
cv2.createTrackbar('S_Low','image',0,500,nothing)
cv2.createTrackbar('S_High','image',500,500,nothing)
cv2.createTrackbar('V_Low','image',0,500,nothing)
cv2.createTrackbar('V_High','image',500,500,nothing)

while(1):
	_,frame = cap.read()
	hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
	H_l = cv2.getTrackbarPos('H_Low','image')
	H_h = cv2.getTrackbarPos('H_High','image')
	S_l = cv2.getTrackbarPos('S_Low','image')
	S_h = cv2.getTrackbarPos('S_High','image')
	V_l = cv2.getTrackbarPos('V_Low','image')
	V_h = cv2.getTrackbarPos('V_High','image')
	lower = np.array([H_l,S_l,V_l])
	print H_l
	print S_l
	print V_l
	upper = np.array([H_h,S_h,V_h])
	print H_h
	print S_h
	print V_h
	mask = cv2.inRange(hsv, lower, upper)
	mask2 = cv2.erode(mask, None, iterations=2)
	mask2 = cv2.dilate(mask2,None,iterations=2)
	res=cv2.bitwise_and(frame,frame,mask = mask2)
	
	cv2.imshow('frame',frame)
	cv2.imshow('mask',mask)
	cv2.imshow('res',res)
	cv2.imshow('mask2',mask2)
	k=cv2.waitKey(5) & 0xFF
	if k == 27:
		break
cv2.destroyAllWindows()