How to write frames into video after applying bounding box ?

6 次查看(过去 30 天)
I have a video file from where I need to track an object in each frame. I have successfully done that using (rectangle('Position',[st.BoundingBox(1),st.BoundingBo). I have total 300 frames from that video and I need to write the images containing bounding box within the for loop.
for k=1: nframes
. . .. . .. .. .. . .. . .. .. . . .. .. . .
obj=bwareafilt(~dilated1, 1, 'largest');
figure(1);
imshow(I);
hold on
st = regionprops(obj, 'BoundingBox' );
BB = st(1).BoundingBox;
K=rectangle('Position',[st.BoundingBox(1),st.BoundingBox(2),st.BoundingBox(3),st.BoundingBox(4)],...
'EdgeColor','r','LineWidth',2 );
end

回答(1 个)

Image Analyst
Image Analyst 2021-11-28
Just burn them in. Like if you want it white
row1 = ceil(BB(2));
row2 = row1 + BB(4) - 1;
col1 = ceil(BB(1));
col2 = col1 + BB(3) - 1;
obj(row1:row2, col1) = 255;
obj(row1:row2, col2) = 255;
obj(row1, col1:col2) = 255;
obj(row2, col1:col2) = 255;
% Write this frame out to a new video file on disk.
writeVideo(videoWriterObject, obj);
See attached demos if you need more help in using writeVideo().
  2 个评论
Bipasha Kundu
Bipasha Kundu 2021-11-28
Hello Sir, this was helpful informations. because the position of the bounding box changes in every frame, i created a empty array as a placeholder at the beginning of the for loop. I was planning saving each frames into that array to write a video at the end. All of the frames are in grayscales. The problem i am runninh into after post processing when i save the image into an empty array, it not not saving with the boinding box.
placeholder = zeros([size(I,1) size(I,2) 1 nframes], class(I));
for k = 1:nframes
% processing binary image
A=pre processed original_frame
imshow(A);
hold on;
st = regionprops(obj, 'BoundingBox' );
BB = st(1).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)], 'EdgeColor','b','LineWidth',2 );
placeholder(:, :, k) = A (k) ;
end
The bounding boxes are not being saved in the placeholder array and they did a mismatch of the sizes.
Image Analyst
Image Analyst 2021-11-28
Correct. And the reason is that your code above totally ignores my suggestion. Why aren't you assigning the rows and columns to obj, and using writeVideo() like I suggested?
You can save the image to a frame movie structure instead of saving just the bare image alone, if you want. That's how I did it in the attached demo where I built a movie from individual saved image files.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by