How do I change line thickness and color on a video

2 ビュー (過去 30 日間)
Diego
Diego 2022 年 11 月 3 日
コメント済み: Steven Lord 2022 年 11 月 7 日
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)

採用された回答

Walter Roberson
Walter Roberson 2022 年 11 月 3 日
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 件のコメント
Diego
Diego 2022 年 11 月 7 日
Thank you so much Walter. My last question. What does "-1" do following LineWidth? Is this necessary?
Steven Lord
Steven Lord 2022 年 11 月 7 日
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.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by