OpenCV in C# for Beginners - Introduction to OpenCV in C# using Emgu!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone chris here and today we're going to start a new series where we take an introductory trip through opencv which is a computer vision library along with imgu which is a c-sharp wrapper for opencv i'm going to take you through those and we're going to kind of get the basics going i've toyed with these libraries in the past quite a bit and to be honest they're super fun they're very easy to use and once you get through the basics you can do some pretty um impressive stuff and so i wanted to make an introductory series to kind of get you guys up to speed with it and so you can start having that fun too so let's dig in okay so the first thing to really get going with this is to install the packages um so like i said we have opencv which is the general library it's it's open source and also it is available as essentially like a dll and you can use it in pretty much any language most languages have wrappers written for them and in our case we're going to since we're in c-sharp we're going to be using a wrapper called imgu e-m-g-u there's a there's a few others i believe as well but i've just always used in-goo and when we download and install mgu that actually takes care of the opencv installation um for the most part there might be one instance where we want to download it later but we'll look at that so basically we just need to install imgu now the way we install mgu is through nougat obviously if you if you view c-sharp before you probably use nougat it's a package manager for c-sharp if you're using visual studio this is going to look a little different but for me since i'm using vs code i just have to run the dot net command so dot net i believe it's add package mgu dot cv yep okay so that added mgu.cv there's actually another one we want to do as well um it is the windows runtime frame gucci so that that just added those to my cs project file um so now we have those available to use again if you're using visual studio uh you know not code um it's gonna look a little different you'll just go into nuget in in visual studio you know right click your dependencies manage nuget packages and then do browse and search for mgu and like i said you wanna you wanna install imgu and then you also want to install the windows runtime as well both are necessary okay so now we've got our project set up just to double check we can just build this real quick make sure things are running you know always suggest doing that even on the most basic hello world program just to make sure you gotta go and so we've got hello world there all set all right so now let's go ahead and add good.tv so that again pulls in and go into our project um we don't need to do anything complicated here we're just going to stay inside main and we're going to start with the matte structure so what a map is it's a matrix if you're familiar with how images are stored or represented in memory there's different kinds but in terms of when we're discussing this with opencv in general uh you're going to see a lot of these matte structures you're going to see a lot of a class called image which has two it's a tuple set two sets of uh types and uh that's that's how we're gonna represent uh an image basically an opencv is with this mat this matrix and that keeps um it keeps data on the colors of the each pixel in the image um it keeps depth type it keeps all this this other stuff so that's kind of the core structure of opencv is matte uh if you look at older versions of opencv it's it's other stuff but i think anything i want to say after like three it's mostly mats um so okay we've got a mat um and to double check let's just make sure we build it knows where everything's at um okay exit code zero great all right so well what do we do with the mat well um probably the first thing to do is to um read in an image to populate it uh you can populate it a bunch of different ways so we're going to be doing a method called i'm read and it'll take a file name and it will return okay so we need a file name all right so the the file i'll be working with in this uh course here i just downloaded starry night the band go because i think it looks cool and also um it's uh it's useful for showing a lot of stuff in terms of things you can do to images you know that that sort of thing so what we'll do is we'll pull that in so i'm going to make a directory called image and then i'm going to copy this image in i will provide this source code and i'll try to make sure and put the starter.jpg in there as well on the repository so if you need the image as well you can find it online too i mean it's whatever i think i got it from like one of the museum websites or something and just saved it jokes on them now i have priceless art so you know i'll make an nft um so i've got starry night here so what we're gonna do is uh we're going to read that in i believe it's yeah starting at jpeg okay i believe this is the correct file so you gotta be careful sometimes with reading and stuff when you're using um relative namings i believe this is the correct file structure um but we'll run it we'll see may have to adjust but that's okay um okay so we've got pig in our we've got our mat named pig and we're going to use this cv invoke class using its function called i'm read and then we're going to read in an image and store it in our pic map so um okay we've got that there cool that's great so let's display it back out right all this does is store it in our object but we need to show it right so what we can do there is another cv invoke method this one's called i'm show uh so there's two arguments as you can see from my little pop up here there's two arguments uh the name of the essentially will be the window it displays in and then our image of type i input array matte is of type i book array so i'm going to call this one starry night and then i'm going to pass it pick okay so there's one more thing we need to do so this code would display the image but the problem is um as soon as it's completed it's going to exit the program and on exit for these opencv type libraries uh one big part of what they do is they destroy the windows because to free up the memory and such so if i run this in fact i'll just do that right now it's going to display and it disappears instantly so um not very helpful so we we do want in fact to see um we want to see the images so what we're going to do is we're going to tell it to wait on us to press a button you know sort of like you do in the in a console application for a terminal so okay uh so we've added weight key now we just need to run it we can see the image all right so i presume yep all right so that is coming through on the recording i just want to make sure because i'm recording the uh the actual application itself so anyways um all right so we have a beautiful image of starry night which is uh looks great we see our title here we gave it starry night it's up here at the stop window good to go okay so now we can press any key and press escape and it will exit it was just waiting on us to press a key okay now that we have the ability to uh read in an image save it to a matte show the image back out that's that's quite a bit done there so now i want to show a quick example of i guess what you might call image processing or image manipulation um we're going to have i think in the next video there's going to be a lot more information about this kind of stuff we're going to show a lot a lot more different things you can do but for right now i just want to show a quick example of what's known as a gaussian blur and a gaussian blur is essentially a image processing technique that you can do that it takes what's called a convolution kernel and it applies that um what it's a x by x matrix it applies that matrix of values to each pixel in the uh image and based on you know some extreme math that was figured out a long time ago you can essentially do what to the human eye at the end of it looks like a blur effect so we're gonna try that real quick um basically let's move all this down um so we've got our pick and uh what we want to do is create a new mat um we'll call this one gaussian blur and um we're just playing a matte doesn't really matter and then what we're going to do is another cv invoke obviously you can tell a lot of cv invoke methods in this this early stuff um you can do a lot of things with it i mean you can scroll through here and see there's a there's a lot of things basically these are all these kind of built-in opencv functions there's a lot more than this there's tons of other classes you can utilize as well this is kind of just the static stuff and what we're going to do is the gaussian blur now the gaussian blur has a few inputs and the first one here is the source so it's going to be your picture your the the input essentially to the the algorithm and in our case we're going to do pick the second argument is the output array uh you'll find in a lot of these opencv static method calls uh you'll pass the actual um the argument that's going to be populated instead of returning it you know by reference type thing um and in our case that's going to be the gaussian blur and then it's going to take this size so it is in it's an object of type system.drawing called size so we're gonna we're gonna do that and this will determine the output image size in our case we just want to get these size we just want it to be the same size you could use this to resize as well and so the way we're going to do that is we're going to access the pick size and the easiest way to do that is using its values pic.rows and pic.column it's important to note that uh i think in a lot of cv libraries and image processing stuff uh this might actually be backwards i i read that somewhere in the documents at one point but in our case opencv the sizes rows is the it's the width and columns is the height and then from here there's also one more argument we need and it's called the sigma x in um in this uh the the documentation uh essentially i've also heard it called the um uh like the uh what's a good word for it um i forget exactly what they call it on the the wikipedia for gaussian blur but basically it's uh it's kind of how blurry do you want it um and these values you're gonna see anywhere from one which is not very much to or if any to like 10 which will be very blurry so let's do a significant blur let's do a 7 and then um yeah so that's pretty much uh there's there's other types for the gaussian blur there's other overloaded methods um but this is this would be good enough for our purposes here so this will then store this blurred image in our gaussian blur matte so then we want to show it right so let me do this blurry night and then so we'll do that and yeah let's see if this runs should mention at this point that uh the gaussian blur is a very computationally expensive um algorithm if you're going to do it on a image that has as many pixels as this one and so it might take your computer a little bit just to get through it um so don't be surprised if it hangs a little bit um okay so you see two images we have our blurry night and our starry night our star knight is this original picture beautiful blurry night quite blurry uh this is with a seven um i assume you could give it just such a high number that it just it just evolves to a mess of colors um you know you still see some form here but this is it's fairly blurry um i'll say that the gaussian blur algorithm is also used to soften edges and things like that so just a slight blur on an image can make it look um you know kind of smoother i guess is the word which can you know make it look better depending on what you're wanting so this is just again one example of an image processing uh kind of algorithm you can do using opencv on an image and yeah there's many more where that came from we're going to cover those more in the next video but yeah so for right now i think that's pretty much where we're going to leave off in this first video we you know we created a mat which is a big thing since it's just one line of code but it is a very important object in imku and then we read in an image we displayed that image we learned how to do a gaussian blur just real quick just to kind of give an example and we learned to show them and obviously we learned this white key function which is important otherwise you wouldn't be able to see your work so all right so that right there is pretty much our quick introduction to mgu and opencv this next video that i'm about to record is going to dive much deeper into image manipulation we're going to discuss image rotation we're going to discuss how you can change the colors of the image by pixel and we're even going to build our own uh convolution kernels to kind of do these interesting algorithms to the images luckily a bunch of old guys long time ago just you know figured out a bunch of that math and we can take that math and use it and create our own kernels and do what the intention of that kernel to the image so like you know there's a specific there's specific numbers you can put in your 3x3 matrix if you want to do edge detection for instance or if you want to do you know blurs like the gaussian blur things like that so pretty cool stuff so we're going to dive into that in this next video and anyways thanks for hanging out if you don't mind please like and subscribe as always and if you want to see you know more programming tutorial content in your inbox uh other than that i will see you in the next one thank you very much
Info
Channel: Programming With Chris
Views: 8,873
Rating: undefined out of 5
Keywords: OpenCV, C-sharp, C#, Intro To Programming, Tutorial, Programming, VSCode, Emgu, opencv tutorial, OpenCV With Emgu, Introduction To Computer Vision, introduction to computer vision and image processing, OpenCV in C#
Id: YTBAjP-0Fto
Channel Id: undefined
Length: 17min 45sec (1065 seconds)
Published: Tue Jul 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.