Skip to content

Instantly share code, notes, and snippets.

@Luctins
Created December 3, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luctins/c74b95be4dc5a27bedbc22802402c925 to your computer and use it in GitHub Desktop.
Save Luctins/c74b95be4dc5a27bedbc22802402c925 to your computer and use it in GitHub Desktop.
OpenCV experiment
import cv2 as cv
import numpy as np
def nothing(x):
pass
#img = cv.imread('fil_img.jpg')
cap = cv.VideoCapture(0)
#hsv color is 0-179 (180) for hue, 0-255 for saturation, and 0-255 for value
cv.namedWindow('controls')
#Top Color
cv.createTrackbar('topColorH','controls',0,179,nothing)
cv.createTrackbar('topColorS','controls',0,255,nothing)
cv.createTrackbar('topColorV','controls',0,255,nothing)
#Bottom color
cv.createTrackbar('botColorH','controls',0,179,nothing)
cv.createTrackbar('botColorS','controls',0,255,nothing)
cv.createTrackbar('botColorV','controls',0,255,nothing)
sample = np.zeros((512,512,3), np.uint8)
while 1:
#get frame
grabbed, frame = cap.read()
#get slider values
top_color = np.uint8([[[ cv.getTrackbarPos('topColorH','controls'), \
cv.getTrackbarPos('topColorS','controls'), \
cv.getTrackbarPos('topColorV','controls') ]]])
bot_color = np.uint8([[[ cv.getTrackbarPos('botColorH','controls'), \
cv.getTrackbarPos('botColorS','controls'), \
cv.getTrackbarPos('botColorV','controls') ]]])
print 'top_color' , top_color
print 'bot_color' , bot_color
#set sample colors
sample[:,0:255]= cv.cvtColor(top_color, cv.COLOR_HSV2BGR)
sample[:,255:512]= cv.cvtColor(bot_color, cv.COLOR_HSV2BGR)
#11,11 is the matrix size used to blur the image
blur = cv.GaussianBlur(frame, (11,11), 0)
hsv = cv.cvtColor(blur, cv.COLOR_BGR2HSV)
result = cv.inRange(hsv, bot_color, top_color)
cv.imshow('result', result)
cv.imshow('controls', sample)
cv.imshow('original', frame)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment