how to create a video with a looming stimulus

4 visualizzazioni (ultimi 30 giorni)
Jessica
Jessica il 9 Ago 2018
I am using the below code to create a video in which I specify a certain an image that gradually gets larger each frame. As written, it takes many hours to run and often stalls out, likely because of the part where I export the image and then reload it. When I omit the image exporting and reloading steps, it's not possible to run the code because I get the error:
Error using VideoWriter/writeVideo (line 369) IMG must be of one of the following classes: double, single, uint8
Thanks for any helping in making my code more efficient!
for i=2:FramestoSave-1
img1 = imread(FaceImage);
Width=(1284/1296)*i;
Height=(Width*1352)/1284;
X1=Center_X-(Width/2);
X2=Center_X+(Width/2);
Y1=Center_Y-(Height/2);
Y2=Center_Y+(Height/2);
BlankImage=imread('Blank_Dell.jpg');
ibg2 = imshow(BlankImage);hold on
iim2 = image(img1,'XData',[X1 X2],'YData',[Y1 Y2]);
export_fig TempFigure.jpg -r96;
B=imread('TempFigure.jpg');imwritesize(B, 'TestFace.jpg', 26.663, 96); %Save file with 26.663 inch width (2560 px) and 96 dpi
CurrentFrameImage=imread('TestFace.jpg');
imgt=[CurrentFrameImage];
step(videoPlayer, imgt);%play video
writeVideo(outputVideo, imgt); %record new video
delete('TempFigure.jpg');
delete('TestFace.jpg');
end

Risposte (1)

Thorsten
Thorsten il 9 Ago 2018
Modificato: Thorsten il 9 Ago 2018
This version works without exporting. In the code you can first choose 'test' to see if the sequence of images is what you want to see in your video, and then 'create video' if it's fine and you want to actually create the video.
(If you still have trouble with large images, it may help to pre-allocate the frames of the video and to rewrite the code accordingly. See how the create a movie structure array: Read Video Files)
I = imread('../../Downloads/Mona_Lisa_headcrop.jpg');
I = imresize(I, 1/20); % work with smaller image for test
% create a video in which the images looms from 1/n its size in Nsteps to
% its full size on a background of color col
n = 10;
Nsteps = 10;
filename = 'test.avi';
% some examples of how to specify the background color
col = I(1, 1, :); % top left corner of the image
%col = intmax(class(I)); % white
col = uint8(fix(double(intmax(class(I)))*rand(1,1,3))); % random color
% note: assuming image is uint8; better would be to convert to class of I
% I found no way to do this with one command, without switch(class(I))...
% case 'unit8', ..., case 'uint16'...
B = repmat(col, [size(I,1), size(I, 2)]);
what_to_do = 'test';
what_to_do = 'create video';
if strcmp(what_to_do, 'create video')
v = VideoWriter(filename);
open(v)
end
for s = linspace(1/n, 1, Nsteps)
Is = imresize(I, s);
% determine top left corner
ij = round((size(B) - size(Is))/2);
% create ith frame Ii by inserting Is in B
Ii = B;
Ii(ij(1) + (1:size(Is,1)), ij(2) + (1:size(Is,2)), :) = Is;
switch what_to_do
case 'test'
imshow(Ii), axis off
disp('press key for next frame')
pause
case 'create video'
imshow(Ii), axis off
writeVideo(v, getframe(gcf));
otherwise, error('Unknown method.')
end
end
if strcmp(what_to_do, 'create video')
close(v)
end
disp('Press return to playback video')
pause
if strcmp(what_to_do, 'create video')
% play video to check
v = VideoReader(filename);
ax = axes;
ax.Visible = 'off';
while hasFrame(v)
F = readFrame(v);
image(F, 'Parent', ax);
ax.Visible = 'off';
pause(1/v.FrameRate);
end
clear v
end

Prodotti


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by