How do I change line thickness and color on a video

3 views (last 30 days)
This code provides a video output of two videos for comparison purposes side by side. From the second line of code, I get a black line across the specified pixel (517). I would like to be able to change the thickness and the color of the line. I know how to do this for plots but not sure how to do that for this example.
%% Create Movie
img=cat(2,video,uint8(video_translated)); %videos put side to side for comparison purposes
img(517,:,:,:)=0; %change thickness of line and color
vidname= ['stabilize' fname '.mp4']
vidObj = VideoWriter(vidname,'MPEG-4'); %any name can be given to this
vidObj.FrameRate = 15;
open(vidObj);
for i=1:size(img,3) %Number of frames
writeVideo(vidObj,img);
end
close (vidObj)

Accepted Answer

Walter Roberson
Walter Roberson on 3 Nov 2022
img(517:517+LineWidth-1, :, :, :) = 0;
to change the width of the line (while still leaving it black)
You are doing 4 dimensional indexing there, but it is difficult for us to figure out what the four indices represent. You have the line
for i=1:size(img,3) %Number of frames
implying that the third dimension is frames. But videowriter expects one of:
  • a 2D array, for grayscale or indexed images
  • a rows x columns x 1 x frames array for multiple grayscale or indexed images
  • a rows x columns x 3 x frames array for multiple rgb images
Number of frames is never the third dimension for writeVideo
Furthermore, you loop 1 : size(img,3) but you send all of img to writeVideo(), repeating the same information multiple times.
Can we get a hint from the way you cat() the two videos? Unfortunately, no. That cat() does restrict the situation to either being one single frame consisting of a single merged image, or else to two multi-frame videos that happen to have the same number of frames... but we cannot tell whether they are rows x columns x 3 x frames or rows x columns x 1 x frames
If they happen to be rows x columns x 3 x frames then for colour you would do
img(517:517+LineWidth-1, :, 1, :) = RedComponent;
img(517:517+LineWidth-1, :, 2, :) = GreenComponent;
img(517:517+LineWidth-1, :, 3, :) = BlueComponent;
If they happen to be rows x columns x 1 x frames then you would first do
img = repmat(img, 1, 1, 3, 1);
and then do the R G B assignment.
  7 Comments
Steven Lord
Steven Lord on 7 Nov 2022
If I want a vector starting with 0 containing 7 elements (each element 1 greater than the previous), what's the last element of the vector? Is it 7 or 6? See the Wikipedia page for off-by-one error.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by