How do I change line thickness and color on a video

3 Ansichten (letzte 30 Tage)
Diego
Diego am 3 Nov. 2022
Kommentiert: Steven Lord am 7 Nov. 2022
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)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Diego
Diego am 7 Nov. 2022
Thank you so much Walter. My last question. What does "-1" do following LineWidth? Is this necessary?
Steven Lord
Steven Lord am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by