Skip to content

Instantly share code, notes, and snippets.

@asarver
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save asarver/9716537 to your computer and use it in GitHub Desktop.

Select an option

Save asarver/9716537 to your computer and use it in GitHub Desktop.
Video Filtering
import cv2
import numpy
import sys
# python hsv_filter.py minH minS minV maxH maxS maxV
if __name__=="__main__":
defaultHSV = [0,0,0,255,255,255]
hsvArgs = sys.argv[1:]
if len(hsvArgs) < 6:
hsvArgs = defaultHSV
try:
hsvArgs = map(int, hsvArgs)
except:
hsvArgs = defaultHSV
minHSV = numpy.array([hsvArgs[0], hsvArgs[1], hsvArgs[2]])
maxHSV = numpy.array([hsvArgs[3], hsvArgs[4], hsvArgs[5]])
captureObject = cv2.VideoCapture(0)
VideoObject = cv2.VideoWriter('hsv_filtering_video.avi',
cv2.cv.CV_FOURCC('M','J','P','G'), 25, (640,480))
x = cv2.waitKey(5)
while (x != 27 and x == -1):
x = cv2.waitKey(5)
_,image = captureObject.read()
hsvImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
filteredImage = cv2.inRange(hsvImage, minHSV, maxHSV)
cv2.imshow('Filtered Image', filteredImage)
# open cv having problem writing image to video as returned by cv2.inRange
# so save image to temp image, and then reread and write to vide
cv2.imwrite('temp_hsv_image.jpg', filteredImage)
videoImage = cv2.imread('temp_hsv_image.jpg')
VideoObject.write(videoImage)
cv2.destroyAllWindows()
VideoObject.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment