Adding black border within video (similar to padarray)

4 views (last 30 days)
Hi, I am a new user to Matlab, and I was wondering if there is a function to add a black border within a video frame such that the size of the matrix does not change. Thank you in advance!

Accepted Answer

Aurele Turnes
Aurele Turnes on 8 Aug 2014
In order to create a black border on your video frame, you can use matrix indexing methods. See the documentation page for accessing multiple elements in a matrix .
For instance, if your video frame is a N-byN matrix called VidFrame1, you would add a border of 3 pixels on the left by doing:
VidFrame1(:,1:3) = 0;
Similarly, to add a border of 3 pixels on the top, you would do:
VidFrame1(1:3,:) = 0;
For a border on the right:
VidFrame1(:,end-2:end) = 0;
And for a border on the bottom:
VidFrame1(end-2:end,:) = 0;
If your video frame is in color and VidFrame1 has a third dimension, you would modify the above code as follows for the left border:
VidFrame1(:,1:3,:) = 0;
For a full working example, try the following code:
% open the video called 'xylophone.mp4'
xyloObj = VideoReader('xylophone.mp4');
% read the frames into a 4D matrix
vidFrames = read(xyloObj);
% get the first frame which is a 3D matrix
VidFrame1 = vidFrames(:,:,:,1);
% view the frame
image(VidFrame1)
% create a 5 pixel border on the left, right, top and bottom
VidFrame1(:,1:5,:) = 0;
VidFrame1(1:5,:,:) = 0;
VidFrame1(:,end-4:end,:) = 0;
VidFrame1(end-4:end,:,:) = 0;
% view the new frame in a new window
figure
image(VidFrame1)

More Answers (2)

Keith
Keith on 9 Aug 2014
Wow thank you so so much for that detailed answer. I'm slowly learning more about matlab and its functionalities, so I would like to also thank you for pointing me in the right direction with matrix indexing.
:)

Image Analyst
Image Analyst on 9 Aug 2014
If you want a box over a live or recorded video that is playing, you can put hold on and then use plot() or line() to put up a frame/box in the overlay. Make the color black and the line width whatever you want
xBox = [x1,x2,x2,x1,x1];
yBox = [y1,y1,y2,y2.y1];
hold on;
plot(xBox, yBox, 'Color', 'k', 'LineWidth', 7);
This way might be faster since you don't actually have to change any image values and redisplay the new image - it all just happens in the overlay. The black box in the overlay "covers up" the underlying image. I do it all the time to show various region outlines over a live video stream.

Community Treasure Hunt

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

Start Hunting!