From 8655c3cdd0592b65d610efd2d7fa5a617384dc5c Mon Sep 17 00:00:00 2001 From: gmartinvela Date: Sat, 30 Mar 2013 19:59:46 +0100 Subject: [PATCH] Create RGB2HSV_converter.py Create a method to convert from RGB to HSV easily in Python. The main purpose is to call the function "inRange" of cv2. Example with the GOLD one: hsv_image = cv2.cvtColor(blur_image, cv.CV_BGR2HSV) threshold_image = cv2.inRange(hsv_image, np.array((23., 100., 100.)), np.array((27., 255., 255.))) --- .../2_core/RGB2HSV_converter.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Official_Tutorial_Python_Codes/2_core/RGB2HSV_converter.py diff --git a/Official_Tutorial_Python_Codes/2_core/RGB2HSV_converter.py b/Official_Tutorial_Python_Codes/2_core/RGB2HSV_converter.py new file mode 100644 index 0000000..71ed1d1 --- /dev/null +++ b/Official_Tutorial_Python_Codes/2_core/RGB2HSV_converter.py @@ -0,0 +1,28 @@ +#RGB2HSV_converter# +# Gustavo Martin Vela - WebLab Deusto. www.weblab.deusto.es +# gustavo.martin@opendeusto.es + +import colorsys + +def RGB2HSV_converter(R, G, B): + ''' Put your RGB color here and this method return the HSV code of color prepared to use with cv2 library''' + #print "RGB %s %s %s" % (R,G,B) + #print "BGR %s %s %s" % (B,G,R) + R = R / 255.0 + G = G / 255.0 + B = B / 255.0 + #print "RGB %.2f %.2f %.2f" % (R,G,B) + H, S, V = colorsys.rgb_to_hsv(R, G, B) + #print "HSV %.2f %.2f %.2f" % (H,S,V) + H = H * 180 + S = S * 255 + V = V * 255 + #print "HSV %.2f %.2f %.2f" % (H,S,V) + return H, S, V + +# Examples of use +# GOLD Color in RGB +H, S, V = RGB2HSV_converter(255, 215, 0) +print "HSV %.2f %.2f %.2f" % (H,S,V) # HSV 25.29 255.00 255.00 +# GREEN Color in RGB +H, S, V = RGB2HSV_converter(118, 238, 0)