C# Convert Image Sequence to Video with EmguCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so welcome back today we're going to show you how to make this very simple application that you see here this is a c-sharp visual studio Windows forms application and what is this going to do well this is going to take a sequence of images and squish them all together into a single video so why would you need to take a bunch of images and squish them into a single video well in our previous video we showed you how to write an application that will make what's called a Time Lapse video and here is an example of what we created in the last video and this is basically grabbing live streaming video we showed you how to do this previously but what it's doing is every 30 seconds in our case or twice a minute it's grabbing one of the images and saving them into a folder and then what we're doing with this application we're going to show you how to build today is we're taking all of those images and squishing them together into a video so here is our folder with all of the time lapse images that we generated in the previous video in this case there's over 700 images taken every 30 seconds or twice a minute so that's like six hours worth of video frames that are all squished together and if you use this application we're going to develop today you make a video like this that it plays six hours back in maybe 30 seconds or something so um that's one reason why you might need a image sequence to video converter another reason is a topic we discussed previously where we talked about generating what's called a 3D animation and this is an example of a 3D animation I created in a free software called blender and I built this spaceship from scratch and used a background image and and this basically blender will take this animation that we develop and spit it out as a sequence of images that you then have to do what we're going to do today take all those images and make them into a video so those are just a couple reasons why you might want to make a sequence of images into a video so we're going to show you today how to write this application to convert your images into a video so let's show you how this works when I start it up it's basically got a text box and a few buttons and the first thing it does is it tells you what video writer back ends are available we're going to be using emgucv which is a c-sharp wrapper for opencv we've talked about that previously and we have on our system some back what are called back ends that are going to allow us to take those images and write them to a video file using the emgu CV or opencv video writer so this is just giving us some initial feedback on what back ends we have available for our codecs so the first thing it's going to do we're going to select what images we want to squish together into a video so we hit that and here are our images our 700 or so images so we'll select the first one and then shift select the last one and now what it's doing it is reading the 718 images and it's gonna when it's done it's going to tell us it's done and in this case probably going to take 35 seconds or something like that takes quite a while to read in those 700 images and they're all 1920 by 1080 images so there's a lot of data there so now it's done it took 33.3 seconds and now what we can do is we can press this convert to video and fairly quickly it's going to convert that to one video file and here you go it's done video file save to videoout.mp4 which is that video that we just showed so here is the resulting video out.mp4 and there is our time lapse video so let's take a look at how we can write this c-sharp application again we're going to have a text box and a few buttons and that's about it so let's go and look at the code so here is a very simple c-sharp Visual Studio Windows forms application and as we talked about before we're going to use a library called emgucv and we've talked about how to install that but that is basically a c-sharp wrapper that allows you to access the very popular opencv library now the first thing we need to do we've talked about this before is go to tools new get package manager manage nuget packages for solution and we need to download and install emview.cv by emgu Corporation emgu.cv dot bitmap by emgu Corporation and emgu.cv.runtime.windows by emgu Corporation again we showed you how to do that uh pretty straightforward but you need to download those and then it's very important that you set this up not for any CPU but for x64 in our case and to do that you're going to have to go to the configuration manager and add a new platform and we've got x64 we showed you how to do that previously it's really important or else it's not going to work so here is our basic application we've got some using statements using emgu.cv and then using systemcollections.generic we're going to make some lists system drawing and system Windows dot forms so basically the design of this as we said we're going to have to drag and drop a text box to give us some feedback and then three buttons and all we have to do is double click on those buttons to get the event handlers so the UI is really really simple I've set this up as I do virtually all of my c-sharp videos on this channel I'm using regions pound region and pound end region we've got some documentation we've got a to-do list we've got some parameters that we're going to discuss and we've got our form one initialized component that comes with the form one and then the initialized variables is a method that we're going to develop we're going to talk about that and then we've got event handlers as you can imagine we're going to have three event handlers for each of the buttons and one of the buttons is going to select the sequence the next button is going to convert that into a video file and then this is going to be an exit button and that's about it so let's first take a look at the documentation that kind of explains what this is going to do and how it's going to do it so as we mentioned before this app reads a sequence of images and converts them to a single video file it uses EMG UCD Library which is a c-sharp wrapper for opencv now presently the supported input file formats when reading the image sequence and that is a emgucv supported input file formats include bitmap BMP PNG and the ones we're going to bring in are PNG jpeg Tiff and open exr there's some others but these are the most common ones that you'll probably be using uh open exr being a high dynamic range that we've talked about in other videos now in this example we'll use the PNG input files and save the video as a DOT MP4 using the h64 codec and the msmf backend now we've talked about that in previous videos so I'm not going to go into details but we're basically going to use this h.264 codec each input image file is read into a list of mats and we talked before in emgucv it's a mat is basically a matrix or an array of pixel values so we're going to get a list of mat and in our case there's going to be over 700 mats or images in that list which is then accessed by the EMG ucv video writer class to convert that image sequence to a video file note the video writer will overwrite any existing video file with the same name also the process of reading the files can take a relatively long time for our 700 PNG files that are about four megabytes each or a total of 2.8 gigabytes it takes around 30 seconds we saw we saw 33 seconds however converting and saving the video files to this might take only a few seconds in our case it was like three to five seconds so very quickly and um as we saw when we started this up we're getting some feedback the following video writer back ends are available ffmpeg gstreamer Intel mfx msmf CV images and CVM JPEG and each one has an index number associated with that so you can specify that to the video writer what back end you're going to use again we talked about this in previous videos so that's the basic description of what we're going to do we also have it to do we want to add asynchronous threading so that when we're doing our reading we don't lock up the UI we can talk about that later here are some of the parameters we're going to use we're going to have a video writer which is an emgu.cv video writer we're going to call it video writer and as we talked before we're going to define a codec in this case we're going to use a codec h.264 and to specify that you do videowriter.4cc and enter these characters and it will then recognize this codec we want to use by returning an integer and we're going to call it codec one also we're going to choose a back end index of 1400 again we said before that the available back-ends that we will see are these and we're going to use the msmf which is index 1400 and we're going to have a list of mat or the emgucv Matt or Matrix defining each image we're going to go we're going to call it image sequence mats which is a new list of matte and then we're going to define the input directory and the output file name so the image sequence directory that we're bringing in D documents and I'm just calling it time lapse images and all of the image is going to be in that folder the output file name is going to be D documents time lapse videos and we're going to call it videoout.mp4 and then all we have is a number of files equals zero that's just to give us feedback when it's counted all the input files it will tell us how many it read in so those are the basic parameters and then next thing is going to have the standard initialized component and we're adding a methical initialize variables and here is the initialized variables and what we're doing is we are generating that list in the text box of the back ends um and we've done this before in previous video we've got an array of backends called back-ends and that's CV invoke where we're invoking an opencv method called writer back ends and that returns an array of back-ends then we're going to give some feedback with what the results are text box1.appen text video writer back ends available and then for each back end we're going to call it be in this back ends array we're going to grab the name and the ID b e dot name and beid so that we can give the user a list of the back ends and the associated ID that we're going to need so very straightforward it's going to go through and list those out and then at this point we know what the output file name is going to be we know what the index is going to be we know what codec we're going to use and we know we're going to use 30 frames per second and we're going to resize it to 1920x1080 and we're also going to use true because it is a color image so here we've instantiated the video writer again we've talked about this in previous videos so now we've got everything initialized and what's going to happen is the first thing is the user is going to check the select sequence button to read in the in our case 700 or more images and this event handler shows an open file dialog and the user selects all input image files to be converted it stores those images in a list of map so here is the way we're going to do the open file dialog using open file dialog and we're going to call it open file dialog is new open file dialog and then we're going to set that open file dialog the parameter called multi-select is true so that means we can select all 700 by shift selecting them otherwise you can only select one file so we have to select the multi-select is true open file dialog.initial directory we've already said here's the directory in the parameters for the image sequence where that's located and then restore directory equals true and here we have if open file dialog.show dialog gives you a dialog result okay if everything works then we're going to go through and start reading so we're going to also time it we're going to do a daytime we call it start is date time now and then we're going to say okay we're going to start reading the images reading image sequence of and we're using open file dialog.file names.length and that will tell us how many have been selected in the open file dialog and that's going to tell us we're reading an image sequence of in our case 718 files or images and then we're going to step through each of those and save them read them in and save them as a mat in our list of maths so for each string file an open file dialog.file names so for all 718 we're going to increment the number of files that it's read and we're going to say the input mat we're defining a mat CV invoke I am read and the I am read is what actually grabs the file from disk and we're going to read that file and we're going to convert that to a matte a matrix and we're going to call it input mat and then we're going to add that mat to our list of mats so image sequencemats.add input matte and we're going to do that in this case 718 times so our image sequence Max our list of mats is going to have 718 elements now it's not saving it it's just grabbing all those and storing them in our list of mats and then we're going to say okay now it's done doing that so date time end equals date time now and the time span is the difference between end and now so we're going to print out text box one the Pentax the elapsed time is ts.total seconds to string with an F1 so we only want one decimal point seconds so now we have stored all of the files in our list of mats called image sequence mats and then the user can go and press the next button where it takes those and sends them out to the file and this is a very simple event handler steps so each image in the list of met and writes them to the video file so for each map we're going to call it image in image sequence mats video writer.write that image and then it's going to keep writing all of those but it hasn't actually saved the file to disk it's just written them but you have to do this video right or not disposed to actually save the file and close it right so very very important this dispose method is very important since it's what actually saves the video file to disk so at this point we have read everything saved it to disk and we're going to give a little feedback a Pentax video file saved to whatever the output file name is and now it's all done so the user can then select this button exit dot click and that just does an application exit so really that's the entire application um in the future we may look at cutting down the time it takes to read in those in RK 718 images it takes about 30 35 seconds something like that so we may be able to help that by doing it either with the GPU or by doing it in a separate thread so we can still access the UI but that depends on if there's any interest but that's about it for this one if you like any of these videos I encourage you to hit the like button subscribe hit the Bell notifications but most of all please let others know that we're here so we get some views really appreciate it otherwise take care have a really good day thanks
Info
Channel: EETechStuff
Views: 1,275
Rating: undefined out of 5
Keywords:
Id: _sATvZ4Ab8A
Channel Id: undefined
Length: 17min 50sec (1070 seconds)
Published: Tue Sep 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.