-
-
Save pknowledge/aa1469b7ba8cd652adb652d4359ef4f0 to your computer and use it in GitHub Desktop.
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() |
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() |
JenilDave
commented
Jun 11, 2020
Getting error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
'I tried with the below code , just variable names has been changed , but this code is not working
I don't know what is going ..I tried to figure out the issue , but unable to fix..
can some one please help me with this ...I'll really appreciate ( please ignore the indentation because comment box is not considering the indentation )
import numpy as np
import cv2
cv2.namedWindow('Tracking')
creating tracking empty function
def empty(x):
pass
creating lower hsv paramters
cv2.createTrackbar('lower_H', 'Tracking', 0, 255, empty)
cv2.createTrackbar('lower_S', 'Tracking', 0, 255, empty)
cv2.createTrackbar('lower_V', 'Tracking', 0, 255, empty)
creating the upper hsv parameters
cv2.createTrackbar('upper_H', 'Tracking', 255, 255, empty)
cv2.createTrackbar('upper_S', 'Tracking', 255, 255, empty)
cv2.createTrackbar('upper_V', 'Tracking', 255, 255, empty)
while True:
frame = cv2.imread('smarties.png')
# converting the color of the image to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# getting the lower HSV limit
lower_H = cv2.getTrackbarPos('lower_H', 'Tracking')
lower_S = cv2.getTrackbarPos('lower_S', 'Tracking')
lower_V = cv2.getTrackbarPos('lower_V', 'Tracking')
# getting the upper HSV limit
upper_H = cv2.getTrackbarPos('upper_H', 'Tracking')
upper_S = cv2.getTrackbarPos('upper_S', 'Tracking')
upper_V = cv2.getTrackbarPos('lower_V', 'Tracking')
# setting up the lower blue_limit
lower_blue = np.array([lower_H, lower_S, lower_V])
# setting up the upper blue_limit
upper_blue = np.array([upper_H, upper_S, upper_V])
Mask = cv2.inRange(hsv, lower_blue, upper_blue)
# since we want only blue color , we will apply and operation
result = cv2.bitwise_and(frame, frame, mask = Mask)
cv2.imshow('frame', frame)
cv2.imshow('mask', Mask)
cv2.imshow('result', result)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
my code is not working ..have you got any solution @taufiq1001
'I tried with the below code , just variable names has been changed , but this code is not working
I don't know what is going ..I tried to figure out the issue , but unable to fix..
can some one please help me with this ...I'll really appreciate ( please ignore the indentation because comment box is not considering the indentation )import numpy as np
import cv2cv2.namedWindow('Tracking')
creating tracking empty function
def empty(x):
passcreating lower hsv paramters
cv2.createTrackbar('lower_H', 'Tracking', 0, 255, empty)
cv2.createTrackbar('lower_S', 'Tracking', 0, 255, empty)
cv2.createTrackbar('lower_V', 'Tracking', 0, 255, empty)creating the upper hsv parameters
cv2.createTrackbar('upper_H', 'Tracking', 255, 255, empty)
cv2.createTrackbar('upper_S', 'Tracking', 255, 255, empty)
cv2.createTrackbar('upper_V', 'Tracking', 255, 255, empty)while True:
frame = cv2.imread('smarties.png') # converting the color of the image to HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # getting the lower HSV limit lower_H = cv2.getTrackbarPos('lower_H', 'Tracking') lower_S = cv2.getTrackbarPos('lower_S', 'Tracking') lower_V = cv2.getTrackbarPos('lower_V', 'Tracking') # getting the upper HSV limit upper_H = cv2.getTrackbarPos('upper_H', 'Tracking') upper_S = cv2.getTrackbarPos('upper_S', 'Tracking') upper_V = cv2.getTrackbarPos('lower_V', 'Tracking') # setting up the lower blue_limit lower_blue = np.array([lower_H, lower_S, lower_V]) # setting up the upper blue_limit upper_blue = np.array([upper_H, upper_S, upper_V]) Mask = cv2.inRange(hsv, lower_blue, upper_blue) # since we want only blue color , we will apply and operation result = cv2.bitwise_and(frame, frame, mask = Mask) cv2.imshow('frame', frame) cv2.imshow('mask', Mask) cv2.imshow('result', result) if cv2.waitKey(1) == 27: break
cv2.destroyAllWindows()
Hey,, i am also writing the same code,, but my code is also not working ..have u got the solution?
please let me know
why is source1 and source2 same as frame in line 33 when creating the mask? also why does mask need an hsv image. why cant i use the same colored image in bgr format? anyone can tell?
@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)
L_Hue = cv.getTrackbarPos('Lower_Hue', 'Tracking')
L_Saturation = cv.getTrackbarPos('Lower_Saturation', 'Tracking')
L_Value = cv.getTrackbarPos('Lower_Value', 'Tracking')
U_Hue = cv.getTrackbarPos('Upper_Hue', 'Tracking')
U_Saturation = cv.getTrackbarPos('Upper_Saturation', 'Tracking')
U_Value = cv.getTrackbarPos('Upper_Value', 'Tracking')
lower_bound = np.array([L_Hue, L_Saturation, L_Value])
upper_bound = np.array([U_Hue, U_Saturation, U_Value])
mask = cv.inRange(hsv, lower_bound, upper_bound)
result = cv.bitwise_and(img, img, mask=mask)
cv.imshow('Original', img)
cv.imshow('MASK', mask)
cv.imshow('RESULT', result)
key = cv.waitKey(0) & 0xff
if key == 27:
break
`
`
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