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