Tracker_HSV
Manually putting values for HSV model can be quite frustrating. Easiest way to deal with it is make a openCV based trackbars which allow us to adjust the values manually using a trackbar. So with the help of this, we can able to adjust different combination of values in one go.
Installation
Use the package manager pip to install Tracker_HSV
pip install Tracker_HSV
Samples
Image Mask overlay
Mask Slider
Tip: Blurring a image can make a significant imporvement.
Mask overlay Blurred Image
Mask Slider
Documents
Let’s take a look at it,
import Tracker_HSV as tr tr.tracker(filename)
- file_name : This is everything this function need, you can pass a full path of the file or a variable in which image is stored, it can work in both ways, of some fonts
This program returns
return lower_hue, lower_sat, lower_val, upper hue, upper_sat, upper_val
- llower_hue, lower_sat, lower_val: As the name suggests, its a lower value of Hue, Saturation, Value(Brightness)
- upper_hue, upper_sat, upper_val: Similarly, its an upper value of Hue, Saturation, Value(Brightness).
If anytime you forgot the which come after which then just type
import Tracker_HSV as tr tr.help()
and it will bring you these lines below
<pre class="wp-block-syntaxhighlighter-code">For full code Explanation, please visit <blockquote class="wp-embedded-content" data-secret="zvlaQRfHxf"><a href="https://py2py.com/tracker-for-hsv-model-overview-and-explanation/">Tracker for HSV model: Overview and Explanation</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="https://py2py.com/tracker-for-hsv-model-overview-and-explanation/embed/#?secret=zvlaQRfHxf" data-secret="zvlaQRfHxf" width="600" height="338" title="“Tracker for HSV model: Overview and Explanation” — Py2py" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe> Github: https://github.com/Pushkar-Singh-14/Tracker_HSV PyPI: https://pypi.org/project/Tracker-HSV/ </pre>
Explanation
Allow me to explain this module so that we can understand its working better. This is a very simple module which uses the cv2.createTrackbar() function to make a slide bar, which we can use to adjust the values
I am adding the full code here, if you want to understand the specific function or specific line then just navigate to the particular line in the explaination
import cv2 import numpy as np def tracker(filename): if isinstance(filename,str): file_name=cv2.imread(filename) else: file_name=filename def Null_Value(x): return None cv2.namedWindow("Trackbars") cv2.createTrackbar("L - H", "Trackbars", 0, 179, Null_Value) cv2.createTrackbar("L - S", "Trackbars", 0, 255, Null_Value) cv2.createTrackbar("L - V", "Trackbars", 0, 255, Null_Value) cv2.createTrackbar("U - H", "Trackbars", 179, 179, Null_Value) cv2.createTrackbar("U - S", "Trackbars", 255, 255, Null_Value) cv2.createTrackbar("U - V", "Trackbars", 255, 255, Null_Value) while True: hsv = cv2.cvtColor(file_name, cv2.COLOR_BGR2HSV) l_h = cv2.getTrackbarPos("L - H", "Trackbars") l_s = cv2.getTrackbarPos("L - S", "Trackbars") l_v = cv2.getTrackbarPos("L - V", "Trackbars") u_h = cv2.getTrackbarPos("U - H", "Trackbars") u_s = cv2.getTrackbarPos("U - S", "Trackbars") u_v = cv2.getTrackbarPos("U - V", "Trackbars") lower = np.array([l_h, l_s, l_v]) upper = np.array([u_h, u_s, u_v]) mask = cv2.inRange(hsv, lower, upper) result = cv2.bitwise_and(file_name, file_name, mask=mask) cv2.namedWindow('frame',cv2.WINDOW_NORMAL) cv2.resizeWindow('frame',(500,500)) cv2.namedWindow('mask',cv2.WINDOW_NORMAL) cv2.resizeWindow('mask',(500,500)) cv2.namedWindow('result',cv2.WINDOW_NORMAL) cv2.resizeWindow('result',(500,500)) cv2.imshow("frame", file_name) cv2.imshow("mask", mask) cv2.imshow("result", result) key=cv2.waitKey(1) if key==27: lower_hue=l_h lower_sat=l_s lower_val=l_v upper_hue=u_h upper_sat=u_s upper_val=u_v break cv2.destroyAllWindows() return lower_hue,lower_sat,lower_val,upper_hue,upper_sat,upper_val def help(): print(""" For more information please visit py2py: https://py2py.com/Tracker-for-HSV-model-Overview-and-Explanation Github: https://github.com/Pushkar-Singh-14/Tracker_HSV PyPI: https://pypi.org/project/Tracker-HSV/ """)
First of all we are importing the necessary packages
import cv2 import numpy as np
and here is our function
def tracker(filename):
Since the file name can be a full path or a variable in which an image is stored so we are using if-else statement.
if the file is string then its a path, so we open the file and save the variable name.
if its a variable name then we just transfer this variable name to module variable name for file.
if isinstance(filename,str): file_name=cv2.imread(filename) else: file_name=filename
This null_value() function returns the NULL value, we need this null value in the later step. I will explain why.
def Null_Value(x): return None
Here in the 17th line, we create a window in which we paste our images, In the next line, we create the trackbar using cv2.createTrackbar() function, and its attributes are
cv.CreateTrackbar(trackbarName, windowName, value, count, onChange)
- trackbarName: Name of the created trackbar.
- windowName: Name of the window that will be used as a parent of the created trackbar.
- value: Initial value of
pointer , this is the value wheretracker is set by default. - count: Maximal position of the slider. The minimal position is always 0.
and the reason why we need Null
- onChange – Pointer to the function to be called every time the slider changes position. . If the callback is the NULL pointer, no callbacks are called, but only value is updated.
cv2.namedWindow("Trackbars") cv2.createTrackbar("L - H", "Trackbars", 0, 179, Null_Value) cv2.createTrackbar("L - S", "Trackbars", 0, 255, Null_Value) cv2.createTrackbar("L - V", "Trackbars", 0, 255, Null_Value) cv2.createTrackbar("U - H", "Trackbars", 179, 179, Null_Value) cv2.createTrackbar("U - S", "Trackbars", 255, 255, Null_Value) cv2.createTrackbar("U - V", "Trackbars", 255, 255, Null_Value)
Here the image is converted to HSV as HSV is best suited for color detection if you want to understand why then I suggest you, read my article “We already have RGB so why we need HSV ?”
In the next bunch of lines, we store the Tracker position using cv2.getTrackbarPos and save it to respective variables
while True: hsv = cv2.cvtColor(file_name, cv2.COLOR_BGR2HSV) l_h = cv2.getTrackbarPos("L - H", "Trackbars") l_s = cv2.getTrackbarPos("L - S", "Trackbars") l_v = cv2.getTrackbarPos("L - V", "Trackbars") u_h = cv2.getTrackbarPos("U - H", "Trackbars") u_s = cv2.getTrackbarPos("U - S", "Trackbars") u_v = cv2.getTrackbarPos("U - V", "Trackbars")
here we are creating a list for lower value of color and upper value of color as
- lower(hue, saturation, value)
- upper(hue,saturation, value)
lower = np.array([l_h, l_s, l_v]) upper = np.array([u_h, u_s, u_v])
we use the inRange function to search the color that we specify in lower and upper values, the function attribute is
then we use bitwise_and operation to mask out selected image, if the mask is inverted then we can use bitwise_not(mask) to invert the mask colors.
mask = cv2.inRange(hsv, lower, upper) result = cv2.bitwise_and(file_name, file_name, mask=mask)
here we are showing the frame, mask and result
then we use the condition that if key==27(
cv2.namedWindow('frame',cv2.WINDOW_NORMAL) cv2.resizeWindow('frame',(500,500)) cv2.namedWindow('mask',cv2.WINDOW_NORMAL) cv2.resizeWindow('mask',(500,500)) cv2.namedWindow('result',cv2.WINDOW_NORMAL) cv2.resizeWindow('result',(500,500)) cv2.imshow("frame", file_name) cv2.imshow("mask", mask) cv2.imshow("result", result) key=cv2.waitKey(1) if key==27: lower_hue=l_h lower_sat=l_s lower_val=l_v upper_hue=u_h upper_sat=u_s upper_val=u_v break cv2.destroyAllWindows()
Here we are returnign the calues which is required to select the specific color in a HSV image.
return lower_hue,lower_sat,lower_val,upper_hue,upper_sat,upper_val
Here if you need and help about the program itself then you can type
import Tracker_HSV as tr tr.help()
this will show the link of this page and, github and PyPI pages as well
def help(): print(""" For more information please visit py2py: https://py2py.com/Tracker-for-HSV-model-Overview-and-Explanation Github: https://github.com/Pushkar-Singh-14/Tracker_HSV PyPI: https://pypi.org/project/Tracker-HSV/ """)
So this is all from my side, hope this module will help you someday.
If you have any doubts the please ask in the comments and I will be more then happy to clear them.