I can't get the final video to play. It has a size of 1x1 when it should be 401x401.
I'm using R2014a
dir = '/Users/douglasbrenner/Documents/Rosen/AOSLO_MA_AVIs/';
base_name = 'media';
nimages = 11;
size = 31; %size of subimage around alignment point must be odd.
size_from_center = (size - 1)/2;
for i = 3:3%nimages
avi = [dir,base_name,int2str(i),'.avi'];
disp('Converting')
disp(avi)
a = VideoReader(avi)
v = VideoWriter('aligned.avi','Uncompressed AVI');
open(v);
n_frames = a.NumberOfFrames
figure(1);
frame = read(a,1);
imshow(frame);
[Xc(1),Yc(1)] = getpts(1);
subframe = frame(int16(Yc(1)) - size_from_center:int16(Yc(1))+size_from_center,int16(Xc(1))-size_from_center:int16(Xc(1))+size_from_center);
center = centroid(subframe,size);
yc1 = Yc(1);
xc1 = Xc(1);
Xc(1) = center(2) - size_from_center;
Yc(1) = center(1) - size_from_center;
J = imtranslate(frame,[Xc(1),Yc(1)],'cubic');
writeVideo(v,uint8(J(a.Height)))
for j = 2:5%n_frames
j
frame = read(a,j);
subframe = frame(int16(yc1) - size_from_center:int16(yc1)+size_from_center,int16(xc1)-size_from_center:int16(xc1)+size_from_center);
center = centroid(subframe,size);
Xc(j) = center(2) - size_from_center;
Yc(j) = center(1) - size_from_center;
J = imtranslate(frame,[Xc(j),Yc(j)],'cubic');
writeVideo(v,uint8(J(a.Height)))
end
%
Mx = mean(Xc)
Sx = std(Xc)
My = mean(Yc)
Sy = std(Yc)
end
close(v);
avi = 'aligned.avi';
a = VideoReader(avi);
implay(a);
thanks

 Réponse acceptée

Douglas
Douglas le 6 Août 2016
Modifié(e) : Walter Roberson le 6 Août 2016

0 votes

This code works. I now I need to learn how to have two movie players open at once and adjust the frame rates.
dir = '/Users/douglasbrenner/Documents/Rosen/AOSLO_MA_AVIs/';
base_name = 'media';
nimages = 11;
size = 31; %size of subimage around alignment point must be odd.
size_from_center = (size - 1)/2;
for i = 3:3%nimages
avi = [dir,base_name,int2str(i),'.avi'];
disp('Converting')
disp(avi)
a = VideoReader(avi)
implay(avi)
v = VideoWriter('aligned.avi','Uncompressed AVI');
open(v);
n_frames = a.NumberOfFrames
figure(1);
frame = read(a,1);
imshow(frame);
[Xc(1),Yc(1)] = getpts(1);
subframe = frame(int16(Yc(1)) - size_from_center:int16(Yc(1))+size_from_center,int16(Xc(1))-size_from_center:int16(Xc(1))+size_from_center);
center = centroid(subframe,size);
yc1 = Yc(1);
xc1 = Xc(1);
Xc(1) = center(2) - size_from_center;
Yc(1) = center(1) - size_from_center;
J = imtranslate(frame,[Xc(1),Yc(1)],'cubic');
writeVideo(v,uint8(J(1:a.Height,1:a.Width)))
for j = 2:n_frames
j
frame = read(a,j);
subframe = frame(int16(yc1) - size_from_center:int16(yc1)+size_from_center,int16(xc1)-size_from_center:int16(xc1)+size_from_center);
center = centroid(subframe,size);
Xc(j) = center(2) - size_from_center;
Yc(j) = center(1) - size_from_center;
J = imtranslate(frame,[Xc(j),Yc(j)],'cubic');
writeVideo(v,uint8(J(1:a.Height,1:a.Width)))
end
%
Mx = mean(Xc)
Sx = std(Xc)
My = mean(Yc)
Sy = std(Yc)
close(v);
end
avi = 'aligned.avi'
implay(avi);

Plus de réponses (3)

Walter Roberson
Walter Roberson le 4 Août 2016

0 votes

You create your VideoWriter within the for i loop but you do not close it until after the for i loop. That would mess up your output.

2 commentaires

In your line
writeVideo(v,uint8(J(a.Height)))
a.Height is going to be a single scalar, so you are accessing your frame data at a single location, converting the resulting scalar to a uint8 and writing out that scalar.
Douglas
Douglas le 4 Août 2016
In the workspace it still says that v is a 1x1 VideoWriter. Doesn't that suggest what to do?

Connectez-vous pour commenter.

Douglas
Douglas le 4 Août 2016

0 votes

You are right about the open statement being in the loop with the close outside but the loop only executes once. Nevertheless, I moved the close into the loop where it belongs and changed the video write to writeVideo(v,uint8(J(a.Height,a.Width))). I get a Movie Player Window with a message that says Invalid video data which is what I had before.

3 commentaires

a.Height and a.Width are scalars. J(a.Height, a.Width) is going to give you a scalar output.
Possibly you want
writeVideo(v,uint8( J(1:a.Height, 1:a.Width)))
A question: is your input RGB or is it indexed or is it grayscale ? Your code treats it as grayscale.
Douglas
Douglas le 4 Août 2016
I thought you had it but I got this error: Error using VideoWriter/writeVideo (line 383) Frame must be 1 by 1
Error in avi_to_single_frames (line 37) writeVideo(v,uint8(J(1:a.Height,1:a.Width)))
It would seem the size has to be established when v is created. Yes it's grayscale.
Walter Roberson
Walter Roberson le 4 Août 2016
Did you change both writeVideo calls? You could get that error if you missed changing the first one.

Connectez-vous pour commenter.

Douglas
Douglas le 6 Août 2016

0 votes

I tried changing the first writeVideo but couldn't figure out how.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by