I have a problem in MatLab. I try to read a video, and play it, but I only read a part of the movie, and I can't control the frame rate.
The idea is to treat the image after that, and do real-time processing, so I can't save the image in a file to create a video later.
Here is my code :
videoFReader = vision.VideoFileReader('movie.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoPlayer, videoFrame);
end
release(videoPlayer);
release(videoFReader);
This video contains 2521 frames, and I only read the 372 first frames.
Thanks in advance for your help !
Edit : Can this be a codec problem ?
Okay, if this can help anyone, I found out that the 64 bits video codec aren't right, and were the problems. It seems that if I want to solve the problem, going back to matlab 32 bits is the solution...
Yes, you can change the frame rate. Simply add a statement
pause(0.04);
in the end of the loop;
It will let you have 25 frames per second. Also try to use small videos in matlab.
Related
The Logitech C910 webcam spec indicates image and video capture. Because image and video capture are listed separately, I am assuming that they are encoded and sent differently: effectively forming two different 'channels' to select from. If this understanding is incorrect, please respond with an explanation of the true nature.
This reference indicates a maximum frame-rate of 15 because of windows.
My search returned webcam video acquisition that comprises a series of time lapsed images 'stitched' together
% Connect to the webcam.
cam = webcam
% Open Video File
vidWriter = VideoWriter('frames.avi');
open(vidWriter);
% Write images file
for index = 1:20
% Acquire frame for processing
img = snapshot(cam);
% Write frame to video
writeVideo(vidWriter,img);
end
%Close file and cam
close(vidWriter);
clear cam
MATLAB has captured successfully images with the C910.
Question
If possible within MATLAB, how does one configure the webcam's **video* frame rate and save the video stream to .avi or the like? (not writing still images to video file as depicted above).
Perhaps someone with experience or sharper Google skills can provide an example of bridging the webcam's video (vs the image) stream into MATLAB. Any example that can be tested is greatly appreciated.
A good way to 'jumpstart' or accelerate newbie familiarity is with the image acquisition tool:
imaqtool
The tool seems to be GUI wrapper that reduces the command-line syntax to a GUI. Note that the bottom right panel shows the command line equivalent of a GUI interaction.
Configurable video capture paramaters comprise:
Resolution & ROI (Region of Interest)
Frame Rate
Video type (.avi .mp4) etc.
I would like to know how I can record a video and how to change the recording framerate in MATLAB with my webcam using Support Package for USB Webcams (not using toolboxes, including Image Acquisition tool box). I am thinking about using it because that package is free.
The code I made is like the below. I tried to set 2 frames/sec (fps) by v.FrameRate=2 but that code doesn't work (the recorded framerate was about 15 fps and the recording ended on about 10 sec), and I was not able to change fps in this way. Does my webcam work in Matlab Support Package for USB Webcams? How do I solve it?
function webcam_recordingvideo()
cam = webcam;
preview(cam)
v = VideoWriter('frames.avi');
get(v);
v.FrameRate=2;
open(v);
frames = 150; % frame number to get
for i = 1:frames
img = snapshot(cam); % Acquire frame for processing
writeVideo(v, img); % Write frame to video
end
close(v);
clear cam
I'm using Matlab 2016a and Logitech HD Webcam C615.
I am newly working with Matlab. Now, I'm doing a small project about image processing. I want to read a video frame by frame and find intensity of each frame. Help me,please
Read video code :
** It can't use hasFrames **
error : Undefined function 'hasFrame' for input arguments of type 'VideoReader'.
Thank you very much.
There is no such thing as hasFrames in MATLAB for the VideoReader class.
There is a function called hasFrame in MATLAB: http://www.mathworks.com/help/matlab/ref/videoreader.hasframe.html, but it only exists as of R2014b. The documentation for R2014b was just released last week, and I prematurely said that there wasn't a function called hasFrame because I have never seen it with the VideoReader class that I am used to. I currently use R2013a. Check out this thread from MathWorks for more details: http://www.mathworks.com/matlabcentral/answers/157477-unable-to-read-and-playback-movie-file
This error is probably happening because you don't have the latest version of MATLAB. If you want to read a frame from the video file using the VideoReader class, use the readFrame function after you read in the video from file: http://www.mathworks.com/help/matlab/ref/videoreader.readframe.html. Don't use hasFrame. You can always check to see if the matrix that is returned from readFrame is empty which could symbolize whether your video has a frame available or not.
Calling readFrame will provide you a video frame where you can do your analysis. You call it each time to grab the next frame in the video.
Good luck!
I am trying to employ a chroma key algorithm on a live video. I need to take a live webcam input, process it in real time and display it. I already have the chroma key algorithm working on images.
How do I process the webcam input and display it immediately. I have tried using snapshot() and passing the image to the chroma key algorithm but it is too slow even if I increase the rate of snapshots. I want a smooth output.
[ Also, if there is a better alternative than Matlab please let me know. ]
Instead of using getsnapshot() which connects to the camera and disconnects on EVERY single frame (thus slow framerates), try to use videoinput and then preview the connection: http://www.mathworks.de/de/help/imaq/preview.html
This example is made for you:
http://www.mathworks.de/products/imaq/code-examples.html?file=/products/demos/shipping/imaq/demoimaq_LiveHistogram.html
As shown you can even define a callback-handler-function which is called on every newly received frame.
You must set TriggerType to manual, or else getsnapshot() will create (and destroy) a conection to the camera everytime you need a frame. By setting it to manual, you can start the camera once, get the frames and stop the camera when you're done.
Here is an example:
vidobj = videoinput('winvideo', 1, 'RGB24_640x480');
triggerconfig(vidobj, 'manual');
start(vidobj);
while true % Or any stop condition
img = getsnapshot(vidobj);
% Process the frame.
...
imshow(img);
drawnow;
end
I am plotting a randomly walking points on the figure and trying to capture the motion at each step by getframe. After I collect all the frames, I output the result as avi with movie2avi, but the output file was so big to fit into my presentation. I am looking for a way to export the movie to mp4, anyone have any idea? I also try to use the 3rd party movie2gif, it save the size to huge extent but when I play the gif, it looks so unsmooth
In later versions of Matlab (e.g. 2012) it is done by creating and writing a video object. For example, the following code generates movie of a randomly moving circle. You can adjust the speed of the movie with the FrameRate and the size with the Quality properties. For more details see the Matlab documentation.
vobj=VideoWriter('MyMovieFile', 'Motion JPEG AVI');
vobj.FrameRate=4;
vobj.Quality=75
open(vobj);
for i=1:100
plot(rand,rand,'o')
F=getframe(gcf);
writeVideo(vobj, F);
cla(gca)
end
close(vobj)