I have following code which basically trying to create a video based on a series images of size 256 X 256 X 3, where 3 is the RGB channls:
close all
clc
%% creating image volum
img = phantom(256);
img = repmat(img, [1,1,3]);
figure(1)
vidfile = VideoWriter('testVideo.mp4','MPEG-4');
open(vidfile);
%% loop to ceate images with different colors
for n = 1:120
a = n/120;
img(:,:,1) = img(:,:,2)*a;
img(:,:,3) = img(:,:,2)*(1-a);
imagesc(img), axis off
set(gca, 'Position', [0 0 1 1])
drawnow
videoFrame(n) = getframe(gcf);
writeVideo(vidfile,videoFrame(n));
end
close(vidfile)
However, although I got rid of the margin, I cannot get the image aspect ratio right. The image looks like this:
The goal is to remove the margin of the plot and maintain the aspect ratio of the images. In this case, it should be a square image with size 256 X 256 that looks like this:
Any pointers? Some sample code would be very nice. Thanks!
I'm not sure exactly what you're looking for. The phantom dataset is square, but the phantom image does not occupy the whole dataset; instead, there is a margin on the left and right sides that is greater than on the top and bottom sides. Simply add one line of truesize to the data to plot it as-is will maintain the aspect ratio.
...
imagesc(img), axis off
set(gca, 'Position', [0 0 1 1])
truesize(gcf,[256,256]) % <<<< Just add this
drawnow
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
However, you need to do an additional step if you want a circular phantom without a margin.
img = phantom(256);
img = repmat(img(12:245,41:216), [1,1,3]); % <<<< I changed this line
figure(1)
vidfile = VideoWriter('testVideo.mp4','MPEG-4');
open(vidfile);
%% loop to ceate images with different colors
for n = 1:120
a = n/120;
img(:,:,1) = img(:,:,2)*a;
img(:,:,3) = img(:,:,2)*(1-a);
imagesc(img), axis off
set(gca, 'Position', [0 0 1 1])
truesize(gcf,[256,256]) % <<<< I changed this line
drawnow
videoFrame(n) = getframe(gcf);
writeVideo(vidfile,videoFrame(n));
end
close(vidfile)
That's a chubby phantom.