you gotta keep 'em separated

In which we threshold video frames using HSV values.

In my previous post, I provided a tool that allows one to determine the H, S, and V values of a pixel or pixels in an image for use in setting a threshold on an image or video frame.  Today, I will show you what these values are good for, namely setting a threshold on frames in a video or webcam stream.

You may download the Python code here HSVthresholder.
The code is copyright © 2013 Matthew Witherwax and released under the BSD license.

To use the application, simply supply a command line argument for either a video file or a webcam using -v.  In the case of a webcam, specify dev# where # is the number of the device.  If you only have one webcam, you should be able to pass dev0; otherwise, you may start at dev1 and increment until you find the webcam you would like to use.  In the case of a video file, the application will loop the video indefinitely until you exit with esc.  For other options, pass -h on the command line.

The general flow of the program is:

  1. Capture frame from video or webcam
  2. Dilate image
  3. Convert to HSV
  4. Threshold image
  5. Erode image
  6. Display image

 

You can find more information about dilation and erosion here [1] and the function inRange() used for thresholding here [2].

All the interesting bits in the code are documented with comments, but I would like to draw attention to a two operations with parameters you may wish to tune.

In both dilating and erosion, a structuring element is supplied.  Possible values to adjust are the element itself which may be one of cv2.MORPH_RECT, cv2.MORPH_CROSS, or cv2.MORPH_ELLIPSE as well as the size, the second argument to cv2.getStructuringElement.  In addition, you may adjust the number of times the operation is applied by adjusting the iterations parameter.  While tuning these parameters, you might find it beneficial to create a third named window to display the frame after the dilation so you can see what effects your changes have.

Dilating

# here we dilate the image so we can better threshold the colors
if self.dilate:
	element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
	frame = cv2.dilate(frame, element, iterations=5)


Erosion

# here we erode the image to remove noise
if self.erode:
	element = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
	frameHSV = cv2.erode(frameHSV, element, iterations=2)

And here are some screenshots that show the application isolating the red grip on a pen.  The red grip show up as white while the rest of the image is blacked out.  If you look closely, you will see some stray white dots.  Depending on your needs you may either ignore these or tune the parameters until they disappear.  Now that the grip has been isolated, you can track it, but that is a topic for another post.

[1] http://docs.opencv.org/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html
[2] http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#cv2.inRange

HSVthresholder.py (6.38 kb)