How to convert the video into frames and get binary images of those frames and save them individually

31 views (last 30 days)
I have a video of microscopy trial. I need to get each of the frames from the video and convert them into binary images and save each one of them in a folder. I have written code for it but there is a problem here. I cannot get frames and its corresponding binary images saved in image format in a folder.

Answers (1)

Kiran Felix Robert
Kiran Felix Robert on 22 Jul 2020
Hi AB,
It is my understanding that you need to convert each frame of your video into binary image and write it to a file in readable image format.
The video reader object can be used to read files with video data which can be found in the VideoReader documentation.
Each frame can be converted to binary using the imbinarize function (Assuming that the frame is in 2D Grayscale, rgb2gray can be used if this Is not the case).
The converted frame can be to a readable format using the imwrite function.
If A.mp4 is the video file, and suppose you want to save the frames as A_frame_<frame number>.png, as shown below,
v = Videoreader("a.mp4");
i = 0
while hasFrame(v)
frame = readFrame(v);
gray_frame = rgb2gray(frame);
binary_frame = imbinarize(gray_frame)
file = sprintf("a_frame_%d.png",i);
imwrite(binary_frame,file);
i = i + 1;
end
Thank You,
Kiran

Community Treasure Hunt

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

Start Hunting!