How to draw bounding boxes on a video when its coordinates and frame numbers are saved in excel?

13 views (last 30 days)
I have extracted all the bounding boxes for each frame of the video into excel with the followig format:
<Column 1> <Column 2> <Column 3> <Column 4> <Column 5>
<Frame no> <x_topleft> <y_topleft> <width> <height>
1 649.44 231.5 44.42 86.13
1 252.78 207.73 35.81 96.64
2 623.80 251.74 36.10 70.51
: : : : :
: : : : :
Is there a way I can write these boxes to each frame of the video? I guess it will need grabbing of each frame at a time and then writing all the boxes for that frame and so on for all the frames.
I would also like to save the output video with the bounding boxes!

Accepted Answer

Raunak Gupta
Raunak Gupta on 30 Sep 2019
Hi,
As you mentioned in the question you may read the frames from the directory using imread and then can draw bounding boxes using the rectangle. Since the excel file is there with you may loop through the frames with the frame number from first column. I can see in the example excel file that x_topleft and y_topleft is given. Here, for drawing rectangle, x_topleft will work but y coordinate must be y_bottomleft. So, subtracting the height from corresponding y_topleft will be y_bottomleft. Drawing one bounding box may be done as follows.
rectangle('Position',[x_topleft(1),y_bottomleft(1),width,height]);
Once all the bounding boxes are plotted on an image frame you may save each frame separately with some naming convention so that it is easier for creating a video after it. For creating video from above image sequence, you may refer to Convert Between Image Sequences and Video and VideoWriter.
  2 Comments
Rishabh Mittal
Rishabh Mittal on 2 Oct 2019
Thanks for your answer. I think this is a good trick to first convert a video into image sequence, then draw the rectangle on the respective images and at last compile the frames again to get a video. Nice! I think this will solve my problem.
But is it possible to do it without converting the video into image sequence? To somehow grab each frame from the video itself, match its frame number with the number in the excel and then draw the rectangles on each frame and save the new video as output?
Raunak Gupta
Raunak Gupta on 2 Oct 2019
Hi,
The VideoReader can be used to read the video directly but it also stores the video as a series of frames. You may edit the frames there itself while looping through the frames and inserting rectangles. This can be done by assigning the edited frame to a new VideoWriter object and matching the frame per second to the original video. Here since the frame numbers are given in the first column of excel sheet, looping through may become easy.
You may use Read Video by Frame Index for getting frames number wise. This process is helpful if you pipeline of work is related to generating video only. If you may need to uses the images generated after adding bounding boxes you may use the first method only.
Hope it clarifies.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!