Time for action – creating a 3-D video from a regular one

But what happens if we do not have two cameras and we want to experiment with the 3-D video making process? Well, we can actually be creative. A left and right image from a stereoscopic video will ideally only have a horizontal shift of some pixels. This means that if we take a simple, monocular video and shift its frames towards one horizontal direction, we can create a synthetic right, or left image. More specifically, by shifting the frame to the right, we create a synthetic left frame and shifting it to the left, we create a synthetic right frame. Let's demonstrate this and adjust the previous example to work with a regular video.

  1. First, we will open a regular video. We can use one of the videos included in MATLAB. Here, we will use the video file rhinos.avi distributed with the Image Processing Toolbox:
    >> vid = VideoReader('rhinos.avi'), % Open video file
  2. Create and open a video to write our results in:
    >> sV = VideoWriter('Synthetic3D.avi'), % Open 3D video file
    >> open(sV)
  3. Again, we must loop through all the frames:
    >> for i = 1:vid.NumberOfFrames  % For all frames
    frame = read(vid,i); % Read i-th frame
    l = circshift(frame,[0 10 0 0]); % Create synthetic left frame 
    r = circshift(frame,[0 -10 0 0]); % Create synthetic right frame 
    l(:,1:10,:) = 0; % Darken shifted part of left image
    r(:,end-9:end,:) = 0; % Darken shifted part of right image
    %Fuse the right and left channel into a red-cyan false colorimage
    v3 = imfuse(r,l,'falsecolor','ColorChannels','red-cyan'),
    writeVideo(sV ,v3); % Write frame to video
    end
    close(sV);
  4. Now, we can play back our video:
    >> implay('Synthetic3D.avi')
    Time for action – creating a 3-D video from a regular one

What just happened?

You just became familiar with the process of creating a synthetic red-cyan 3-D video using only a few steps, even if you don't have two cameras to shoot a real stereoscopic video. The process had only a few tweaks in comparison with the one followed in the previous example. The first two steps involved loading a regular video and also creating and opening a video file to save our synthetic 3-D frames in. Then, the for loop, that will load each frame of our original video, is written. In it, after loading one frame at a time, we created two synthetic right and left frames by horizontally shifting our original frame to the left and right, respectively. Then, we erased the circularly shifted areas from the right and left part of the image to avoid irregular artifacts that come from the opposite part of the image (remember that circshift brings the part of the image that falls off the borders into the opposite side of the image). Finally, we performed fusing of the two images into one and wrote it as a new frame in our output video file. After the for loop is over, we closed the output file to finalize the process.

Have a go hero – writing a function for 3-D video creation

Now that you know the process of 3-D video creation, why don't you try to turn the code given in the previous example into a function? The function should get four inputs from the user: the input video filename, the output video filename, the shifting distance in pixels, and a choice of whether the left and right parts should be darkened (as we did in the example), or cut. In the second case, the output video will not have equal dimensions to the input video.

Pop quiz –working with video frames

Q1. Which of the following are true?

  1. Subtracting two frames can result in pinpointing the moving objects in a scene, when the videos are shot in confined environments.
  2. Large moving objects with little detail in them can be mistaken as immobile when relying solely on information from frame subtraction.
  3. We can seamlessly reconstruct a frame of a video just by knowing its next frame and the Horn-Schunck optical flow between them.
  4. We can create a red-cyan stereoscopic video from a regular monocular video, just by shifting the original frames in the vertical direction.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset