I'm currently learning how to use Open CV for python and I am trying to write a program to see an image in real time from a webcam based off of an hsv value range. When I run the program I am able to get the webcam to work (it shows a black screen as expected) but the trackbars to adjust the hsv range are not showing for some reason. Anyone have any solutions? Thanks.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
def nothing(x):
pass
#creates three trackbars for color change
cv2.createTrackbar('H','frame',0,255,nothing)
cv2.createTrackbar('S','frame',0,255,nothing)
cv2.createTrackbar('V','frame',0,255,nothing)
while(1):
# Capture frame-by-frame
_, frame = cap.read()
#creates trackbars
h = cv2.getTrackbarPos('H','frame')
s = cv2.getTrackbarPos('S','frame')
v = cv2.getTrackbarPos('V','frame')
# Converts from BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define color strenght parameters in HSV
weaker = np.array([h+10, s+10, v+10])
stronger = np.array([h-10,s-10,v-10])
# Threshold the HSV image to obtain input color
mask = cv2.inRange(hsv, weaker, stronger)
#displays mask
cv2.imshow('Result',mask)
#terminates program
if cv2.waitKey(1) == ord('q'):
break
cv2.waitKey(0)
cv2.destroyAllWindows()
The second argument of cv2.createTrackbar('H','frame',0,255,nothing) should be the name of the window that will show the trackbars. You've used frame, but there doesn't seem to be a window named frame opened in your code. You could do so by adding
cv2.namedWindow('frame')
or by changing your display line to
cv2.imshow('frame', mask)
Related
Going through the tutorials and trying to figure this out. When I run:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(-1)
while True:
ret, frame = cap.read()
if not ret:
print("no frame")
img = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.imshow('frame', img)
I get a nice gray image as I would expect. But if I change that second to last line to
img = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
Then I get this thing with the colors all screwed up:
I get something a lot closer if I use COLOR_BGR2RGB but it's still not right. I'm usually not so blue:
I'm assuming what it's doing is reading in the image as BGR, and then applying some sort of transform on it so that it's in a different format, but when I display or save the image, it still thinks its BGR so now the image isn't displayed correctly? That would at least explain why I'm so blue. The red and blue channels just got swapped. I tried COLOR_BGR2BGR but apparently that's not a thing.
I'm brand new to OpenCV and I can't seem to find a way to do this (It probably has to do with me not knowing any of the specific lingo).
I'm looping through the frames of a video and pulling out a mask from the video where it is green-screened using inRange. I'm looking for a way to then insert an image into that location on the original frame. The code i'm using to pull the frames/mask is below.
import numpy as np
import cv2
cap = cv2.VideoCapture('vid.mov')
image = cv2.imread('photo.jpg')
# green digitally added not much variance
lower = np.array([0, 250, 0])
upper = np.array([10, 260, 10])
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('frame', frame)
# get mask of green area
mask = cv2.inRange(frame, lower, upper)
cv2.imshow('mask', mask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Use Bitwise operations for masking and related binary operations. Please check below code to see how Bitwise operations are done.
Code
import numpy as np
import cv2
cap = cv2.VideoCapture('../video.mp4')
image = cv2.imread('photo.jpg')
# green digitally added not much variance
lower = np.array([0, 250, 0])
upper = np.array([10, 260, 10])
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('frame', frame)
# get mask of green area
mask = cv2.inRange(frame, lower, upper)
notMask = cv2.bitwise_not(mask)
imagePart=cv2.bitwise_and(image, image, mask = mask)
videoPart=cv2.bitwise_and(frame, frame, mask = notMask)
output = cv2.bitwise_or(imagePart, videoPart)
cv2.imshow('output', output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
RGB bad color space
Since, you are doing color processing, I would suggest you to use appropriate color space. HSV would be a good start for you. For finding good range of HSV values, try this script.
Generating Video Output
You need a to create a video writer and once all image processing is done, write the processed frame to a new video. I am pretty sure, you cannot read and write to same video file.
Further see official docs here.
It has both examples of how to read and write video.
OpenCV with python(MAC OS X EL Capitan)
I'm trying to implement simple color detection code in python using openCV.
following is my code example for color detection.
hsvTracker.py
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
while (1):
#take each frame
_, frame = cap.read()
#Convert BGR to HSV
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
#define range of blue color in hsv
lower_blue = np.array([110,100,100])
upper_blue = np.array([130,255,255])
#Threshold the HSV image to get only blue colors
mask = cv.inRange(hsv, lower_blue, upper_blue)
#Bitwise-AND mask to the original image will give us tracked object
res = cv.bitwise_and(frame,frame,mask=mask)
cv.imshow('Frame',frame)
cv.imshow('mask',mask)
cv.imshow('res',res)
k=cv.waitKey(7) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
When i run this code it works properly and blue color object is detected. But after some time this suddenly crashes and gives following exception stack. Is it really related to camera capture issue or is it related to something else in code?
Ask me if you want more details.
I'm trying to use the slider to control my lower and upper bounds for HSV masking. I'm able to get the slider but can't get it to hold the position I set; it keeps going back to zero each time a new frame is pulled in.
import numpy as np
import cv2
def nothing(x):
pass
cap = cv2.VideoCapture(0)
while(True):
# Make a window for the video feed
cv2.namedWindow('frame',cv2.CV_WINDOW_AUTOSIZE)
# Capture frame-by-frame
ret, frame = cap.read()
# Make the trackbar used for HSV masking
cv2.createTrackbar('HSV','frame',0,255,nothing)
# Name the variable used for mask bounds
j = cv2.getTrackbarPos('HSV','image')
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of color in HSV
lower = np.array([j-10,100,100])
upper = np.array([j+10,255,255])
# Threshold the HSV image to get only selected color
mask = cv2.inRange(hsv, lower, upper)
# Bitwise-AND mask the original image
res = cv2.bitwise_and(frame,frame, mask= mask)
# Display the resulting frame
cv2.imshow('frame',res)
# Press q to quit
if cv2.waitKey(3) & 0xFF == ord('q'):
break
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
You are creating track-bar inside while loop, that's why you are getting new track-bar on each frame.
So change your code like,
# Make a window for the video feed
cv2.namedWindow('frame',cv2.CV_WINDOW_AUTOSIZE)
# Make the trackbar used for HSV masking
cv2.createTrackbar('HSV','frame',0,255,nothing)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
........................
........................
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
# Take each frame
_, frame = cap.read()
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_green, upper_green)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
Note: i'm new to open cv ,so please help guys!!!
In this program
while reading a frame , why is there the symbol ' _, ' before frame
is it a syntax??
The lowerbound and upper bound of blue color is specified.
is that RGB values or BGR values or HSV values??
How can i find lower bound and upperbound of others colors like red,green?
Please explain the process of finding values of other colour ,i tried other colours but it gave me black screen output for hsv and res!!!
Can some one change this program to detect red color or other color so i can know the difference?
This is tuple unpacking; cap.read() returns two values, we assign the first to _ (convention for "we won't be using this") and the second to frame.
The comment literally says "in hsv".
You just need to specify your own bounds, or change the ones already there, and see the difference yourself. Use an HSV converter to see what colours you are using. If the colours within your range aren't in the image you process, it will be black.