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

조회 수: 11 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 28일
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
Walter Roberson
Walter Roberson 2022년 9월 30일
If your values in stabilized is double precision data in the range of 0 to 255, then just uint8() it and write that.
If video is uint8 then
stabilized= zeros([2 2 1 1].*size(video), 'like', video);
would make stabilized into uint8, not into double.
The code you do inside the loop that copies parts of video into stabilized will not change the datatype if you initialize stabilized the way I showed.
It is not uncommon for people to write code that looks something like
img = double(imread('flamingos.jpg'));
img = img + (randn(size(img)) * noise_std + noise_median);
The problem with that is that using double() keeps the data range 0 to 255, and then when you try to image() or writeVideo() MATLAB is going to see the double() data and believe that anything outside the range 0 to 1 is out of range. The much better way to deal with such situations would be to use
img = im2double(imread('flamingos.jpg'));
img = img + (randn(size(img)) * noise_std + noise_median);
and later after all of the processing is done, im2uint8() the results.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Diego
Diego 2022년 9월 30일
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.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by