Video editing in MATLAB for SF2526
(There is also a Julia version of this tutorial: Video editing in Julia for SF2526)
Loading images
Images can be conveniently loaded with the imread command in MATLAB:
>> im=imread('SF2526_numerics_for_data_science.png');
>> size(im)
ans =
768 1145 3
The size command reveals that im is a 769 x 1145 x 3 tensor. The image is stored with three numbers for each pixel, red green and blue. MATLAB will display the image with the command imshow:
>> imshow(im)
The image is not stored in the standard floating points, but instead integers between 0 and 255 (called the uint8 = unsigned integer with 8 bits Links to an external site.). The pixel in the top left corner has (red,green,blue) = (250,250,250) and can be inspected like this:
>> im(1,1,:)
1x1x3 uint8 array
ans(:,:,1) =
250
ans(:,:,2) =
250
ans(:,:,3) =
250
We want to manipulate the image and prefer the standard floating points which we obtain with the "double" command:
>> imfloat=double(im);
>> imfloat(1,1,:)
ans(:,:,1) =
250
ans(:,:,2) =
250
ans(:,:,3) =
250
We will do the manipulations of the image with floating points and then reverse it to integers. (Calling imshow on imfloat will not display the correct image. You need to convert it back.) Reversing the transformation can be done with uint8-command:
>> imfloat(1:100,1:100,1)=255; % red. Lets make a top left part red
>> imfloat(1:100,1:100,2)=0; % green
>> imfloat(1:100,1:100,3)=0; % blue
Turn it into a matrix again.
>> im_new=uint8(imfloat);
>> imshow(im_new);
Vectorizing the image into a vector
A video is a sequence of images (called frames), and we want the sequence of images to be stored in a matrix. For this reason we need to turn our image (which is a tensor) into a vector. This is done by the stacking the elements, or "reshaping". The resulting vector is of size size(imfloat,1)*size(imfloat,2)*size(imfloat,3):
>> n1=size(imfloat,1); n2=size(imfloat,2); n3=3;
>> n=n1*n2*n3;
n =
2638080
>> v=reshape(imfloat,n,1);
>> size(v)
ans =
2638080 1
Later we want to reverse this transformation, which can also be done with the reshape command:
>> img_new2=reshape(v,n1,n2,n3);
Loading a sequence of images (video)
In the course files area you will find videos stored both as avi/mkv-files viewable directly in your browser, and as snapshot zip-files. In the zip-archive you will find png-files with names like testbild_snapshot_0004.png, which represent frame 4 in the video. In your for-loop where you turn the sequence of snapshots into a matrix, you need to create the filename. For this filname format, the name is most easily done with the sprintf command in matlab:
>> k=4;
>> filename=sprintf("testbild_snapshot_%04d.png",k)
filename =
"testbild_snapshot_0004.png"
>> frame_k=imload(filename);