MATLAB: Avoid poor quality matlab videos - matlab

I create a video of my plot series as below:
clc;
clear;
close all;
r0=1;
theta=0:0.01:2*pi;
a=0.2;
psi=0;
writerObj = VideoWriter('shapes.mp4'); % Name it.
writerObj.FrameRate = 1; % How many frames per second.
open(writerObj);
for k=1:3
pause(1);
r=r0+a*sin(k*theta+psi)+a*sin((k+1)*theta+psi)+a*sin((k+2)*theta+psi)+a*sin((k+3)*theta+0.8*pi);
figure();
x=r.*cos(theta);
y=r.*sin(theta);
plot(x,y);
axis([-2 2 -2 2]);
% axis equal
grid on
%%%%%%%%% Finding Area %%%%%%%%%%%%%
A = polyarea(x,y);
title({'line1',['Area = ' num2str(A)]})
% title(['Area = ' num2str(A)]);
set(gcf,'renderer','zbuffer');
view(2)
frame = getframe(gcf);
writeVideo(writerObj, frame);
close all
end
close(writerObj); % Saves the movie.
But when you watch the video it has poor quality (by poor quality I mean, the quality when you export matlab plots in a jpeg format for instance).
For matlab plots people usually export in .eps format so that they save high quality plots and use them in their report or presentations.
What is an equivalent way (.eps in plot saving) for video making in matlab to preserve a good quality.

Related

Eliminate all vertical and diagonal lines in MATLAB

Here is the input image 5.png:
Here is my code:
clear all; close all; clc;
%Input Image
A = imread('C:\Users\efu\Desktop\5.png');
% figure, imshow(A);
C=medfilt2(A,[3 5]);
% figure,imshow(C);
D=imfill(C);
% figure,imshow(D);
%Image obtained using MATLAB function 'edge'
E=edge(D,'canny',[0.01 .02],3);
figure, imshow(E); title('Image obtained using MATLAB function');
image=E;
img=im2bw(image);
% imshow(img)
se = strel('line',3,0);
zz = imerode(img,se);
figure, imshow(zz);
Output after canny edge detection:
After Eroding:
Here the problem is that after eroding all horizontal edges are broken, but I don't want that. I want to extract all horizontal lines without breaking, besides want to remove all vertical and diagonal lines.
Please someone modify the code.
It's a hack but it works. A short note, I would advise against using clear all. It has some negative effects such as clearing references to your mex functions. I'm not really sure what that means but there seems to be no real use for it unless you want to clear global variables.
Basically what I did here was threefold. I added a gaussian filter to give a bit more tolerance, increased the erosion rate of strel, and searched across angles to give a bit of angle tolerance. In the end, you have a bit more than you started with on the horizontal part but it does clear the image up a lot better. If you wanted you could just add up the zz matrices and threshold it to get a new binary image which could be a bit closer to your original. Really enjoyed the question by the way and made me look forward to image processing in the fall.
clear; close all;
%Input Image
A = imread('5.png');
% figure, imshow(A);
C=medfilt2(A,[3 5]);
% figure,imshow(C);
D=imfill(C);
% figure,imshow(D);
%Image obtained using MATLAB function 'edge'
E=edge(D,'canny',[0.01 .02],4);
figure, imshow(E); title('Image obtained using MATLAB function');
image=E;
img=double(image);
img = imgaussfilt(img,.5);
% imshow(img)
zz_out = zeros(size(img));
% se = strel('line',3,-90);
% zz = imerode(img,se);
% se2 = strel('line',3,0);
% zz2 = imerode(img,se2);
for ii = -5:.1:5
se = strel('line',20,ii);
zz = imerode(img,se);
zz_out = or(zz,zz_out);
end
% zz_out = img-zz;
figure, imshow(zz_out);

MATLAB enlarge 3D plot without enlarging colorbar

Is it possible to enlarge a 3D patch without altering the size of the colorbar? My original picture is like this one:
I just want to enlarge the block in the middle of the figure keeping the colorbar unchanged. I used zoom(1.9) before, it is good but since I need to use a for loop to read sequential files to plot different graphs and make video, the graph became larger and larger so it seems that I need another method to enlarge the plot.
Update: my codes
set(gcf,'Renderer','zbuffer'); %eliminate unnecessary background and prevent stationary video - Important!
vid = VideoWriter('afation.avi'); %Create a avi file
vid.Quality = 100;
vid.FrameRate = 15;
open(vid); %Open the avi file so that films can be put into it later on
for ii=FirstFile:FileInterval:LastFile
fid = fopen(filename,'r');
datacell = textscan(fid, '%f%f%f%f%f%f%f%f'); %There are 8 columns to be read so there are 8 %f
fclose(fid);
all_data = cell2mat(datacell); %converted into a matrix containing all the dis. density info. for every simulation cell
M=zeros(NumBoxX,NumBoxY,NumBoxZ); %create a matrix of 50x50x5,representing array of simulation cells
% % the following loops assign the dislocation density from all_data to M
for i=1:NumBoxX
for j=1:NumBoxY
for k=1:NumBoxZ
num=k+NumBoxZ*(j-1)+NumBoxZ*NumBoxY*(i-1);
M(i,j,k)=all_data(num,ValCol); %the ValCol column of all_data is dislocation density
end
end
end
indPatch=1:numel(M);
[F,V,C]=ind2patch(indPatch,M,'v'); %Call the function ind2patch in order to plot 3D cube with color
title('AA in different cells','fontsize',20);%set title \sigma_{xx}
xlabel('y','fontsize',20);ylabel('x','fontsize',20); zlabel('z','fontsize',20); hold on;
set(get(gca,'xlabel'),'Position',[5 -50 30]); %set position of axis label
set(get(gca,'ylabel'),'Position',[5 50 -15]);
set(get(gca,'zlabel'),'Position',[64 190 -60]);
patch('Faces',F,'Vertices',V,'FaceColor','flat','CData',C,'EdgeColor','k','FaceAlpha',0.5);
axis equal; view(3); axis tight; axis vis3d; grid off;
colormap(cMap);
caxis([MinDis MaxDis]); %set the range of the colorbar %caxis([min(M(:)) max(M(:))]); %range of the colorbar according to one file only
cb = colorbar; % create the colorbar
set(get(cb,'title'),'string','aaty(m^{-2})','fontsize',20);
lbpos = get(cb,'title'); % get the handle of the colorbar title
set(lbpos,'units','normalized','position',[0,1.04]);
MyAxes=gca;
set(MyAxes,'Units','Normalized','position',[0.05,0.1,0.8,0.8]);
zoom(1.9);
writeVideo(vid, getframe(gcf)); %get the picture and put in the avi file with the handle "vid"
end
close(vid); %close the avi file after putting all the films into it
% winopen('ggggon.avi'); %play the movie
I put the zoom in the for loop because I need to enlarge the picture one by one and put them into my avi file to make movie.

Why "lighting phong" is a must in order to create aminated GIF/movie successfully?

I have the following Matlab code, which is used for gif and avi animation creation;
Matlab imwrite is used to create frames of the gif; but I found lighting command is critical in order to successfully obtain the animated gif.
close all
clear all
clc; %
nFrames = 50;
% Preallocate movie structure.
mov(1:nFrames) = struct('cdata', [],'colormap', []);
% Create movie.
figure('color','white');
Z = peaks(150); surf(Z,Z,'edgecolor','none');
view(3);
axis vis3d tight equal off;
v = axis;
%
for k = 1:nFrames
clf;
surf(1.5*sin(2*pi*k/20)*Z,Z,'edgecolor','none');
axis(v);
axis off
camlight;
lighting phong
mov(k) = getframe;
[Inx,cmap]=rgb2ind(mov(k).cdata,256);
if k==1
imwrite(Inx,cmap,'testoutx.gif','gif','DelayTime',0.25,'LoopCount',Inf) %
else
imwrite(Inx,cmap,'testoutx.gif','gif','WriteMode','append','DelayTime',0.25) %
end
end
% Create AVI file.
movie2avi(mov, 'myPeaks.avi', 'compression', 'None');
When I comment "lighting phong", then the obtained gif is static, not animated. It seems only one of the frames is stored. I also tried to use drawnow before each getframe sentence, but that did not work. Why? How can I solve the problem without using lighting?
The desired gif (obtained from the code above):
The undesired gif with "lighting phong" commented:

MATLAB Create Movie

** I figured out how to create the movie so the code has been changed to reflect the correct one in case it is useful for anyone in the future.
This script creates a movie of a eqdconic map and saves it in avi format. The movie will run through 1255 frames. It also plots a dot at a certain point on the image, places a changing title on the movie to show what month is being run through, and has a colorbar on the right side.
Some of the variables used were created elsewhere. The code for creating them has been left out to condense the code (and since they will not be useful for anyone else other than myself).
% Create movie
nFrames = 34; % Number of frames
for k = 1:nFrames
% Eqdconic script
% Define figure and axes
fg1 = figure(1);
axesm('MapProjection','eqdconic', 'MapParallels', [], 'MapLatLimit',[-80 -59],'MapLonLimit',[190 251]) % 60-70S and 120-160W
framem on; gridm on; mlabel on; plabel on; hold all;
% Plot data
frame = dataPoint_movie(:,:,k);
image = contourfm(lat,lon,frame, 'LineStyle', 'none');
hold on
% Plot dot
plotm(-66.75,224,'k.','MarkerSize',30);
% Colorbar
caxis([0 100]);
h = colorbar;
ylabel(h,'Percent');
% Title: Days 1:1258 inclusive. 20100101 to 20130611
date = datenum(2009, 12, 31) + k; % Convert t into serial numbers
str = datestr(date, 'mmm yyyy'); % Show in the format mmm yyyy so title changes only once a month
title(str);
mov(k) = getframe(gcf); % gca would give only the image. gcf places the title and other attributes on the movie.
end
close(gcf)
% % Save as AVI file
movie2avi(mov, 'SeaIceConcentration.avi', 'compression', 'none', 'fps', 2);
I prefer to export my movies from matlab to a .avi file.
before the for loop, initialize your movie:
vidObj = VideoWriter('Movie.avi');
vidObj.FrameRate=23;
open(vidObj);
then get your frame in the for loop:
A = getframe;
writeVideo(vidObj,A);
(note, i'm not saving each frame in a matrix, so A is an MxN matrix)
Then write out your movie after the for loop
close(vidObj);
The movie will be in your current working directory. you can open using quicktime or some other avi player. To change the frame rate (speed) of your movie, edit the second line of code. 23 fps is a good smooth movie framerate.
Check the consistency of the statements:
A = dataPoint(:,:,t);
and
A(i) = getframe;
A is overwritten all the time So that you will at best get the last frame.

movie2avi And Surf Plots [duplicate]

What are the possibilities to create videos in Matlab? I was searching and found mainly 3 toolboxes that work in this field, image processing, image acquisition and control vision... but how can I do it without them, just to create the video from scratch? I'm interested in various approaches to have an overview, but I was unable to find any decent tutorial or consistent source of info on the internet.
Thanks for the help!
Here are some of the different ways to create movies in (core) MATLAB.
MOVIE2AVI
(deprecated, use VIDEOWRITER instead)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# preallocate
nFrames = 20;
mov(1:nFrames) = struct('cdata',[], 'colormap',[]);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
mov(k) = getframe(gca);
end
close(gcf)
%# save as AVI file, and open it using system video player
movie2avi(mov, 'myPeaks1.avi', 'compression','None', 'fps',10);
winopen('myPeaks1.avi')
AVIFILE
(deprecated, use VIDEOWRITER instead)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# create AVI object
nFrames = 20;
aviobj = avifile('myPeaks2.avi', 'fps',10);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
aviobj = addframe(aviobj, getframe(gca));
end
close(gcf)
%# save as AVI file, and open it using system video player
aviobj = close(aviobj);
winopen('myPeaks2.avi')
VIDEOWRITER
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# create AVI object
nFrames = 20;
vidObj = VideoWriter('myPeaks3.avi');
vidObj.Quality = 100;
vidObj.FrameRate = 10;
open(vidObj);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
writeVideo(vidObj, getframe(gca));
end
close(gcf)
%# save as AVI file, and open it using system video player
close(vidObj);
winopen('myPeaks3.avi')
IMWRITE
(technically not a movie, but an animated GIF image)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# preallocate
nFrames = 20;
f = getframe(gca);
[f,map] = rgb2ind(f.cdata, 256, 'nodither');
mov = repmat(f, [1 1 1 nFrames]);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
f = getframe(gca);
mov(:,:,1,k) = rgb2ind(f.cdata, map, 'nodither');
end
close(gcf)
%# create GIF and open
imwrite(mov, map, 'myPeaks4.gif', 'DelayTime',0, 'LoopCount',inf)
winopen('myPeaks4.gif')
There is http://www.mathworks.de/help/techdoc/ref/videowriterclass.html
My approach is to print the single frames/figures to png files using the print function giving them filenames like 1.png, 2.png, ... and then I use the free FFMPEG converter to make a video.
ffmpeg -r 20 -i %d.png foo.avi
This allows for a lot of finetuning, when it comes to the parameters of the conversion (bitrate, codec, geometry etc.).
Matlab has a built in 'movie' command to play movies. I find it pretty easy to work with. I've used it on plots to show changes in time, as well as individual images to make a real movie.
http://www.mathworks.com/help/techdoc/ref/movie.html
I believe the general procedure is:
for ii=1:100
plot(something(ii))
F = getframe;
end
movie(F)
To save a movie, you can use a similar procedure as above, but use the
writeVideo
command.
http://www.mathworks.com/help/techdoc/ref/videowriterclass.html
QTWriter
To export QuickTime movies, my own QTWriter is available: http://horchler.github.io/QTWriter/. It works very similarly to Matlab's VideoWriter class, but has both lossy and lossless still image codecs (compression formats) that work well with typical data in Matlab plots (i.e., no inter-frame compression). Notably it also supports alpha channel transparency ('Photo PNG' codec), looping (two kinds), and continuously variable frame rates. QTWriter is written as a single Matlab class file and should work on all platforms, but I have not tested it on Windows.
Here's some example code illustrating how a simple looping, variable frame-rate QuickTime movie can be generated:
% Prepare new movie file using the default PNG compression
movObj = QTWriter('peaks.mov');
% Create an animation
hf = figure; Z = peaks; surfc(Z); frames = 100;
axis tight; set(hf,'DoubleBuffer','on');
set(gca,'nextplot','replacechildren');
% Animate plot and write movie
for k = 0:frames
hs = surfc(sin(2*pi*k/frames)*Z,Z);
set(hs,'FaceColor','interp','FaceLighting','phong');
light('Position',[0 0 4]);
movObj.FrameRate = k; % Vary the frame-rate
writeMovie(movObj,getframe(hf)); % Write each frame to the file
end
movObj.Loop = 'backandforth'; % Set palindromic looping flag
close(movObj); % Finish writing movie and close file
The output movie, another more complex demo, and further details are available on the project website. QTWriter is open source (BSD license) and the code repository is hosted on GitHub.