Incorrectly getting a blank video when using videowriter function to display data

17 views (last 30 days)
Hello, I am having a difficulty with writing and displaying a video for data that is meant to represent multiple frames that have been corrected for motion. I am essentially stabilizing the motion of a "shaky" video. I am still learning how to use Matlab within my research so this could be lack of coding experience on my end.
I am not receiving any error message but rather getting a video output of a blank video. In the first section, I am correcting the frame offsets. "Stabilized" should be the data used that I want to represent in a movie. When calling on it as a singular and averaged frames figure, I receive what I expect. The "Stabilized" file contains Y axis, X axis, RGB, and number of frames. The second section is my attempt to write a movie of my data which is where I believe I might be doing it incorrectly. I followed the examples provided in youtube and under "Help: Videowriter" in the command row, but nothing has worked. If more information is needed, please do not hesistate to ask. I greatly appreciate the help.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2022
You are not changing the display between getframe() so all of the frames would be the same.
If you have pre-computed frames stored in stabilized then write them directly, like
for i = 1 : size(stabilized,3)
currFrame = stabilized(:,:,:,i);
writeFrame(vidObj, currFrame);
end
And... you should be sure to initialize stabilized to the appropriate data type, such as
stabilized = zeros([2 2 1 1].*size(video), 'like', video);
which I suspect was the major source of your problem with the output being white.
  11 Comments

Sign in to comment.

More Answers (1)

Diego
Diego on 30 Sep 2022
The stabilized file was in double precision in the range of 0 to 255. I followed your instructions for simply writing it as a uint8() and it worked. I commented out
stabilized= zeros([2 2 1 1].*size(video), 'like', video);
My code for reference to anyone who experiences the same issue in the future is
img= uint8(stabilized; %rescale the data back to uint8 before writing it
vidObj = VideoWriter('Stabilization.mp4','MPEG-4');%any name can be given to this
vidObj.FrameRate = 15 %set this your own preference
open(vidObj);
writeVideo(vidObj,img);
close (vidObj)
Thank you so much Mr. Roberson. I am extremely grateful for your help. Your explanations were phenomenal and learned more than I anticipated learning. Not only did I get the outcome I wanted, but I also learned how to inherit the right logic for solving this MatLab issue.

Community Treasure Hunt

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

Start Hunting!