Created
March 31, 2019 22:48
-
-
Save pknowledge/aa1469b7ba8cd652adb652d4359ef4f0 to your computer and use it in GitHub Desktop.
OpenCV Python Tutorial For Beginners - Object Detection and Object Tracking Using HSV Color Space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
def nothing(x): | |
pass | |
cv2.namedWindow("Tracking") | |
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing) | |
cv2.createTrackbar("US", "Tracking", 255, 255, nothing) | |
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing) | |
while True: | |
frame = cv2.imread('smarties.png') | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
l_h = cv2.getTrackbarPos("LH", "Tracking") | |
l_s = cv2.getTrackbarPos("LS", "Tracking") | |
l_v = cv2.getTrackbarPos("LV", "Tracking") | |
u_h = cv2.getTrackbarPos("UH", "Tracking") | |
u_s = cv2.getTrackbarPos("US", "Tracking") | |
u_v = cv2.getTrackbarPos("UV", "Tracking") | |
l_b = np.array([l_h, l_s, l_v]) | |
u_b = np.array([u_h, u_s, u_v]) | |
mask = cv2.inRange(hsv, l_b, u_b) | |
res = cv2.bitwise_and(frame, frame, mask=mask) | |
cv2.imshow("frame", frame) | |
cv2.imshow("mask", mask) | |
cv2.imshow("res", res) | |
key = cv2.waitKey(1) | |
if key == 27: | |
break | |
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
def nothing(x): | |
pass | |
cap = cv2.VideoCapture(0); | |
cv2.namedWindow("Tracking") | |
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing) | |
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing) | |
cv2.createTrackbar("US", "Tracking", 255, 255, nothing) | |
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing) | |
while True: | |
#frame = cv2.imread('smarties.png') | |
_, frame = cap.read() | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
l_h = cv2.getTrackbarPos("LH", "Tracking") | |
l_s = cv2.getTrackbarPos("LS", "Tracking") | |
l_v = cv2.getTrackbarPos("LV", "Tracking") | |
u_h = cv2.getTrackbarPos("UH", "Tracking") | |
u_s = cv2.getTrackbarPos("US", "Tracking") | |
u_v = cv2.getTrackbarPos("UV", "Tracking") | |
l_b = np.array([l_h, l_s, l_v]) | |
u_b = np.array([u_h, u_s, u_v]) | |
mask = cv2.inRange(hsv, l_b, u_b) | |
res = cv2.bitwise_and(frame, frame, mask=mask) | |
cv2.imshow("frame", frame) | |
cv2.imshow("mask", mask) | |
cv2.imshow("res", res) | |
key = cv2.waitKey(1) | |
if key == 27: | |
break | |
cap.release() | |
cv2.destroyAllWindows() |
To detect the object from the image from scratch using python; Click here I found the best article https://debuggingsolution.blogspot.com/2022/02/object-detection-from-scratch-in-python.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pknowledge i did copy and paste of your code but its not working just like mine is not working
Here is my code:
`import cv2 as cv
import numpy as np
def nothing(x):
pass
cv.namedWindow('Tracking')
cv.createTrackbar('Lower_Hue', 'Tracking', 0, 255, nothing)
cv.createTrackbar('Lower_Saturation', 'Tracking', 0, 255, nothing)
cv.createTrackbar('Lower_Value', 'Tracking', 0, 255, nothing)
cv.createTrackbar('Upper_Hue', 'Tracking', 255, 255, nothing)
cv.createTrackbar('Upper_Saturation', 'Tracking', 255, 255, nothing)
cv.createTrackbar('Upper_Value', 'Tracking', 255, 255, nothing)
while True:
img = cv.imread('opencv_resources/balls.jpeg')
hsv = cv.cvtColor(img, cv.cv2.COLOR_BGR2HSV)
cv.destroyAllWindows()
`
`