Undefined function or variable 'mmreader' - matlab

I try to use matlab 2016a to read avi videos, however, I get the following problems:
undefined funciton or variable 'mmreader';
The code is as following:
clc;
clear;
%% this to read avi by using mmread to get every frame
video = mmreader('D:\My Documents\MATLAB\My\fire.avi');
nFrames = video.NumberOfFrames;
H = video.Height;
W = video.Width;
Rate = video.FrameRate;
% Preallocate movie structure.
mov(1:nFrames) = struct('cdata',zeros(H,W,3,'uint8'),'colormap',[]);
%read one frame every time
for i = 1:nFrames
mov(i).cdata = read(video,i);
P = mov(i).cdata;
disp('current frame number:'),disp(i);
imshow(P),title('original picture');
% P2=rgb2gray(P);
end
Why? Could anyone help me? Thanks in advance.

The function mmreader was deprecated in version R2010b, removed in version R2014a, and removed from the documentation altogether in version R2015b. It was replaced by the VideoReader function, so use that in its place.

Related

How to get the audioread.m file again in the matlab?

I have edited the audioread.m file
I have used
which('audioread', '-all')
rmpath(fileparts(which('audioread')))
savepath
Now I have got an error
Undefined function or variable 'audioread'.
I have checked with
[d,sr] = audioread('sadness22.wav');
% Plot the spectrogram
subplot(311)
specgram(d(:,1),1024,sr);
% Read in a different format
[d2,sr] = audioread('piano.mp3');
subplot(312)
specgram(d2(:,1),1024,sr);
Can you please give any suggestions regarding this issue?

MATLAB code for combining .fig files from a folder to generate a video/ animation?

I am trying to make an animation from a sequence of figures (.fig) files of MATLAB from a folder in my system. I referred to the questions here and here.
Finally, I have the code mkvideo.m :
% Creating Video Writer Object
writerObj = VideoWriter('peqr.avi');
% Using the 'Open` method to open the file
open(writerObj);
% Creating a figure.
% Each frame will be a figure data
Z = peqr;
surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');
[figures,var] = uigetfile('*.fig','Multiselect','on');
for k = 1:length(figures)
Multi_Figs = [var,filesep,figures{k}];
Op = openfig(Multi_Figs);
% Frame includes image data
frame = getframe;
% Adding the frame to the video object using the 'writeVideo' method
writeVideo(writerObj,frame);
close(Op);
end
% Closing the file and the object using the 'Close' method
close(writerObj);
I am getting the following error
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 278)
In VideoWriter/delete (line 213)
In mkvideo (line 2)
Undefined function or variable 'peqr'.
Error in mkvideo (line 8)
Z = peqr;
I think this code should be able to produce a video once in user interface, I select the files?
It would be helpful if I could get some help to rectify the error in this code so that the animation or video making is possible.

How to save all sequentially captured image in MATLAB?

I need to save all the captured images in MATLAB but I am able to save one picture at a time.
mycam = webcam();
img = snapshot(mycam);
imwrite(img,'img.jpg');
If somebody knows how to save all the pictures taken at a time in MATLAB, please help me with the code.
I would save the images as a movie and then access the frames later. This is untested but it would work like this.
mycam = webcam();
% if you know the number of images use that here
% a movie is just a collection of frames
% if not then just don't initialize F
F(nFrames) = struct('cdata', [], 'colormap, []);
for i = 1:nFrames
F(i) = im2frame(snapshot(mycam));
end
% save F
movie2avi(F, 'MyMovie.avi', 'compression', 'None');
Then you can load the movie and look at the frames. This example uses the older movie2avi but VideoWriter is also an option
v = VideoWriter('MyMovie.avi');
open(v);
for i = 1:nFrames
writeVideo(v, snapshot(mycam));
end
close(v);
Again untested as I don't have a webcam attached to this computer. But it works for animated graphs. See doc readFrame for the way to read frames
As they have already told you, you should use a for loop with the sprintf function in order to not overwrite the previous images. Try with the following command:
%capture the frames
for i =1:n;% n is the number of frames you want to capture
frames{i} = getsnapshot(mycam);
end
%save in the current folder
for i = 1:n;
imwrite(frames{i}, sprintf('imageName%d.jpg',i))
end
You will have all the captured frames saved in the Current Folder.

reading and displaying video file frame by frame

I am newly working with Matlab. I want to read a video file and do some calculations every frame and display every frame. I wrote the following code but every time it only displays the first frame. can anybody please help.
mov=VideoReader('c:\vid\Akiyo.mp4');
nFrames=mov.NumberOfFrames;
for i=1:nFrames
videoFrame=read(mov,i);
imshow(videoFrame);
end
Note: mmreader API has been discontinued by MATLAB so prefer using VideoReader.
See comment by #Vivek.
I usually do this:
obj=mmreader('c:\vid\Akiyo.mp4');
nFrames=obj.NumberOfFrames;
for k=1:nFrames
img=read(obj,k);
figure(1),imshow(img,[]);
end
As far as your code is concerned, I saw MATLAB's documentation. You should do the things in following order:
mov=VideoReader('c:\vid\Akiyo.mp4');
vidFrames=read(mov);
nFrames=mov.NumberOfFrames;
for i=1:nFrames
imshow(vidFrames(:,:,i),[]); %frames are grayscale
end
Function read() and the field NumberOfFrames() are now deprecated, Matlab suggests using
xyloObj = VideoReader(file);
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
mov = struct('cdata',zeros(vidHeight, vidWidth, 3,'uint8'), 'colormap',[]);
while hasFrame(xyloObj)
mov(k).cdata = readFrame(xyloObj,'native');
end
In case you want to estimate a number of frames in the video, use nFrames = floor(xyloObj.Duration) * floor(xyloObj.FrameRate);
Below suggested code is showing only one frame
imshow(vidFrames(:,:,i),[]);
I am doing following things to store each frame
obj = VideoReader('path/to/video/file');
for img = 1:obj.NumberOfFrames;
filename = strcat('frame',num2str(img),'.jpg');
b = read(obj,img);
imwrite(b,filename);
end
This will store all the frames in your home directory.And yes, as also suggested by Vivek and Parag
You need to use VideoReader as mmreader has been discontinued by
MATLAB.
*=I was making a function to play any .avi file as a set of frames in a figure. Here's what a did. A bit of a combo of what you've done, except my NumberOfFrames wasn't working: (noteL this also shows it in colour)
function play_video(filename)
% play_video Play a video file
% play_video(filename) plays the video file specified by filename in a MATLAB Figure window.
figure
set(figure, 'Visible', 'on')
mov=VideoReader(filename);
vidFrames=read(mov);
duration = mov.Duration;
frame_rate = mov.FrameRate;
total_frames = duration .* frame_rate
for i=1:1:total_frames
imshow(vidFrames(:, :, :, i), []);
drawnow
end

How to make and save a video(avi) in matlab

I am learning matlab myself andI have made an animated plot using matlab;now i want to save it as an video file.can you tell me how to convert my animation into a video file in matlab.Below is my code
x=[1:2];
for i=1:25,
m=randi([3,5]);
n=randi([3,5]);
y=[m n];
bar(x,y)
axis equal
A(i) = getframe;
end
matlab version 7.8 R2009a
use avifile:
aviobj = avifile('example.avi','compression','None');
x=[1:2];
for i=1:25,
m=randi([3,5]);
n=randi([3,5]);
y=[m n];
bar(x,y)
axis equal
aviobj = addframe(aviobj,gcf);
drawnow
end
viobj = close(aviobj)
If Matlab's avifile doesn't work (it might have problems with codecs of 64-bit OS), then use mmwrite.
http://www.mathworks.com/matlabcentral/fileexchange/15881-mmwrite
It's simple, and it works. I used it to create *.wmv files simply by:
mmwrite(filename, frames);
Edit: code example
% set params
fps = 25;
n_samples = 5 * fps;
filename = 'd:/rand.wmv';
% allocate frames struct
fig = figure;
f = getframe(fig);
mov = struct('frames', repmat(f, n_samples, 1), ...
'times', (1 : n_samples)' / fps, ...
'width', size(f.cdata, 2), ...
'height', size(f.cdata, 1));
% generate frames
for k = 1 : n_samples
imagesc(rand(100), [0, 1]);
drawnow;
mov.frames(k) = getframe(fig);
end
% save (assuming mmwrite.m is in the path)
mmwrite(filename, mov);
One way to do this is to print the figure to an image, and then stitch the resulting image sequence into a video. ffmpeg and mencoder are great tools for doing this. There are some great resources for describing this if you know the right search terms. I like this one
In mencoder, you could stitch your images together with a command like:
mencoder "mf://*.jpg" -mf fps=10 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
Have a look at VideoWriter or see this forum discussion