I'm trying to read a video into python (be it live or pre-recorded is irrelevant) then have each frame processed using a thresholding algorithm to convert the video to a 2 colour format.
with a simple thresholding method, i get this error:
cv2.imshow('newFrame',newFrame)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'
Thresholding for images seems simple but i cant seem to convert the data produced from the thresholding method into a format that is recognised by anything further down the line.
I have included the full code below.
import numpy as np
import cv2
cap = cv2.VideoCapture('Loop_1.mov')
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
threshed = cv2.threshold(frame,50,255,cv2.THRESH_BINARY)
newFrame = np.array(threshed)
cv2.imshow('newFrame',newFrame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
#out.release()
cv2.destroyAllWindows()
The threshold function should return two parameters
retval, threshold = cv2.threshold(frame, 50, 255, cv2.THRESH_BINARY)
Related
cap = cv2.VideoCapture(1)
count = 0
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Frame', gray)
ret,thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('capture.png', thresh
Hello I am using above code to see live view on a Python program. I would like to have only a part of the live view but I couldn't do that.
Could anyone comment on how I could do that?
Not Cropped View
I would like to see only red squared area on live view.
Are you trying to access your front webcam?
Try
cap = cv2.VideoCapture(0) instead of cap = cv2.VideoCapture(1)
cv2.imwrite('capture.png', thresh) instead of cv2.imwrite('capture.png', thresh
make sure to add:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
after cv2.imwrite() or your screen will freeze up.
(I'm also not sure what your ret,thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) line is trying to do. I think that might be done wrong)
I want my code to capture a single image from webcam and process it further, like later detect colours and sobel edges and a lot more.
In short, I want to do image acquisition.
To use your webcam, you can use VideoCapture:
import cv2
cap = cv2.VideoCapture(0) # use 0 if you only have front facing camera
ret, frame = cap.read() #read one frame
print(frame.shape)
cap.release() # release the VideoCapture object.
You start the webcam, read one image and immediately release it. The frame is the image and you can preprocess it however you want. You can view the image using imshow:
cv2.imshow('image', frame)
if cv2.waitKey(0) & 0xff == ord('q'): # press q to exit
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0) # Usually if u have only 1 camera, then it's 0, if u have multiple camera then it's may be 0,1,2 ...
ret, frame = cap.read() # ret is True or False status which shows if you are success reading frame from web cam, frame is an array
# If u want to loop to read continously
ret = True
while ret:
ret, frame = cap.read()
if frame is None:
continue # this will stop the loop if we failed to read frame, because ret will be False
If this is the answer that u wanted, then it have been asked multiple times. Make sure you have tried to search the answer before you asked
cam = cv2.VideoCapture(0)
image = cam.read()[1]
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
I have been struggling for some time with the following lines of code on mac, which send me back the error
error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor"
at the line where I try to write my video out.write(out_frame)
Here is my code:
import numpy as np
import cv2
from scipy import ndimage, misc
dir_vid='/Users/qandre/Pictures/Videos/video_input.mp4'
cap = cv2.VideoCapture(dir_vid)
fps = cap.get(cv2.CAP_PROP_FPS)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
out = cv2.VideoWriter('/Users/qandre/Pictures/Videos/video_output.mp4', fourcc, int(fps), (int(w), int(h)), isColor=False)
if not out :
print("!!! Failed VideoWriter: invalid parameters")
sys.exit(1)
while(cap.isOpened()):
ret, frame = cap.read()
if ret is True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
continue
edges = cv2.Canny(gray,50,150)
out.write(edges)
# Display the resulting frame
cv2.imshow('frame',edges)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.release()
cap.release()
cv2.destroyAllWindows()
So... What can possibly be wrong?
I must precise that I only get this error when turning the frame in grey scale thanks to the cvtColor() function.
I found the answer to my problem:
.MP4 was looking for a 3-channel BGR image to write, but I was only providing it a single channel image, since I was trying to write a grayscale image
Trying doing this:
edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
converted my greyscale image to BGR image. Whilemy pixel values remain gray, this changes edges to a 3-channel image and thus now it works.
I can capture a video from the webcam and save it fine with this code
cap = cv2.VideoCapture(0)
fgbg= cv2.BackgroundSubtractorMOG()
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret,frame = cap.read()
if ret:
fgmask = fgbg.apply(frame)
out.write(frame) #Save it
cv2.imshow('Background Subtraction', fgmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break #q to quit
else:
break #EOF
cap.release()
out.release()
cv2.destroyAllWindows()
This records it as one would expect, and shows the background subtraction thing also. It saves it to output.avi. All is well. But I can't save the foreground mask, it gives me a Could not demultiplex stream error. (This line is changed in the code above).
out.write(fgmask) #Save it
Why is this? Is the fgmask not a frame like when I am reading from the capture?
Alright figured it out! Let me know if there's a more efficient way to do this or if I am missing something..
The foreground mask generated in background subtraction is an 8bit binary image, so we have to convert it to a different format. Probably a better one exists, but I used RGB
frame = cv2.cvtColor(fgmask, cv2.COLOR_GRAY2RGB)
I am following these tutorials http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video and trying to capture video from camera. Although the code functions properly, but it doesn't show anything except black screen on the frame window. I don't know why is this happening.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if cap.isOpened() == 0:
cap.open(0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I have read in some posts that OpenCV might not be supporting my camera. So i don't know what is happening exactly?