HSV Pixel Values in OpenCV


HSV color space is more often used in machine vision owing to its superior performance compared to RGB color space in varying illumination levels. Often thresholding and masking is done in HSV color space. So it is very important to know the HSV values of the color which we want to filter out. HSV color space of  OpenCV is a bit complicated than other software programmes like Gimp, Photoshop etc.So  I have written a small python script to grab frames from a camera, and print the HSV value of the pixel under the cursor

Screenshot

HSV means Hue-Saturation-Value, where the Hue is the color.Saturation is the greyness, so that a Saturation value near 0 means it is dull or grey looking.And Value is the brightness of the pixel.(For a lot more info about HSV and other color spaces, go to HSL and HSV on Wikipedia)

The python code is given below . It prints the HSV values under the cursor into the terminal as well as overlays on the video as text.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# returns HSV value of the pixel under the cursor in a video stream
# author: achuwilson
# achuwilson.wordpress.com
import cv
import time
x_co = 0
y_co = 0
def on_mouse(event,x,y,flag,param):
  global x_co
  global y_co
  if(event==cv.CV_EVENT_MOUSEMOVE):
    x_co=x
    y_co=y

cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)
while True:
    src = cv.QueryFrame(capture)
    cv.Smooth(src, src, cv.CV_BLUR, 3)
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    thr = cv.CreateImage(cv.GetSize(src), 8, 1)
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    cv.SetMouseCallback("camera",on_mouse, 0);
    s=cv.Get2D(hsv,y_co,x_co)
    print "H:",s[0],"      S:",s[1],"       V:",s[2]
    cv.PutText(src,str(s[0])+","+str(s[1])+","+str(s[2]), (x_co,y_co),font, (55,25,255))
    cv.ShowImage("camera", src)
    if cv.WaitKey(10) == 27:
        break

4 thoughts on “HSV Pixel Values in OpenCV

  1. Hi there! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing
    a few months of hard work due to no backup. Do you
    have any methods to stop hackers?

  2. Great tutorial. I also recently wrote some new articles related to this topic. Check out my weblog 🙂 and please let me know your opinion.

  3. First of all Great article!,
    I am just getting started with image processing and opencv,i am fairly comfortable with c,c++and python.But i am getting stuck as i cant get hold of any good tutorials on python opencv,as all the good ones are written for opencv 1.0 in c,i dont want want to get into all the memory management issues so can you suggest any good tutorials for c++ or python for opencv

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s