I think this is gonna be a adventurous idea to make a 3D video on your own in MATLAB. When first time I found this kind of trick, I was a bit shocked, coz I didn’t thought about this before and I just found this thing while doing a completely different project. So it happens to be an unwanted surprisingly byproduct of my thought and I am gonna share it with you. It’s a matter of single line code. When you will see this, you will be surprised as well.
Before starting the main discussion I want to clarify few basic things. What I am gonna show right now, this is the very primitive kind of technology behind the 3D projection image. Anyone can recall the 3D spectacles which consists of bi-colour glasses – Cyan and Magenta; more likely Blue and Red as in Fig 1. But unfortunately this kind of technology can be found only in few science museums. Modern 3D image technology has been escalated a hundreds of yards than this Red-Blue era with the advent of Polarized light and IR technology. Anyway I am just giving the examples in a single line code describing the interesting part of it so that you can only make it by yourself.
Now, apart from the MATLAB software what you basically need is the most important thing: The camera. If you don’t want any kind of trouble in coding section, then you have to have two cameras. In that case the output video will be lossless. But if you are desperate enough in coding section and are ready to loose some information of the input video, then I would encourage you to go for only one camera.
Both processes has their own advantages and disadvantages. If you go for two cameras, then the coding section is relatively easy. And total content of the videos are intact and the output video is lossless. But the two cameras should be synchronized i.e. You have to power on both the cameras at the same time and more over all the basic properties of both the cameras e.g. brightness, contrast, zoom, frame rate etc should be well matched. Though it is impossible to put on both the cameras at the same time and the frame rate of the cameras could be dissimilar, but these problems can be overcome by my previous video tricks. If you couldnot be able to power on both the cameras in synchonized way, you can just remove some of the frames using Video Cutter Program in MATLAB and match the frame rates of both using How to Change the Frame Rate of a Video in MATLAB. Now if you are able to do these properly, at the end of everything you could have two similar (but not identical) videos which are just shifted by some amounts. So how to do this, I’ll say a bit later.
But even if you don’t have two cameras, you can continue. What you only have to do is to modify your code a little bit. You have to shift every frame of the video to both right and left direction. So to shift the video in the right direction, you are gonna cut a little portion from the right and to shift into left direction, you have to cut the same amount of portion from the left. So your effective resolution of common video would be a bit less than that of input videos, that’s why it’s called lossy method.
Everything you are doing is to fulfill the objective of getting two videos which are spatially shifted by an amount in both directions. I myself, opted for the first option, as I had two cameras. So I need not to bother for the second option. But I am encouraging you to go for the second option as it’s more adventurous. I am not emphasizing on which way you are gonna take (as it’s not important). Its the coding section which is more exciting.
Now I will teach you how to operate the cameras. First try to get two cameras made by same company. In this way automatically you can solve the frame rate mismatch problem. Now manually try to make the basic picture quality settings e.g. brightness, contrast be same or almost same.
Now I will discuss what should be the minimum distance between two lenses of the cameras. It is noted that the average distance between our two eyes is almost 6.5 cm as in Fig. 2. So you need to align the two cameras in such a way so that the two lenses are apart by around 6.5 cm from each other. Thus the real effect of eyes can be made. Make a stand that can hold both the cameras so that you can handle both cameras with a degree of ease.
Now the minimum synchronization is dependent upon you. Try to power on the cameras at the same time. Record the same video using both the cameras. Make sure there should be some 3D effect on this e.g. move your hand off and on, get something closer to lenses and move it apart again, etc.. They are tolerated by errors and noises. So make them error free using my previous two video tricks. Now you have two error free videos named as vidLeft
for Left camera and vidRight
for Right camera. Make sure they are having same number of frames in it.
Now the most important part: the coding to make a 3D video out of these two. Follow the next piece of code.
MATLAB CODE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : MATLAB 3D Video maker
% Author : Arindam Bose
% Version : 1.1
% Copyright : © Arindam Bose, All rights reserved.
% License : Open Source, Freeware
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vidLeft = VideoReader('Left.avi');
vidRight = VideoReader('Right1.avi');
writerObj = VideoWriter('VDO3D.avi');
open(writerObj);
nFrames = vidLeft.NumberOfFrames;
vidHeight = vidLeft.Height;
vidWidth = vidLeft.Width;
movie3d(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap', []);
for k = 1 : nFrames
leftI3chan = read(vidLeft, k);
leftI = rgb2gray(leftI3chan);
RightI3chan = read(vidRight, k);
rightI = rgb2gray(RightI3chan);
movie3d(k).cdata = cat(3,rightI,leftI,leftI); // Line#: The main logic behind 3D video
writeVideo(writerObj,movie3d(k).cdata);
end
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight]);
movie(hf, movie3d, 1, 30);
close(writerObj);
This code will produce a Cyan-Magenta 3D video named as ‘VDO3D.avi’. In Fig. 3 I have made a screenshot of my own Left Video and Right video. And the same frame of 3D video is also shown in Fig. 3.
Now there is a small correction I must point out. Look at your 3D glass what you have. IF the left glass is Cyan and right glass is Magenta, then you have to make no change. But if your glass is just opposite what I have just mentioned, that Left glass is Magenta and Right glass is Cyan, you have to make a small change in Line#
. Replace the line with the following line.
movie3d(k).cdata = cat(3,leftI,rightI,rightI);
And you are done, now you have Magenta-Cyan 3D video. You can watch the following videos. First video will show how I had made my first 3D video myself and next video will show the LEFT, RIGHT channel videos, as well as the corresponding 3D video.
To download the source code from MATLAB CENTRAL Community Click Here: How to make a 3D video in MATLAB
Video 1: How to make a 3D video in MATLAB
Video 2: 3D Video made in MATLAB
Enjoy your own 3D video and show it to others. 🙂 Enjoy, All the best.