LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel this is opencv c plus course that will teach you everything you need to know to get started this course is based on my previous opencv course with python that now has more than a million views and 98.8 positive feedback like before we are going to learn the basics that include processing images videos webcams and even finding shapes colors humans and vehicle number plates we will also have three example projects that will cover all the basics that we have learned c plus plus is an excellent language for implementation and creating real world products so having this advanced computer skill set on your cv will really make it stand out from the competition and don't worry if you are a beginner we will go step by step right from installation up to creating exciting projects and our main focus will be practical implementation so we will skip all the boring theory stuff i upload videos on a weekly basis so don't forget to subscribe and hit that notification bell to keep updated so let's get started so let's start with introduction to images so what are images made of let's say i wanted to display the number three i will take an array of boxes where each box could be filled or empty so to write the number three we would color a few boxes to create that shape now some boxes are white in color whereas some are black we can denote all the black boxes as 0 and all the white boxes as 1. in this example we have 10 by 10 boxes if we wanted more detail we can increase the number of boxes in reality these boxes are pixels you would have heard of vga hd full hd and 4k all of these represent a fixed number of pixels for example vga is 640 by 480 hd is 1280 by 720 this means vga has 640 boxes in the width and 480 boxes or pixels in the height currently the image we drew has only two colors black and white this is known as a binary image in order to get more detail we can have an image with more levels this would mean instead of having only 0 and 1 we will have a range of values here we can see the difference between 2 6 and 16 levels or shades of gray but still the image is not very clear so we will be using an 8 bit value this will give us a resolution of 256 where 0 will be black and 255 will be white this means we now have 254 colors between white and black in other words we have 254 shades of grey this image is now known as a grayscale image for a colored image we have three grayscale images representing the intensities of red green and blue in short rgb adding these images together gives us a full color image this means a colored vga image is 640 by 480 by 3. in windows we are going to use visual studio for developing our applications so we will download the community version you can just click here and start the download once the download has begun just make sure when you are installing you use the desktop development with c plus so make sure that this is selected and then you can install so once that is done we will go to my website and in the website here i have listed down all the steps within this course so the link to this course will be in the description so you can check all these steps over here so let's go step by step so step number one is to download opencv so you will click on this link and it will take you to the github page and in the github page you will click on releases and over there you are going to click on the exe executable file so once that is downloaded you will install it now i have installed it in d so all my path will be containing the drive d now if you have installed it in c then you will have to change your links with the corresponding drive so in step number two you will add your environment variable path so this is the path that we have to add so here you can see the process so you have to double click on paths and add this so let me show you so we click on this and then we can go to the path double click on this and you can see i have added the path over here now once that is done you should restart your computer and after restarting you will create a new visual studio c plus project and you have to set the platform target to x64 so let's go ahead and do that so here we can create a new project and i will use an empty project and we can hit next and here i will write the name so we will call it open cv course this will open up our project and now let's go back to the steps so as it's mentioned here we have to change this to 64. and then we have to add the directories and we have to add the library directories and the build directories so what we can do is we can click on our project and then we will go to projects and then properties in the project properties we will go to vc plus plus directories and over here we are going to include the links so here we have two links so the first one we have to include directories and the second one we have to include the library directories so these are the two parts that we have to add again i have listed both of them here so you can copy this you can copy this and you can go here we'll click on edits and we will add a new one and we will paste this here and press ok then we will go back and we will paste copy the other one this is the library directories we will double click on this edit then add it here didn't copy properly we will go back so we'll click on this and edit and then we will go to our linker and input and over here we are going to edit and if we go back to our step so add linker input and we have to type in this opencv world now this might be different for you if you have a different version so make sure your version is the same or you use the correct version number here so if you go back here and you go to edits you can put it here now d is for debugging and without the d is for release so if you are using for debug just write d here so this file is actually present in your folder so let me show you where that is so here we go so this is the paths actually i will include this path as well it is in the bin folder so you can check the name you can see here this is the name you can check that name over here so this is the file so what you have to do is you have to write lib dot lib with this okay so let's go back and now we will go to step number five which is testing the code so we will copy all of this this is for testing an image and we will go to our course add new item and let's call this test and over here we are going to paste this and now we need the resources folder that has this test.png so we will go back to our course and over here you can see download resources we will click on that and we will click on download resources and here we can click here to download so this will download our resources we can click on that and here we have the resources folder so we will go to our project right click we will open the folder in file explorer and now we can simply paste or we can drag our resources folder here so in this resources we have all these files that we will be using within the course so then we can close both of these and now it should already link here now if you don't see the files here you can click on this button show all files and then it will show you resources and the test.p cpp so let's run this and there you go so now you can see we have successfully imported our image and we have successfully imported the opencv library now for mac we are going to use xcode to write all of our code so the first thing you want to do is to go to the app store and install xcode and then we can go and follow the steps on my website so this is again available on the course page which is available in the description below so what you will do is you will go step by step so step number one is to install homebrew so you will go to this website you will copy this and paste here so here you will copy this link and you will go to your terminal and you will paste it here now i have already done this so i will not repeat once this is done you will go to step two which is to install opencv so you will write brew install opencv again i have done this already so i will not run it again then you are going to create a new xcode project so let's go to xcode here we will create a new xcode project we will choose mac os and then command line tool and here we are going to use c plus here we can write the name so once that is done now we will add our dynamic libraries now to add this we are going to go back we are going to right click here and we will click on add files to open cv course over here we have to go to our folder to search for the folder you can click on forward slash and then you can write user local seller opencv so this will open up your current version of opencv you can double click on that and then you can go to lib and over here you will see all the dynamic libraries so if it's showing you with icons you can go to the list and over here you can sort by kind so if you do that then you can select let me click again okay so here are the libraries the dynamic libraries so if you select one of them and then you go to the end till here and while pressing shift if you click on the last one it will select all of them so now you can add so one by one all these files will be added so next we are going to search for the header search path so we are going to search for this we can copy it from here and we will go to build settings we will make sure all and combined are selected and then we will search for header search paths here in the header search path we are going to add this link now this is again the same path user local seller opencv after that you have to write your own version make sure your version is correct and then it should be include opencv 4. so i will double click on this side i will click on add and then i will paste this and make sure this is recursive so once that is done now we can run our opencv programs but the problem is that it does not allow you to add any image or use any image or camera for that we need to have access so here we can enable access to our resources folder so step number six is for that and step number seven is to enable the camera access so let's do the resources first so as we did earlier if we go back and we have to go to download and we have to click on download resources we can click on this and it will download our resources so once you have downloaded and extracted your resources folder now you can drag this folder into your xcode project so we will go to xcode and over here we will go down and we will click on opencv course right click and show in finder and here in the main cpp file we are going to drag our resources folder now we can close this and if we go back to our code so once we have completed step number six and we have added the resources folder we will go to our test code and we will select all of this and we will paste it in our main cpp file and then we will hit run so it will ask you to allow the access you will say okay and there you go so now you can see we have our image and this means that we have successfully imported opencv and the we have also allowed the access to our resources folder so to enable the camera access we need the property list file so we need to create a new property list file and we have to add the camera usage description so let's go and do that so first we will go to file new and we will create a new file then we will go down and select the property list and here we will call it info and we will hit create now you will see your info.plist here you will double click it and you will add a new item the item will be the item will be this ns camera usage description so we will double click and paste this and the value will be this string over here so once you do that you will right click and show in finder you will copy this or you can drag and then you will go to products and then open cv course and again show in finder so this will show you this debug so make sure this is visible and then you will go to your previous finder and you will drag the info p list over here and now you will close this so once we have finished the step number seven of creating the property list then we will test the code for webcam so we will copy all of this and we will go to our xcode and we will paste it here and now here i'm using one but most probably you will be using zero so this is the id number so you can try which one works for you so if we hit run and there you go so this is my webcam i have put a black tape in front of it that's why it's showing this otherwise you will see the actual image so you can press stop if it asks you for the camera access you will hit yes and this will work fine so now you are all set to get started with opencv c plus in this chapter we are going to learn how to import images videos and webcams so the first thing we will do here we are in our project and we are going to right click and you can right click anywhere and you can create a new item and we will call this chapter one so here first of all we are going to include all the header files that we require now throughout this course we will need only three header files for opencv so in one of the chapters we will need one more but when we go there i will show you how to include that as well but for the rest of them we will need only three files so we are going to include we are going to write include and then we will write opencv and we will first need our image codex dot hpp then we will need opencv and we will need the gui dot hpp and then we will need the image processing so include even though we are not using the image processing for the first uh chapter we will still need it for the upcoming chapters so we will include it now so image processing yeah so there you go then we will also include the input and output stream so input output stream there you go okay so now we can write our function so for example we can write void main and we don't want to output anything so we are writing void so we can start off by writing our path now our path will be a string so we can write here string but you can see it doesn't recognize string and the reason is that we have to use the standard library to actually define that this is a string now the same thing we have to do with opencv functions as well so for example if i'm using i am read to read images i cannot directly use it i have to write cv and then i have to write two columns so then it will recognize this function so to avoid these two things what we can do is we can write we can write using name space cv and we can write using name space standard so if we just remove these two now then it should work without any issues there you go so as i was saying we are going to first define our path so we will write string path is equals to and now we need to import our image so actually let's write down here that this part is for images so let's write importing images like that okay so here we are going to define our path now to import the image we will first load it into our project so we are going to right click on our project name and we will open up the folder and once that opens up we are going to paste our resources folder now this folder is available on my website you can download it from there so it will have all the images that are required or all the files that are required for this course we have a test video we have test image we have some for color detection some for shapes and then for word perspective and face detection number plate and all these files we will be using later on so for now we will use this test image to import and test our image so here i can now write resources slash test dot jpg is it jpg i think it's png let me check again yeah it's png so we can import it from here but now you will see that it's not available here and the reason is because it's hidden so you can click here on show all files and then you will see that we have the resources with all these files and we have the chapter one so it's optional if you want to keep it like this or like this okay so once we have the path now we are going to import our image so we will say that mat image is equals to imread and we will give in the path and we will put our semicolon so this will read the image from this path but what is this matte this mat is basically a matrix data type that is introduced by opencv to handle all the images so whenever we are talking about images we will write the data type as matte okay so then we will write i am show to show our image and we will give it a name let's say image and then we can write let's say the image itself so this is what we want to do and at the end of the day it will show but it will close automatically because we are not giving it any delay so we have to add a delay so we can write weight key and here we can write 0 for infinity so this way whenever it opens up it will not close until we actually press the close button so let's run this and there you go so this is an image from one of my videos so we are going to use this as our test image so this is how you can import an image it is very simple and it's very similar to what we have seen in python so actually i don't like to write summary here let's just remove that and right here images so now we will do the video part where we will import a video and to do that we are going to use the same file which is chapter one normally you would add a new file and you would write the code in that but i want to keep it simple and add every uh every chapter in just one file so i'm going to copy this and we will paste it here and then we will write here video and this part here i am going to comment now this is not the best practice but since we are learning this should be more than enough and keep in mind that any given file can only have one main function so you cannot have this uncommented and this uncommented as well so you will have to comment one of them okay so when it comes to importing videos it's a little bit different because a video is basically a series of images so we need to iterate through all the images or all the frames and capture them one by one and display them so for that we are going to use a while loop so first of all let's introduce that while loop so we will say while and we will write true and then we will give our brackets there you go and then we are going to introduce our video capture object so here we are going to write video capture and then we are going to write the name we will call it cap and now we have to define the path so the way we defined it here the same way we are going to define the path over here so we can just remove this part and we can bring it on the top and here we can copy this and paste it here for the path now the path of course will be changed because now we are using a video so we will remove the test.png and we will write test underscore video i think that was the name yeah dot mp4 so we will write this and then of course we have to put a semicolon and then we are going to read the images one by one and we don't want to declare it inside so we will declare it over here we will call it maths image and then inside here we are going to read our image so we will say that from this object we are going to read our image and we are going to store it in this image variable so then we can display it as we displayed earlier but this time we don't want to pause for infinity we want to give it a delay but for a few milliseconds so in this case we will go with one millisecond and let's see how that works out so let's run that and there we have it so now you can see this is one of my project videos and you can see it went really fast if we want to slow it down we can write for example here 20 milliseconds and we can play it again so now you can see it's much slower and you can see the video so whenever it finishes it is going to give you an error because there will be no more images to display so that's why it gives you an error okay so that is how you can import a video and now when it comes to webcam the idea is very similar so we can copy all of this and we can go down we can comment this and here we can write webcam now the path is what we have to change so instead of defining the path we can simply write an id so here we will remove the string and over here we are going to write the id number of our camera so if you have only one camera attached you can put zero and for my case i have multiple cameras so i'm going to put one because i'm using camera id one now here you have to change it to one so that it is not too slow so let's run this and see what happens and there you go so now if i show you my webcam you can see this my mouse we have some stream deck my keyboards a raspberry pi camera lying around so this is the idea of the webcam it's very simple uh it's very similar to what we have done in the video all we have to do is we have to define the source we have to change the id instead of the path we will give it an id so in your case most probably it will be zero so i will keep it zero and i will store it zero in the code so that there is no confusion so this is basically how you can import images videos and webcams using c plus and opencv in this chapter we are going to learn the basic functions in opencv that will help us a lot in creating different projects so first of all we are going to create a new file and we will call it chapter 2. now the thing with these separate files is that you cannot run two files that have the function main in them so either you have to comment everything out or you have to exclude it from your project so i like to exclude so you can right click and you can click on exclude from project so this way it it will go away but what you can do is you can click on show all files and then it will show again and if you want it back you can click on include again so it is not like python where you can have each file separate and run it so it is a little bit messy to do it this way otherwise you will have to create new projects for each of the chapters which will be very hectic so therefore this is an easy hack you can right click and exclude whatever chapter you don't want to include so any given time you should only have one chapter running so right now we are doing chapter two so we will exclude chapter one so but what we will do is we will copy all of this and we are going to paste it in our chapter number two and we are going to use an image so we will remove all of the video and webcam code and we will uncomment our images so images code so here we are going to use the same test image and then we are going to look at the basic functions that we can use which are present in the image processing so after reading our image we are going to first convert it into grayscale so to convert it we can write cvt color and then here you can see that we have all the arguments that are required so we can write here the input image and then we have to write the output image or the destination image so we will write image gray and then we will write uh the conversion so we need to convert from color underscore bgr okay now this is giving error because it does not recognize image gray so what we can do is we can define here matte and we can write here image gray so now it should be fine so if we write it again color underscore r uh sorry bgr so opencv has the convention of bgr instead of rgb so it's blue green and red so we will convert bgr to gray so while you are typing you will see all the options that you have and we can click on them and there you go so now our image will be converted into gray so what we can do is we can copy this and we can paste here and we can write here gray and then we can write here image gray so now if we run it so here you can see we have the original image and here we have the image gray so next we will add a little bit of blur so we have uh different types of blurs that we can add but one of the most common ones is gaussian blur so we are going to use that so let's write here gaussian blur and then we are going to define our image then our destination image now if you remember in python we don't actually write the destination inside the function you could but most of the time we write it outside for example let me comment this for example this we normally write it like this image gray is equals to cvt color and all of that and we write it like that so this is how you normally write it in python now most of the times in c plus plus we are going to write the destination image inside the function so just keep a note of that so here in the gaussian blur we are going to write image and then we will write the destination image now we will need an image blur so we will define here image blur and then we want to define the size of the kernel so we will write here size and we will define let's say seven by seven i'm giving it a little bit more so that you can see the effect and then you have the sigma x and sigma y so you can leave it blank zero zero or you can add a value so let's say we add five and zero so you can play around with these values to see what the effect is or you can press ctrl and go to the function itself in the header file and you can read a little more about it here and you can follow the link as well here to read more about this so let's close that and we will go back to our asean blur and now what we can do is we can create a new image a new window for our blur image so we can write here blur and we can write here blur as well so if we run that now there we have it so this is the original image and this is the ocean blur image so if i make this three by three and let's say i make this three as well then you will see that the blurriness has reduced there you go so now it's very subtle and you can't really tell that if it has been blurred or not so now that we are done with blur we will learn how to perform edge detection and one of the most commonly used edge detector is the canny edge detector so let's write down the kanye edge detector so can e and then we are going to give in our image so we will write image blur so most of the times when we are using uh the kanye edge detector we also use a little bit of blur before it now this is a common practice so we are going to use the image blur and then we are going to write image canny and then we have two thresholds so you can see here threshold one and threshold two so we are going to write the uh threshold values over here so here we will write let's say 50 and here we will write 150. again these values you can play around with to find the optimum results so i can write here image canny because we have not declared it and then we can run it actually we didn't okay we didn't create a new window so we have to do that so let's write here image kanye and we will write here kanye as well where is it oh it's here there you go so this is the canny image and here you can see that we have detected a lot of the edges the prominent edges now of course you can change the values if i decrease it it will give us more edges for example 25 and let's say 75 so one other thing you can do which is quite useful you can create track bars and you can move it to see how much edges do you actually need so we will learn how to use track bars as well in the upcoming chapters so once you are done with that you can come back and use this to actually find the optimum detection then we are going to actually let's stop it first and then we are going to learn how to dilate and erode an image so a lot of the times when you detect the edges they are not completely filled or you can say they are not joined or they are very thin to actually detect properly so what you can do is you can dilate it which means it will increase their thickness and you can erode it which means it will decrease their thickness so let's see how we can do that so here we will write dilate die late yeah and then we are going to give in our image so image canny and then we are going to uh give out our image output so we will write image dilation so let's add here image dilation and image erosion or let's write a road so then we have as you can see we have a kernel so we need to define this kernel now in what you call python we used numpy to define our kernel but here we are going to use the structuring element so we will use math then we will write kernel is equals to get structuring elements and here we are going to say that we are going to define the type or the shape which is morph underscore rect so it is rectangular and then we will write the size and we will write the size as let's say five by five so you don't have to go into too much detail of what this does and how it works all you need to know is that this creates a kernel which we can use with dilation and if we increase the size of this it will dilate more if we decrease it will dilate less so we can copy this and we can paste it here and let's see the result of this before we go to erosion it is giving an error why is that dilates spellings are fine image kernel oh yeah the semicolon when you're coming from python a lot of the times you will forget the semicolon anyways so then you will copy this and you will write here image dilation and you will write here dilation i'm writing the spelling wrong am i yes die it's d i l relation here also we should write d i l instead of d i a [Music] okay so let's print it out if you like the video so far give it a thumbs up and don't forget to subscribe and there you go so this is the normal candy image and this is with dilation so this is quite a bit of dilation the thickness has increased quite a bit now if we reduce the size let's say three by three so by the way you should use only odd numbers here so three by three five by five nine by nine seven by seven and there you go so now it's a little bit lesser than before so then we can also use erosion so we can write here erode and we can write our image dilation as the input and the image erode as the output and like before we have to define a kernel we are going to use the same kernel because we are just lazy uh you can actually create a new one if you want so let's copy this and let's paste it here and we will write here image erode and we will write here erode as well so let's run this and there we have it so we started with where is the canny okay the candy is here so this is the candy image so we started from here then we dilated this image and then we eroded it so basically we are increasing the thickness and then we are decreasing the thickness so these kind of functions come really handy when you are trying to detect the shapes of different objects and this we will look into the upcoming chapters also we need to change the name here so these are basic functions there you go so these were five functions that everyone should know when they are starting to learn opencv in this chapter we are going to learn how to resize and how to crop images so what we will do is we will go here and right click on our projects and we will click on add a new item and over here we are going to write chapter three so as i have mentioned before we need to exclude the previous projects so we will so the previous files so we will exclude it from the project and now we can work on our chapter three so we can copy the code from chapter two so we can select all of it and we can paste it in chapter three and we can remove chapter one and two so most of this we are not going to use so let's remove all the windows and let's remove all the different functions and yeah so now we have a clean code that we can start with so the first thing is we are importing our image and now we are going to resize it so to resize we have a function in opencv that helps us out so we will write resize and then we are going to no we have to write the correct spelling first and then we have to write the input image which is image and then we have to write the output image we will call this image resize and let's declare it mats image resize and then we are going to write the size we want to change so let's say we will write size and what is the size of our image at this point um how can we find that okay let me show you how we can do that so let's comment this for now and here we are going to write c out and we will print out the size so we will write here image dot size and let's print that out and we will add and we will end our line so let's run this and there you go so our image size is 768 by 559 so we have a width of 768 and a height of 559 so what we can do is let's say we want a very specific number to resize so what we can do is we can resize it let's remove that we can resize it let's say to 640 by 480. so let's run that okay we need to define a new window so we will call it resize and here we will write image resize so let's run that and there you go so now our image has resized and you can see that the aspect ratio has changed uh it is not the same aspect ratio it's squeezed a little bit with on the width so if you don't want to do that if you don't have a particular number you just want to scale it down then instead of using this size you can remove this and you can define how much you want to scale so here you have double fx and double fy so you can define here for example 0.5 and 0.5 so this will scale instead of exact pixels so let's have a look and there you go so now it has decreased it 50 percent so i mostly use the scaling part because it is more convenient unless i have a very specific number that i want the image to be okay so this is basically our resizing and now we will learn how to crop our images because this is very important a lot of the times when we find an object we want to crop it to add more processing on that specific region and a lot of the a lot of the times this region is called region of interest roi so to crop an image we simply have to write let's say that our new image is image crop so we can define it here image crop and which is equals to we will define image and inside we are going to define the values for cropping so to do that we are going to use the rectangle data type so we will write here rect and then we will define a name so let's say roi and inside that we have to define the x y width and height so basically we will write first of all let's say 100 is my x my y is again 100 and then we have to define the width and the height let's say we will put 300 for the width and we will put 250 for the height so this is our rectangle and what we will do is we will put this roi in our image so this way we can crop our image so let's see the result of this so here we will write crop and here we will write image crop so let's run this and there you go so now you can see that the image is cropped this is the resized image and this is the cropped image you can see i can align it like this so this point here what did we write let me check this point here is 100 so this point is 100 and 100 and then from 100 we are going 300 pixel forward and 250 downwards so let's let's try to find my face let's try to crop it so if this is 100 maybe this is about 200 and we will keep the height same so let's put 200 here so let's try that again yep and maybe a little bit down so if it's 250 let's make it 300 let's see if it crops yep there you go so now you can see that we have successfully cropped my face from the image is it aligned i can align it from my shirt over here uh there you go maybe a little bit to the left a little more i think yeah that is pretty much aligned so that's how you can crop the image and that's how you can resize it so let's rename this to resize resize and crop in this chapter we are going to learn how to draw shapes and how to write text on images so we will learn how to draw circle rectangle line and how can we put text on different images so we will copy our chapter 3 which was about resize and cropping and we will create a new chapter so we will right click and we will add new item we will call it chapter is it for yeah chapter chapter 4 and then we will paste it and let's change this name to draw shapes and text and then we can remove these images and yeah let's remove pretty much everything yeah we are not going to use any image we are going to create our own image so that is also quite useful when you are creating different projects sometimes you want to create a blank image of different colors so let's see how we can do that as well okay so and one more thing that we have to remove chapter three from our project so we will right click and exclude from the project and before we do that we will save it and make sure that this is on if you have this on then you can see all the chapters and you can see all the resources files if you have it off you will not be able to see it okay so in chapter number four we are going to create our own blank image so let's write here blank image and here we are going to define math and we will call our image img now we have to define the size so let's say 512 by 512 and then we can define for example the type here we can define the type as cv underscore 8 bits 8 bit means that each pixel will have a value from 0 to 255 because if you're not familiar with bits bits are basically based on the power of 2 so 2 to the power of 8 is basically 256 values so you have a range from 0 to 255 but you don't have to go into that much detail but all you need to know is that 8 bit means you have a range from 0 to 255 and if it is unsigned then it will be 0 to 255. if it's signed then it will be from -127 to 127 but here we are going to say it is unsigned and the channels are three so we need three color channels bgr so that's how you define this and then we are going to write the color that this image will have so we can create a black image we can create any color that we want so normally whenever we will define color we will define it as a scalar and then inside we will write our bgr values so if i want blue i will write twenty two five five zero and zero and this will create a blue image so let's run this there you go so this is our blue image and let's say if i want purple i can write 255 here and that should give me purple i usually prefer this purple color when i'm creating bounding boxes or any writing any text that's why i know this is 255 and 255. anyways so in this uh in this example we are going to create a white image and we will put on top of that if you want to create black you can put 0 0 0 okay so now we are going to create a circle first so we will write here circle and we will give in our image which we just created and now we have to define the origin point which is the center so for that we will write point and then this is the data type point and then we are going to write uh the center point so let's put it at half of this which is 256 and 256 and then let's define the size of the circle let's say 155 and let's define the color scaler again we are defining color so we have to write scalar now i have already tried this out so i want to add this specific color of orange so that's why i'm using this color you are free to use any color that you want so if we do this it should be more than enough to create a circle so let's see how that works out so this is our image and this is our circle within that image now if we want to increase the thickness of this we can write here for example 10 so this is the thickness if we run it again and there you go now the thickness has increased but what if we want it filled then we can simply instead of thickness we can write here filled so let's run it and there we have it so now our circle is filled we will keep it like this so now we will draw a rectangle rectangle and inside that we are going to define our image first and here we have the option of defining a rectangle like we did in chapter number three so here we have the rectangle option so we will have x y width and height or the second option is to define two points so if you write it as points then it will be the bottom uh top left corner and the bottom right corner so this will be x y and the second one will be x one y one so it is not the width and height so let me show you what i mean so if i write 130 and let's say 2 to 6 again i have tried these values out before so i am putting these exact values so 382 and then 286 and then we can write scalar scalar yeah and we can write two five five two five five and two five five we are going to use white color and we will put the thickness of let's say three so let's run this there you go so this is basically my uh point here 130 2 2 6 and this point here is 382 and 286 so these are points but if i write rectangle then it will be the width and the height not the actual points so it is up to you what you want to use both of them should work fine and for this case we are going to fill it so let's write it filled like we did with the circle and there you go now it looks like a stop sign so let's move on and now we are going to draw a line so to draw that we will simply write line and we will give in our image and then we will write the point the point is basically the starting point and the ending point so we can write 130 and then 296 then the ending point will be 382 by 296 so it will be a horizontal line then we can write again the color so scaler and we will write again the white color two five five two five five and two five five thickness let's put it as two so let's try that out and there you go so now we have a nice line right below our rectangle again if you change this value it will not be horizontal let's say i put 450 now it will go down there you go so it's going this way so again you can change these values you can play around with those to find out what design do you want so the last thing we will do is to put the text so we will write here put text and again we will define our image and we then we will define what we want to display so let's write my channel's name multiples work shop and then let's write the point points is 137 and then 262. so this is basically the x and y the starting point and then you can define font so we can write here font and you will see that we have a lot of different fonts that you can select from let's just pick a random one and here we can define the scale so let's say i put 2 as the scale and then we can define the color scalar and let's pick the orange color over here let's copy that paste it here and at the end you can also define the thickness so let's put as two as well so let's run this and there you go so now you can see the text is displayed but it is quite huge so let's reduce this to 0.75 and there you go so now it perfectly fits within that circle and it looks quite good now let me show you what the thickness means so if i put let's say let's say five which will be crazy but still let's try it out so there you go the the text becomes really thick and it now is quite hard to actually read so let's keep it as two because that was good enough so this is how you can create a blank image of any color then you can add a circle rectangle and a line along with some text so this comes very handy when you are detecting objects so you can create bounding boxes rectangles around them and then you can for example display what size is the rectangle what is the name of the object so all of these things you can define on your image so it will be very useful when we do our projects in this chapter we are going to learn how to warp images now if you are not familiar with warp then let me show you this is the image that we will use it is of a few cards and what we will do is we will take these four points one two three and four and then we are going to display them as if it was taken from the top so it will be like a bird's eye view so that is basically what warping is so let's see how we can do that so first we are going to go to our project right click add new item and we will call this chapter five and over here first we are going to exclude the chapter four and then we will copy the code from chapter number one because we are importing an image so let's paste it here and we will comment it out here we will write warp images let's say and then we will import our was it called cards cards.png or jpg i think png let's run that no it's jpg jpg there you go so this is our image and we are going to so you can select any of these cards now i'm going to do this king card i recommend you to do after that try out to get the jack the nine and the queen so what is the idea behind this first of all we have imported our image now we need the points that we want to work so if i open this image in paint right click and edit if i open this in paint then when i go to this point it will tell me in at the bottom of the screen it is going to tell me what is the pixel value at this point uh not the pixel value what are the coordinates of this point of this pixel so here you can see it is 529 and 144 pixel then over here you can see it is 405 392. so i have noted all these values down and we will write them here so here we are going to write points but the function that we are going to use actually requires floating points so we are going to use 2f instead of just point then we have to define the name so let's call it source and we will define the size of it so we will have four points inside of it so let's create our points the first one will be 529 and 142 then the second one is 771 190 so these are the points that i've already collected from our image four zero five and three nine five and then we have six seven four and four five seven so these are our source points and then we will have our destination points so destination points will be basically a rectangular image so so we have to define that what does this point correspond to so this point corresponds to 0 0. so this point over here let me bring a circle so this point over here should be the if let's say this is my card so this point over here should be this point 0 0 and then the second point should be this point then the third will be this and the fourth will be this so these are the corresponding points so we have to define it so here we are going to define that this should be zero so we cannot just write zero because we have to have floating values so we have to write 0.0 f and then we will write that this is the width so this is the second point so it will be the width and in the height it will be zero so this will be width and 0.0 f then we will have 0.0 f and height and then we will have width and height so what is this width and what is this height so this should be the size of our card now i have noted down the size of cards so they are 2.5 by 3.5 inches so what we can do is we can keep the same aspect ratio so we can define here that let's say let's define it down can define here the float width is equals to 250 and the height is equals to 350. so now we can use the width and the height over here so these are our source points and then we have our destination points now we can use a transformation matrix to actually find the work so in order to transform we need a matrix so whenever we input the values we whenever we input the points it will output the corresponding transformed points so we need that matrix so let's say this is our matrix is equals to so we need to define it so we can define here math matrix mate rex and we will also need our image work so let's define it here so yeah that should be good and then we can define here gets perspective am i writing the spelling wrong yes perspective transform so here we are going to input our source points and our destination points so this will give us a matrix that will that we can input some values and in the output it will give us the corresponding values so then we are going to use word perspective verb perspective again i'm writing the spelling wrong word perspective image then image work and then we will write the matrix that we just found out or created and after the matrix we have the size so we are going to given a point and we are going to define the width and the height so this width and height will be basically the width and height of our card or the final image so now what we can do is we can simply uh plot or we can simply display our image so here we can write image verb and image work so let's run this and there we go so now we have this image straightened out stretched as if you are looking from the top view the same thing you can do with the queen and the nine and the jack and in fact you should do that as an exercise to do some practice now one more thing we can do over here is to create circles around the points that we have selected so we can use these points and we can create circles on them so to do that we can write here 4 integer i is equals to 0 then i is less than let's say 4 or we can take the size but let's just keep it 4 then i plus plus and then we can write our loop so what we can do is we can use the circle command so let's just stop this first so the circle we did in chapter 4 yeah so in chapter 4 we have this circle we will copy it from here we'll go to chapter 5 and we will paste it here so here we have to just change the point so we will say that take the point from our source at the value i or the element i and then this 155 is just way too big so let's just put 10 and the color let's put red okay so let's run this and there you go so now you can see we have uh selected these points so this is a nice way to know if you are selecting the correct points or not if there is an issue or not now the reason we are doing it after all of this you might say why don't you put it up here before we actually do the work the reason is that then the circles will appear in the verb as well so there you go so at the edges you can see the circles they are appearing so that is not good so therefore we are putting it down after the word perspective okay so this is how you can do a work of an image now in the upcoming projects we are going to use this technique to actually scan documents so if you have used any mobile app that actually detects a paper or a document and then it creates a bird eye view and does some scaling to it and then some processing to make it look like it has been scanned so all of this we are going to learn how to do that in the upcoming project so in this chapter we are going to learn how to detect colors in images so you can use this to create an object detector of a specific color as well so first of all we are going to go to our project and we are going to add new item we will call it chapter six and we will copy what should be copy let's copy chapter one and we can close chapter one four and five we can save that let's paste it here and we will uncomment this here we can write color detection so we are not going to use the test image instead we are going to use lambo which is uh the image of a mini lamborghini so let's exclude this from our project chapter number five and now we have only chapter six running so let's run this and see if it works properly there you go so we have our image the orange image so basically we are going to detect this orange color by the way you can write here integer the common or the standard practice is to write int and then you return 0 return 0 but since it's a very simple code we can simply write void doesn't make much of a big difference anyways so now we are going to convert our image to hsv space because it is much easier to find the color in the hsv space you can read more about this color space it is basically hue saturation and value so as we did earlier in chapter number two we saw that we can convert cvt color image then we are converting our image so we can copy this or we can write it again so cvt image and then we can write here image hsv and then we convert this bgr to hsv now let's create this image so we will write here mat and let's write our image hsv then we are going to print it out so let's write i am show and then image hsv and then we will write image hsv there you go so let's see how it looks like okay so this is our original image and this is our hsv image it looks a little bit odd but trust me it will work out at the end of the day okay so once it's converted we are going to use the in range function to actually collect our what do you call color so we are going to write in range and we will write the image hsv so we will write that this is our image and then we will define a lower limit and an upper limit now usually you can define exactly what color do you want for example i want red color i want yellow color but in reality because of the lighting and different shadows the color will not be exactly one value so it will be a range of values so it is always good to use that range so we will have a range with lower and upper values and then the output will be our mask so we will call it the output image will be our mask so we need to define the mask so this will be our new image and then we also need to define the lower and the upper limit so we can define here scalar because it is just three values we can define lower as h minimum so this is the hue minimum and then we have saturation minimum and then we have the value minimum same way we are going to have max so let's copy that and right here upper and instead of minimum we will write max now these values you can hard code them and you can write them directly here the other way that what i prefer is to write here integer h minimum is equals to let's say zero now this color i have already picked out and i know what exactly this color is so i'm writing those values over here but i will later show you how to find a different color as well so let's put this here and then we can write s minimum is 110 then v minimum is equals to 153 then i'm writing it in a new line because we are writing maximums so max is equals to 19 then s max is equals to 240 and v max is equals to 255. so now if we print out or if we display our image which is basically the mask we can write here mask so let's see how that looks like so there you go so this is our original image this is the hsv space uh the color space and this is our mask and you can see that we have detected the orange color uh perfectly as you can see over here but the question is how did i find these values and a lot of the times you might want to use your own values so how can you do that now the thing is because there are six values involved changing all of these every time and running it again will be very painful and it will take a long long time so what we can do is we can create track bars which will allow us to change these values in real time and then we can find the minimum and the maximum values so how can we declare this track bar so opencv has a built-in functionality for this and whenever we are using trackbar we will have to use a while loop so we are going to create a while so we do not need to convert or imports it again and again so we will create a while after that so we will write here while true and then we are going to close this here and we will change this to one like we did in the video and webcam we will change this to one and over here again we need to we do not need to create the track bar again and again so we will create it outside the while loop so we will call it named window and our window name will be track bars let's keep it small track bars and you can define the size of this so let's say 640 by 200 so this will create our track bar so if we run this you will see a new window so this is our track bar window and now we can create track bars inside of this so here we will say create track bar and we will give the value a name so let's say this is hue minimum and then we will say this is inside this should be inside the track bars window and then we have to give the address of our value so here this is the h minimum so the address of that will be and h minimum and then we will give it the maximum value so the maximum value will be 179 so for hue it is 180 and for the other two value and saturation it is 255. so what we can do is we can copy this actually let's uh run it and see if it creates so here you can see this is our hue minimum so if i change this it should change yeah so you can see now it has an effect in real time because it is already linked because we are using the h minimum here and then we are using that in our lower so what we can do is we can copy this and we can paste it six times and here we will do let's say max let's copy max and we will paste it here and then we can do uh hue saturation so we will write here sat and sat and then val and then val so here it should be max here it should be s s and max here it should be v and v max so two five five two five five two five five and two five five so these are the changes again these two will be 179 and these 4 will be 255 so let's run this again and there we have it so uh in the beginning it should be like this so the minimums should be at zero so zero and zero all three are zero and then maximum should be at two five five and one seventy nine two five five and two five five so it should be like this now we can move these track bars to find our color so we will move this around and we have to keep it white so we will change it so that it remains white so here you can see then we can go to the next one you can see it becomes more clear we can reduce now then we can check the value yeah getting a little bit better and there you go so these are the values once we have these values we can go back and we can enter these values as our starting points so here we can write 4 then here we can write 24 then 145 255 117 and 255 so that's how you can change actually let me show you another example i have another image of shapes so this shapes.png let's bring that in and let's pick one of these colors so shapes dot png and by default this should be 0 0 two five five two five five oh this should be one seven nine one seven nine okay so let's run this so this is our blank blank mask so now we can let's pick one of the colors so let's pick this red or let's pick this purple because this purple so we can keep moving until this one is in the picture and the rest are gone so we'll go till here then we will start reducing the others and there you go so this is very simple because uh we are using definite colors there is no variation in these colors so it was very easy to find then if we want to for example the green one let's try that so we will keep moving it until the green one stays and the rest are gone and there you go because there is no change in value and saturation therefore we can only play around with these and we can find that so this is how you can find colors in different images and based on these color detection you can detect different objects and this is a very fast method to do object detection and tracking using color in this chapter we are going to learn how to detect shapes or contours in images so we will be able to detect rectangles squares triangles circles and all these different shapes so let's get started first we are going to right click add new item and we will call this chapter number seven now we will remove chapter number six we will exclude that from our project and we can copy from chapter one because we will be using an image so we can copy from chapter number one and we can paste it here we can uncomment this and we can change this to shapes so if we run this that should output our shape image there you go so these are the different shapes that we will be detecting okay so the first thing we will do we have to pre-process this image before we find contours or before we find the shapes so the idea is that if we find the edges of our image then we can find which shape is that edge or which shape is that contour so what we will do is we will use the kenny edge detector to first find the edges and then we will find the contours contour points so if you did not understand it right now don't worry as we go along i will explain every bit so in chapter number two to use the canny image we we first did the conversion to grayscale and then we added the gaussian blur and then we used the kanye edge detector so we are going to copy all of this and we will paste it in chapter number seven and we will also paste the declarations over here so we can paste it here so image gray blur candy dilation and erode so for now we are only using these three and maybe we will use dilation as well let's copy that as well so where is dilation okay so we can copy this as well there we go so let's see first of all the gray so we can copy this we can paste it multiple times and we can say image gray then we can say image blur then kanye and then dilated so then we will change its image gray image blur image canny and then image dilation so let's run all of them and see one by one what are the results so there we have it so this is our original image this is our gray image this is the blur uh blur is not been applied on this okay let's go back so here we are applying the blur on the wrong image so we will put here image gray in fact we should do the same in chapter number two as well we should use image gray so let's run it again so this is the original image then we have the gray image then we have the blurred image uh then we have our canny image and from the canny we have our dilation image now if you see here this triangle and if you look at the edge of this triangle you will see that it has a small gap so when we dilate the image this gap is gone and now it is a solid line so this means that our detection will be much better with the dilated image rather than the canny image so that's why we are using dilation as well so this all of this is basically pre-processing of the image so we can call it pre-processing now once the image is pre-processed then we are going to find the contours in it now what we will do in this chapter will be a little bit different than what we have done before so we are going to introduce a new function to find the contours instead of writing everything in the main function we are going to introduce a new one so over here we are going to define our new function we do not need anything coming out of it there will be no returning value so we will write void then get contours and then later on we are going to decide what are the things that we need but for now we are going to write here gets contours and we will write it like this so first of all we need to send in our image so this is the last image that we have so we need to send this so we will write here that we are sending image dilation and over here we need to receive it and we have to define the data type first so we define it as mat and then we will write that this is our image that we are receiving so this is the receiving image let's call it image dilation and then we will also receive another image mat image on which we want to draw so we can write here that this is the image that we want to draw on and this is the image that we want to find the contours on so then we can write here first of all we can write the function find contours so we will write here find contours and let's see what is required so the first thing is the input image so we will write here image dilation and then we have our contours and then we have our hierarchy and then we have our type our method so we are going to use the external method you have a lot of different methods um that you can use so external flood fill list tree so you can use any of these methods we are going to use external and then we have the chain approximation we can use the simple chain approximation now you can read all about this in the documentation and you can if you don't want to read about it you can play around with these values and see what the effect is but we are not going to go into the detail of the theory for these parameters so we are getting these errors because we did not define them so we have to define our contours now unlike python we need to define the data type uh over here before we can define it so what will it be like so these contours will be a vector and inside that vector it's like a list and inside that list there will be another list of different points so let me show you how that will look like so this is a vector and inside that vector we will have more vectors so these are more vectors and for each vector each vector will be a contour and each contour will have some points so this will be let's say 0.1 let's say it's 20 30. and then we will have 0.2 let's say that is 50 60. so this will be one contour and then there will be another contour then another contour so this structure is basically we have a vector and inside that vector we also have a vector and inside that vector we have points so inside this vector we have different points so point one point two point three point four so the data type inside that is points sorry point and then we need to close this and then we can write contours so we can remove all of that and for the hierarchy the data type is a little bit different it is basically a vector but it has four integers inside of it and opencv has their own data type for this so we will use that you don't need to go into that much detail for this because we are not even using the hierarchy so you don't have to worry about that so we are basically writing vector uh for i which means it has four integer values so we are going to write here hi r key let me just copy the spellings there you go so now we will be able to find these contours and what we can do is we can print them on our image so to print them we have to write draw contours so this is the function that comes with opencv to actually draw contours so let's write that down so we will write here draw contours and then we are going to write which contours are we talking about uh but before that we need to write which image we want to draw on so this is our image and then we are going to write which contours that we want to draw and we can also define which contour number we want to draw but in this case we want to draw all of them so we will write -1 then we can define the color so scalar scaler and we will define let's say purple as i've mentioned before that is the one i usually choose and then we will give it a thickness of 2 so let's run this and see if it works properly by the way we can remove all of these because we have already seen the results and then over here we can write uh i am show actually we already have that so it should display let's see and there you go so on our original image it has drawn all the contours it has detected with purple color so we can change the thickness here for example here we can put 10 it will be really thick there you go so now you can clearly see the difference okay so now that we have uh the points the contour points what we want to do next is we want to find out the area of these contours and we want to filter out so right now there will be no filtration because we do not have any variation in fact let me introduce some variation so let me close this and i will go to my image right click here edit and let's put something very small so let's put something like this and let's save this and let's run it again so we are assuming that this here is noise so we need to remove this now in order to remove this you can simply add a filter for area so if the area is above a certain value then it should detect as an object otherwise or a shape and then it should use it as that shape so first of all we will loop through all the contours that we have found so we will write integer i equals zero we are creating a loop and we will write i is less than contours dot contours dot size so while it is less than that it will keep adding to i so then we are going to find the area of each of these contours so we will write here area is equals to contour area so this is the function that actually helps us find the area so we will write here which contour are we talking about so we are talking about the ith contour and this is giving an error because we did not declare so we can write here for example area will be i think integer so we can write here integer area so we can write here and then we can print this out so we will print out our area and we will end our line okay so let's run this and there you go so these are all the areas that we find and you can see that we have one very different which is 185 and most probably this is 185 so what we can do is we can create a filter where we can say that it has to be above at least 1000 so we can say here that if area is greater than 1000 then we will perform the next steps so rights from the start we can remove this draw and we can draw only the ones that are above 1000 so instead of writing -1 we can write i so it will draw one by one all these contours and it will not draw the ones that are below 1000 so if we run this again there you go so now you can see the purple line has been removed from our uh what do you call image so here 185 is removed okay so now that we have done the filtration of area next we are going to find the bounding box around these uh objects or shapes so to do that we are going to find the parameter so we will write here patty and then arc length so we will find the arc length using the contours contours and we will give in our contour and then the boolean is basically whether this object is closed or not whether this parameter or the contour is closed and then we will say yes it is closed true so that should give us the parameter and we did not define parameter it should be float and then we are going to write the then we will find the corner points or the number of curves that this polygon has so we can say that if the polygon has four curves or four corner points then it is a rectangle or a square if it has three then it is a triangle if it has a lot then it is a circle so what we will do is we will write approx poly dp so we are going to find the approximation of the curves again we are going to say that we need to find for our contour the height element and then we are going to define a new array in which we will send all these values okay so we need to have an array which will have all the updated values that we will use so we will call it con contour poly por lee okay and it is an array as well or it is a vector so we are going to have a lot of different values so we will choose the ith value we will send it to that so basically this is the output and then we are going to use a random number multiplied by the parameter now here if you don't if you don't understand this part and you don't want to do it you can remove it as well and you can put here a fixed number like three or five or something like that i recommend that you use this method instead because it is a little bit better and then at the end we are going to write true again for our boolean which means that if it's close or not so we will say that it is closed so it is giving us an error because we did not declare this so let's declare this at the top so here we are going to say that this this will be exactly like our contours but it will not have all the points it will have only the corner points so if it's a rectangle it will have four points if it's a circle it will have maybe more than six seven points if it's triangle it will have three points so we can copy this type and we can paste it here and then we can write that this is our con poly and we can actually define the size of it as well because we already know what the size of it will be it cannot exceed the number of contours so it will be exactly the same size so we will write here contours dot size so this should remove our error and there you go so now we can draw only these points as well so instead of writing contours we can write con poly and then we can draw i the ith contour so let's try this and see what happens so there you go so now it's drawing all the points that it thinks has it has found so based on these points so here you can see it's not drawing the complete circle it's drawing these points and in between that it's drawing the lines so this rectangle will have only four points and then it's drawing the line between them so what we can do now is if we check the length of any of the corn poly that will give us the approximation of whether it's a rectangle or a square or whatever it is so we can write here for example the corn poly add the eighth element and we can find the size of it oh sorry this should be a dot so let's put c out and let's put an end here and let's print it out so now we can see that the first one it has found has four then the second one maybe it found this one which is eight then you have three which may be this one so all of these are now the number of points or the number of edges or the corner points it has found so we can say if it has found three it is a triangle if it has found four it is a square or a rectangle so one more thing we can do is we can find the bounding box around it so if we want just a rectangle we can find that around it so how can we do that we can do that by writing let's say bounding box uh pounding box or is it recta yeah it's a bounding rectangle so bounding rectangle and we just pass in our contour poly so this is the approximation that we got the number of corner points so we pass that in and it will draw a bounding box or it will give us the coordinates of the bounding box around it so what we can do is we can create a new vector to store these values so we can write here vector and inside that we can store the rectangle values and we can call this bound rect so just make sure that you understand the difference between bound rect and bounding rectangle bound rect is the variable that we are introducing the vector and bounding rect is a function that we are calling to find the bounding rectangle around it okay so don't confuse between that and because we already know the size of the vector so we can define it from here contour size so now we can put all of these values in our bounding rect and we can put them in our vector there we go so we could print it out as well and then we can draw these as well using uh the rectangle method so let's write it here so let's say rectangle or let's just copy it from chapter number four i think yeah so let's copy it from here and we will go back to chapter seven and we will paste so we want to draw it on image that is correct we have the initial point and then we have the second point so we will remove this now rectangle has a basically a method in which we can call the top left uh points so we can say that bound rect at let's say height element we want the top left points okay so we can call it like this and then the same thing we can do for the second points which we can write bottom right instead of top left we can write bottom right so let's change the color to let's say green and let's remove the filled let's make this five so if we run this now there you go so now you can see for the rectangle or the square it doesn't make a big difference but for the triangle you can see the difference that this is the bounding rectangle the maximum area and that it covers and then the the purple one is the actual contour of your rectangle or triangle or circle so this is the idea so we can comment it out and then what we are going to do is we are going to find out which object is which or which shape is which so what we will do is we will create a new variable called let's say integer objects corner so this will have the number of corner points is equals to uh con poly and we will take the ith element dot size so if it has uh three corner points then we will have the value of three here but we need to convert it into integer so we can convert it like this so now we will have uh all the values stored in object corner for each iteration and now we can check if this corner object corners is rectangle triangle or square whatever it is so we can say if objects corner points is equals to three okay i forgot the brackets if that is three then object type so we can declare it here integer or let's say string string object type um yeah let's not write anything there so object type is equals to let's say triangle so if it's 3 then it will be triangle if this is 4 it will be rectangle and if it's greater than four it will be circle it's greater than four it will be circle and then what we can do is we can put the text so let's go to chapter number two or it was chapter number four so we will copy the text this is the command for the text and we will paste it here now the text will be basically our object type and the points will be the starting point the top left or instead of writing the top left what we can do is uh because if it's exactly on the top left it will be on the contour shape so we want to draw or we want to put the text a little bit above so what we will do is we will say bounding rect actually we should use this bounding right let's use this bounding rect and we will put i and then we will put dot x so this is our first point and then the second one will be sorry not the point the value bounding rect and we will put i dot y and here we are going to decrease the value so we will reduce it by 5 so that it goes a little bit above and we can remove this we don't want to draw it so we can remove that or do we want let's let's draw it as well so and we can put everything that is about drawing and putting the text at the end so we can put this and we can put this at the end as well okay so let's run this and there you go so now we are getting rectangle rectangle i think the text is a little too big let's decrease the size so where is it the text value it's 0.75 and this here is two let's change this to plane let's try it again yeah now it's much smaller but it looks weird let's make this one and let's make this one as well there you go so now it's clear to read so you have rectangle triangle circle so but here the problem is that this is also rectangle and this is also rectangle but this is in fact a square so how can we define this as a square so what we can do is we can go to our object type here which is when it's equals to four and instead of that we are going to define a ratio so we are going to say that we have a float we will define a float with the aspect ratio and the idea is that if it's a square then when you divide the width by height it should give you one when you divide width divided by height or height divided by width that should give you one if it's not giving you one then it is a rectangle but in reality you might have some variation so instead of saying that it has to be exactly 1 we can give it a range maybe it can be between 0.9 and 1.1 something like that so this is what we will write so first of all we will get our ratio so we will write here bounding is equals to bounding rectangle and we have the height element we need the width of it and we will divide it by the height of it so bound direct i and then we want the height but both of them need to be float because they are already integers so we will convert them to float okay so now that should give us the aspect ratio and now we can check that if the aspect ratio is greater than is greater than zero points uh let's say and the aspect ratio is less than 1.05 then our object type object type is square else our object type is rectangle okay so if you want we can print out the aspect ratio so c out aspect ratio and we can write a new line okay so let's run this there you go so now it says this is square this is rectangle this is square this is a rectangle now it's working properly and here you can see the aspect ratios you are getting so this is one is very close to one so this is a square this is far away so it's a rectangle and this is very close to one as well so this is a square as well so yeah so it looks good i think the font is a little bit small still or maybe just increase the thickness if you like the video so far give it a thumbs up and don't forget to subscribe yeah now it's a little bit better it's at least visible and clear to read so this is the idea of how you can detect shapes in an object and right now we are not returning anything so all of the things that we have detected we simply displayed but in the upcoming projects we are going to find out those values and then we will return things as well in this chapter we are going to learn how to detect faces in an image now it might sound complicated but it is actually easier than detecting colors or contours so let's get started first thing we will do is we will create a new c plus file and we will call it chapter 8. and we will exclude our previous file and we will remove all of these now we will go to chapter number one and we will copy the code to import a image so we will write it here and we will remove the comment and now we will use the test.png because it was yeah it's my picture so we will detect the face here so we can close this now to detect the face we are going to use viola jones method of hard cascades now this method was introduced quite a long time ago but it is still quite good and it is still used quite a bit so what we need is we need a new header file so we will write here include and we will write opencv 2 and then we will write object detect dot hpp so this is the header file that will allow us to work with our cascade files so basically what we have is a model already trains and we have the xml file for it so here we have the frontal face default dot xml so we are going to use this xml file to load our our cascade and then we will use it to find the faces so here after we have imported our image we are going to load our cascade so we will write here cascade classifier and we will call it let's say face cascade and then we will say that face cascade dot load and we will give in our path so it will be resources slash har actually i'm not going to type all of it let's just go to the folder and here we are going to copy all of this and we can paste it here then we are going to check if it is loaded properly or not this step is not necessary but it is useful just to make sure that it has loaded properly so we can write face cascade dot empty so this is to check if it's empty or not if it is then we will print out that xml file file not loaded and we can print this out so i i'm writing it in one line so that you can remove it if you don't need it so if i if i run this now it should not give any errors yeah it didn't give any errors but if i change the name here for example if i write a now it will give us an error xml file not loaded so let's put it back there you go okay so once we have checked if it's working properly or not next we are going to detect the faces so to detect the faces and store them we need to store the bounding boxes so bounding boxes are basically rectangles so we can create a vector of rectangles so let's create that vector of rectangles rectangles r should be capital and we will call it faces and then we can use our face cascade dot detect multiscale method so we will write here image we will give in our faces we want to store them in this vector and then we have some parameters that we can define so this is the scale factor so we will write 1.1 and then we have the minimum neighbors so for example we can put 10. now these values again you can read about them and you can change the parameters to fit your needs but for now we are going to use these default values so now that even if it's detected we don't know whether it detected or not because we are not printing out anything or we are not displaying anything so what we can do is we can iterate through all the faces that we have detected and then we can draw them one by one so we will write here four integer i is equals to zero then i is less than faces dot size and then we will write i plus plus and here we are going to write the code for rectangle so if you remember let's go to chapter number four we have the rectangle example let's copy that and here in chapter eight we are going to paste it so for rectangles we need the image so this is the image that we will use then we need the x and y so here we can write uh let's say faces if you remember direct the data type rect has the method of top left and bottom right so we are going to use that so we will say that faces at i dot top left and with this we will replace by faces dot i faces i dot br bottom right and we don't need it filled we can put let's say a value of 3 for the thickness and we can change it to purple so let's run that and there you go so now the face has been detected and we are able to draw our rectangle around it in this project we are going to develop a virtual painter now this is one of my favorite projects because it combines a lot of the fundamental techniques that we have learned in this course so we will implement the color detection the contours the shapes and a lot more so let's get started the first thing we will do we will exclude our previous file and we are going to include a new one so add new item and we will call this project one now this project is webcam based so we will go to chapter number one and in chapter number one we are going to copy all of this and we will paste it here and we will remove the video and the images so now we are left off with webcam and here we can replace it to with project one so before we go any further let me show you my webcam actually i'm using id number one so let's run this there you go so it is pointing up at the ceiling and then we are going to detect these markers now you can see that the light is really bad so what i have is this light which i will be using wait what is happening okay i think the brightness is very low yeah so this is the light that i will be using to actually put it next to my webcam so that uh the colors are a little more visible so let me show you the effect so this is after the lights and if i increase it further you will see now it's really bright so now we can really see the color and if i do it without it there is without and this is width so it makes a big difference so that's why we are going to use it so i'm going to have this is full intensity let me decrease that a little bit i think that is good enough okay so we are going to have these different colors so we have these markers green red purple so we are going to detect those so the concept here is that we will first detect the colors using the hs3 space and once we have the color image we are going to pass it to the contour method to find the contours where they are and once we find the contours we are going to take the x and y position of those and then we will create a circle at that point so first of all we have to find the color but like the previous example we are not going to write all the code uh in the main function we are going to create a new function called find color and then we are going to add all the code for finding the color inside of that so we will call it find color and inside that we can send in our image to find the colors so for now we will only write this and here we are going to add a new function and for now we are not going to output anything because we are not sure at this point later on we can decide what to output so here we will write find color and then we will accept an image so that is the main idea okay so let's go to which chapter was it i think chapter six chapter six was about color detection so we will copy all the code from color detection and we will use that to our benefit so here we are going to copy this because this is the main part we are not going to use track bars for this example so let's paste it here and let's go back and see if we need something else do we need something else yeah we need the hsv space so let's copy that and we will come back and paste it here okay i think that should be enough we have uh enough to start off with so we can close chapter one and chapter six so the idea is that we will have an array of all the colors all the minimum and maximum values for each color and we will loop through this array or matrix or vector whatever you want to call it and one by one we will find an image for that so we will not just have one mask we will have multiple masks so for example if we are detecting green and purple then we will have a mask for green and a mask for purple if we have three colors we will have three masks so we don't want to hardcode it so that it always has three masks or four masks so what we will do is we will create a vector here and based on that vector for example if you have four vectors inside it it will detect four colors if you have two it will detect 2. so that is the main idea so we will say that we have a vector and inside of that vector we will have another vector that will contain integer values and we will call it let's say my color um okay let's call it my colors and so the structure will be something like this so we have one color here then we have another color here then we have another color here and so on i think it will go back yep anyways so this is the idea so we have all the values of one color here then the other color here than the other color here so all the values will be integer so we are going to paste the values here now what you can do is you can use the color picker what you call script or code to actually find your colors now i have already written this color picker script what it is is basically chapter number six with webcam that's it so instead of an image it will use a webcam that's it and i want you to try it out for yourself try to make that color picker project for yourself if you are unable to then leave a comment below ask any questions and then once you understand then use this code so i will create a new file here and we will call its color color picker so this is the code for color picker so again it is exactly the same except it has a webcam from chapter six it is exactly the same except for the webcam so let's exclude project one let's exclude project one so let's pick two colors let's say green and purple and then we are going to run it so let me change my camera number here to one so here i will hold the purple color and then i have to move these values until i get the purple color detect actually for the color picker script we should have all of the minimum 0 and then all of the maximums as 255. oh this one was 179 and this one is 255 this way it will be easier to always run it so again i will hold the purple color and then i will move my bars until i only get the purple color so at any point if it's not showing then you have to stop and go back i think that is pretty good there you go and so let's try a little bit again okay i think that is good enough so what this does is it outputs these values these are basically the minimums and the maximums so what you can do is you can just copy it from here uh i think i'll have to do it from the keyboard so let's copy it from here and we are going to go back to our chapter number where did it go project one and in project one we are going to paste it here i think it didn't copy uh so i will copy this again and let's paste it okay there you go so this is how you can select or collect your color so this one was purple and then let's do another one and later we will do the rest so for now we will just use two colors so let's pick the green one so let's go back so let's copy this and then we will paste it here so now that we have defined the colors we want to detect so this one is let me put a semicolon first so this one is green now we also need to define what color do we show when these are detected so what colors do we display so we will also create a new vector but it will have scalar values inside it so we will call it vector and inside that we will have scalar values and we will call it my color values so whenever it's detected what are the values that we want to show so here we are going to add two vectors here or two scalars and the first one is purple so purple is two five five zero and two five five and then green is bgr so it will be 0 2 5 5 and 0. so these are the values that we will have we can put it down so that it's easier to read so again this is for purple and this is for green okay so that being done we are going to go back to our find color now we have the find color over here now the thing is that first of all we need to make sure that this is working properly so here we are going to let's say call in our mat image hsv sv and then we have our color okay these are not showing any colors because ah yeah we have excluded it from the project so we will include it back okay now they are showing the colors and the errors so now how do we how do we write the values of h minimum s minimum and v minimum so these are the values over here and the structure let me show you the structure it is basically where is our color picker color picker is here so this is basically the structure so you have h minimum s minimum v minimum so all the minimums we have first and then we have h max s max v max so this is the idea so let me just copy this from here and in the project we are going to paste it at the top here so this is the structure so what we will do is now for each of these colors we have to find a mask so we need to loop so we will call we will add a for loop here and we will say for integer equals sorry i equals 0 i is less than the size of my colors so here it will be just 2. my colors colors i think you have dot size and then we are going to write i plus plus okay so let's go down and here we are going to add our bracket okay so now we will loop through these and for each loop we are going to get these values so now this one we can call it by saying my colors at the value of i that we are currently on so first it will be zero and then we can take the value of the first element so that will be 0 which will be this one then we can take the value of the second and third so all of these will be minimum so this will be first and this will be second and then we can do the same thing here so these will be the last three values so this will be three four and five so this way it will create a mask and we need to call the mask so here we are going to say the mask will be different every time we can call it inside as well max mask okay and then we can actually display this mask so let's display this so we will write i am show and then we will say let's say the iteration number so we will say to [Music] string because we cannot have the same name each time so we have to have something unique so something unique in this case will be the value of i so we can write that and then we can write for example mask so let's run this and see what happens so is it yeah it is at project one so here we are getting two masks for zero and for one so if i bring in the green one you will say only the green one is showing and if i bring in the purple one you will see only purple one is showing so that is good so don't worry about the noise too much so here you can see a little bit of noise coming in the other one so we will be able to remove that from the size or you can always go back and improve your values again now the best part about this is that if i add a new color here for example let's say we add what other color can we add let's say we add what do i have green purple orange let's say we add orange so i have found the values earlier so 0 62 0 35 2 255 and 255 and then we can add the color for orange so this is for green and this is four orange why is this giving an error it is keeping an error because there should be another bracket okay so then we need to add another color here we will write a comma and then here we are going to write for the orange what is the color value so it is 51 153 and 255 so we are doing this as an example so if i want to add another color how hard will that be so if we do that and we run it so now we have three masks and if i bring up all three colors at the same time let's see what happens so i have these three colors and there you go so now you can see each one of them will show you uh in the different mask i think the orange one is not properly tuned so we need to fix that but the purple and the green that we just did are very good so if i remove one of them you will see the orange is gone if i remove the purple purple is gone then the green uh we will remove the orange for now because i didn't tune it uh with this lighting so it will not be good but you get the idea of how to do it so hopefully you will be able to add and remove more so let's put it here and let's remove this okay so now that we are done with this we will go down and we will continue with this now what exactly are we uh what exactly do we require so from the color we right now have the mask and from this mask we need to find the points the exact points where we want to draw so to do that we will need to get the contours of these masks so for the color part we are pretty much done and we will come back to this function later on but for now what we need to do as as soon as we get the values we need to send them we need to send it to get contours so this will be our new function and i think we already wrote this function but we will have to tweak it a little bit so this is chapter number seven and you can see we already have this function get contours so we will copy it over here it has a lot of uh functionality so we are going to fix a few things from it okay so here we will paste it so get contours and then uh okay this is still showing another because we are not inputting anything so we will input the mask as our image and do we need anything else we don't so we will just have one image if we if we keep it or we remove it it doesn't make a big difference because usually the image we are declaring as a global variable so it should not be an issue anyways so here we have this uh dilated image or whatever you want to call it then here we will need to find the contours that is fine we need that and then we need the area yes we need the area for filtration that is correct and then we need to find the approximation so let me think do we need the approximation yes we need the approximation and then do we need the bounding rectangle yes we need the bounding rectangle and after that we don't need all of this so these aspect ratio what kind of shape it is we don't really care about that so we can remove all of that and did we declare these variables no so we don't need to worry about that and do we need to draw all of this that is the question um we can draw for just showing and displaying but other than that we don't need to draw that and this we have to declare let's say at the very top we need to declare this uh above this like that so that should work yeah so by default it will draw on the original image okay so that is good now if we send our mask to this contour function it is going to find the contour and it will find the bonding box so let's see how that works so let's run it so this is our image and if we go there and there you go so now you can see it's drawing the count contours around it and if we bring in the perp uh the green one or the red one then uh no did we do red no we did the orange oh no we removed it yeah so we only have two purple and green so these are the two that we have done and the idea is that we will get the value of this contour and at the top we are going to get this point once we have this point we are going to draw from the tip so we need to get this point here so how can we get that we can click from the bounding box so should we draw the bounding box uh let me undo okay now let's go back uh should we draw the bounding box yeah i think it's better to draw the bounding box so you understand a little bit further uh where is chapter seven yeah rectangle okay let's draw that over here so let's draw this as well so it will be in green color okay there you go so now we are detecting this and what we have to do is we have to get uh the bounding box so this will be the bounding box at the top and using this value we are going to get the tip of our uh what do you call the marker okay instead of getting it from the contour we are going to get it from the bounding box it will be much easier that way so how can we do that we have the bounding box value uh by the way i made a mistake in i checked it when i was recording these two should be above the for loop they should not be inside the for loop so in chapter number seven i think yeah so these should be outside the for loop and let me go back to chapter seven and do that there as well so these two should be above the for loop okay so going back to our project let's close the other yeah we'll save it so once we have the bounding wrecked what we can do is we can write here that our points my point so we need that point where we have to draw the circle so let's declare that points uh somewhere above here so we can say points my points is zero and zero okay so we will call the mypoint.x is equals to we will set the value of it bounding rect at the ith elements not this bracket at the eighth element plus bounding rect sorry this should be x plus bound rex at the eighth element the width of it divided by two we need it divided by two because we want to draw from the center and not from the edge okay so we want to draw from the edge let me show you in paint what i mean so if let's say this is the rect that we got we don't want to draw it we don't want to draw it from here we want to draw from the middle so we need to find this uh by dividing the width by half so this point here will be divided by two so that's the idea so we will divide this by 2 and then for the y we will copy this for the y we will just write it like this then y and we don't need the center because the y is already at this point if we make it center it will come here we don't want that okay so this is my point and once we have this point we are going to return this because this is the point where we want to draw our circle so we will return my point okay and because we are returning this we need to say that our function basically is not void it will be returning a point okay and then we need to get this point so here when we are calling the contour we need to say points my points is equals to get contours mask so now what we can do is we can store this value uh in an array or in a vector in which we will have all the values we need to draw so we can call a global variable here at the very top at the very top we can call this new points so we can say that it is a vector inside a vector which will have uh integer values inside of it so let me explain this structure so let's just call it first new points so the idea is like this so you have one vector and inside that you have multiple vectors so like this and inside each of these you will have three values one is the x then there is the y where we have to draw the center and the third one will be the value of the color so if the value is zero it will draw purple if the value is one it will draw green okay so this is the idea so for each one of these they will have the corresponding x and y and the color value so that is the main idea and let's go to let's go where is it yeah in fine colors so then we are going to append this or we are going to add it to our vector so let's write it here so we will call it new points dot push back push underscore back and we will say that we want to push my points oh my points dot x then my points points dot y is it point or points it is points okay and then we will write i so i is basically the color so because we are already getting one color one by one from my colors so whatever color we are at it will send it here so it will send the eye value so if it's 0 it means we are doing this color which is corresponding purple color then if we are doing this one it is one which means it will be green so that is the idea and once we have stored that then we can move on and draw all of these together but one problem is sometimes it will not detect anything and we will keep adding zero so that is not a good idea so what you want to do is you want to check if my points any one of those dot x is not equals to zero or you can write both of them so you can write my x is not equals to zero and my points dot y is not equals to zero so if both of them are not zero then we are going to uh append it to our new points okay so once we have the new points so this will loop and it will loop two times so we will have the points for our um what do you call purple and we have the point for our green so now we can draw these points so to draw them we are going to create a new function and we will not output anything from it we will just call it void draw on canvas and inside that we are going to accept our points so our new points that we have here and we are going to accept these color values because we need to color these when we draw we need to decide the color so we need both of these so let's copy this uh let's just copy the whole thing and we will paste it here so this is the first thing that we will need and the second thing we will need is our my color values so we can just copy this as well so these are the two things that we need okay and then we are going to write inside a for loop so it will be very simple we are we just have to loop all the points and color them that's it or draw them so integer i is equals to zero i is less than uh my points size so my points or it's not my point it's new points new points dot size and then we will say i plus plus and here we are going to write circle and we will send in our image because we want to draw it on that then we will have a point this point is the one that we detected here and we already appended it so we have the x and the y so we can simply write that so we have my points at the i position we are going to get the zero element so zero will be this element and then one will be this element so we will write it like this and then we will have the same thing again i'm confused between my points and new points so it should be new points so new points i at zero and then this maybe i should name it a little bit better so it's not that confusing so yeah so then you have this and then you have uh another bracket here because these are together and then we are going to write the size of the circle let's say the radius is 10 and then we have to write we have to write which color so the colors are present in my color values okay so we are going to refer to my color values so here we will write my color values okay that is good but which one should we pick the first one or the second one so that is dependent on this this is the i so we need to get this i and wait what is this okay we need to get the i so to get this we will write it like this new points so we need to get new points i at value number two so this should be our color and at the end we can call it filled uh it should be capital there you go so now it should be able to draw this circle on our uh image so let's run this and try it out fingers crossed okay so we have purple and it didn't draw anything also we are getting something small here we will have to remove that but for both of them it's not drawing anything excellent let's go back and see why oh yeah we didn't call it so we need to call the function so draw on canvas we didn't actually call it so here we are finding the color and after we found the color and the points we need to call this draw on canvas but we need the new points for that so we need to find the new points from here so these new points they are basically coming out of this find color so we need to return new points return new points and we can say that new points what structure does it have it has this structure so it is global so you could get away with not doing all of this but okay so we have pasted it here and then we are returning this new points and then we can send these new points over here and there you go so now it should work fine what is the error here uh does it accept anything it has new points and it needs the color values as well so we need to send in the color values as well so let's run this there you go oh it's drawing really at the top so it seems there is a problem so it's drawing the correct color but it's drawing at the wrong points so let's go and check what the problem is so it seems that the y for some reason is zero oh okay i found the issue these are not separate points they should be in one part like this so it should be point and then this and this so that is my bad so let's run it and if i bring purple there you go i can draw with purple if i bring green i can draw with green perfect now if i wanted to add another color i have already mentioned how you can do that let's stop this and run again i want to show you how it works with two at the same time so there you go like that yeah that looks beautiful if you like the video so far give it a thumbs up and don't forget to subscribe so this is how you can draw this virtual painter and now you can add more advanced methods to actually run this by joining the circles together to create a line instead of just dots and then you can add a erasing method and all these things you can add on to it but the concept is very simple and as you have seen it is quite easy to implement and all you have to do is you have to add another one in these two arrays or vectors and then it will display you that color as well so it is quite generic so you can keep adding more colors to it in this project we are going to create a document scanner now if you have used any of the apps on your mobile phone that scan your documents the idea behind this project will be the same so the methodology will be pretty much same as to those apps so it will be a very good project to practice real world applications and it will include all the things that we have studied before so we can practice our skills okay so to start we are going to go to our project project one we can exclude and then we can create a new project or a new file we will call it chapter or project number two and here we are going to copy from chapter number one because we are going to use an image now later on we can use a video as well it should work in real time but for now we are going to use an image and the image is paper dot jpg so let's run this and see what happens so there you go so this is the image and as you can see it is quite huge so it will be hard to maintain or to manage at this point so for temporary purposes we will just scale it down a little bit so once we are done with the project we will remove that scaling so that it gives us a good high quality image so here we can say actually let's call the mat over here matt let's say we have image original and then we have image gray image kanye and so on we will add all of the maths over here and what we can do is we can call this image original instead of image and then we can remove this from here and then we can say resize we are going to write image original and then our source will also be our destination will also be image original and we will not define any size instead we will define the scale we will scale it down to half so let's okay we need to change it here image original should be here and let's run that so there you go so this is our image scaled to half the size okay so now we can start with our project so the first thing we will do is the pre-processing of the image so we have to convert it into a grayscale and then we have to add blur and then we will use the kenny edge detector to find all the edges so the idea is that once we find the edges we will know where our paper is and then based on that paper uh coordinates we are going to extract the four points of or the four corners of our paper and then from that we are going to work our image so that we can get a top view so it looks scanned so that is the idea so first we are going to do uh pre pro processing the second step will be to get the contours get contours and from this contour we are going to get the biggest one we will assume that our a4 paper is the biggest rectangle in our image and then we are going to work the image so these are the three steps that we will be following so let's start with the first one which is pre-processing so we will create a new function processing and we will send in our original image so we want to pre-process this and we want the image back let's say that the image that we get back is called image threshold so we can declare this over here and now we can define our function so we will say that it will output an image and it is called preprocessing and it will receive an image which will be our image let's say so we'll have to define math image okay so that should work fine so let's start pre-processing our image so first uh all of this we have done in chapter two so let's go there and in chapter two we have uh gray then we have blur we have canny and then we have dilation um we can copy erode as well but i don't think we will be using erosion so let's paste everything here so we will start off with our image and we haven't declared image blur so we can declare it here now you could add all of these inside here as well but we are keeping it as global so that we can print out everything here so then we need the dilation and the erode dilation and image a road so yeah but as i mentioned uh we will not need a road for now but we will keep it if we need it later on so what we can do is we can because we have already seen all of this in action we can just print out our image dilation and see how it works out so we can return return our dilation image and then we can display it out so this will be our image threshold so let's copy this and we will copy this and paste it here so let's run that there you go so okay we need to change the name because it's overwriting it so let's say this is the dilation image dilation there we go so this is our original image and this is the image dilation so it looks really good we can see that we have all the edges properly detected so and this is the biggest one over here the biggest rectangle you will find so hopefully it will detect properly all the edges okay so that is done now we will go to our get contours and we will get the biggest contour we can find so we will create a function here called get contour get contours and then we will send in our image threshold and now what we can do is we can copy the image threshold from the get contour function that we did in project number one so let's go to project number one and from here we are going to copy our get contour function and we can paste it in project number two so this way we will not have to worry about writing it all again so we can remove whatever we don't need so the idea here is that we need our first of all we need to find the contours so that is good we will keep that then we need to do the filtration with area so we will keep that as well and after that we will need the approximation of our poly count so we need to find the ones that are four and then these points we are going to send back so we do not need the bounding rectangle so we can remove all of this we can keep the draw we can draw it on the original image already chain null we can do the same over here uh do we need to draw the rectangle no not really but i will keep it here in case you want to see so we just need that and for the returning parts we are going to return a vector of points because we will need all the four points of our biggest contour so we need to return that so here we are going to say instead of returning just one point we need a vector of points and here we are going to return that vector of points so let's call this the biggest so we will call it here instead of my point we can remove that do we need it no i don't think we need this yeah so instead of that we are going to write vector then points and then we are going to write biggest so this will be the biggest and we are going to return this over here okay all good now we need to find this biggest contour so we already know the the size of our contour which means how many points we have is it four three two one so we can check it here and we can say that bracket if our area is greater than max area so this max area will be the biggest one it has found yet so every time it will loop we will update this max area so but we need to define it so let's define our max area over here where did we define the area okay let's define it outside here so we can define integer max area so this max area initially it will be zero then whatever it will find whatever area it will find it will replace it with that and then it will check if it if there is a bigger one than that if it is it will replace which means we can write it like this if that area is greater than max area then max area is equals to area so this will make sure that we always get the biggest one but then we also have to check that if it is a rectangle or not we do not need a circle or a triangle so we will write here and we will write con poly at this ith element is that value equals to 4 the size of it the size of this equals to 4. if that is the case then we are going to apply all of this so why are we getting this error because i wrote the spelling wrong okay so now we will say that once it is inside we are going to replace our biggest with the values of this con poly okay so the biggest one we find we will store the values inside so we will say our biggest is equals to poly at i and then we are going to get the value of 0 then we will get the value of 1 2 and 3. so i can copy this and we can paste it here so this is three this is two and this is one and we can close this so now whenever it will loop through all the contours it will check if it's the biggest one if it is the biggest one it will replace the value of the biggest with the one it has found and once all the looping is done it will return the biggest one back to us so over here at the end we can get the biggest value from these points so when we receive these values we will call them initial points now the reason i'm calling this initial points because we need to process them more before we work it i will show you why so we will call this initial points and we need to declare them over here so basically these are vector of points and we can call it initial points okay so now that we have done our initial points let's draw them out so what we can do is we can create a new function to draw them uh we can call it draw points so we will not output anything and we can just write draw points and we will send in our points and we can send in for example a color as well if we need so we can write vector give us a vector of points and then we will write here points and then we also need a scalar for color so now we can write here a for loop for uh integer i is equals to zero i is less than the size of our points dot size then we are going to i plus plus and inside we are going to put a circle so circle and we will write that we want it on the image and we are going to use our points they are already uh in the correct format so we can just write i and then we will write uh let's say the size of the circle is 30 and the color is the color that we have and we want them filled okay so we can also what we can do is we can also put the text so this is original we can also put the text which will really tell you why these are not the final points and the initial points so let's put the text we will call it on the original image and then we will change what we want to display is the number of uh the points so what we will do is we will display this i so we will first convert it into a string so we can write two string and we can print i then we are going to give it the same points points at value i and then we have the font so let's pick the plain one and then we have the scale and then the color and then the thickness let's do that so i think 10 will be too much let's put five okay so now we can call draw points over here and inside we can send our points so we have the initial points and what else do we need the color so the color we can send for example let's say red because these are not the original points the final points we will send in the red and for the correct ones we will send in green so let's draw these points and see what happens whoa that is really big um actually let's do one thing before we go there let's go back up and let's only draw what is the draw let's only draw the ones that are the final ones okay so let's copy this and we will only draw the ones that fit this description okay only the rectangles that are big so we can remove this and we can increase the thickness of this so let's say five and that should be good also the text was really big so we can put let's say two and two so let's run that again okay so okay the size is too big for the circles it is 30 let's put it as five as well there you go okay 5 is too small let's put it 10 and let's increase this to 4 and 4 let's see there you go now it is clear so now it is very clear that the only uh candidate that we have for the rectangle is this and we have the found we have found the corners for this now the thing is every time it detects in a new image right now it says this is zero but next time this might be zero or this might be zero so we need an exact uh what do you call format in which we need to find these values so it should be 0 1 2 and 3. so all of them should be reordered in this manner so every time it should be 0 1 2 and 3. so how can we do that well what we can do is we can find the values of this we can find the values of this this and this and we can add each one of these so what i mean is we have the x and y of this so we will add x plus y and that should give us the least value and if we add x and y of this it should give us the maximum value so it will be the highest value and these two how we can find these two is by uh subtracting these values so if we subtract this one should give us the highest value and this one should give us the lowest value so depending on if we are subtracting x from y or y from x so using the summation and differentiation we can reorder these uh values so all of them they are in the correct order so the correct order should be 0 1 2 and 3. so we are going to create a new function called reorder so where is yeah so here we are going to say before we send it for warping we are going to say re-order and we are going to send in our initial points and the one that we receive we are going to call them let's say the final points or the document points so doc points so these will be the final ones so we need to declare this and we need to declare this let's go up initial points and doc points will be in the same format so we do not need to change that and now we need to declare reorder so here we will say reorder and we will get our points so here we will say that we will receive a vector of points and then we are going to write it let's say uh points we will call it point and we are going to send the same thing back so let's write it here like that okay so now that is done what we will do is uh we will first create a vector of new points that we will send back so we will call them new points and then we need to create a vector of all the summations and all the subtractions so we will call this vector and inside that we will just have integers so for example for the first four points let's say that our points are let's say 23 45 then the second point is let's say uh 430 then the third points is let's say uh 25 and 300 and the last point or let's put it here the last point is uh 350 and 400 okay so let's say these are our four points so what we are doing is we are creating a vector in which we will have the sum of these two so whatever the sum of these two is we will have it here so let's say this is 45 and 23 so the sum will be 68. so this is basically what we are doing we are adding all of these together so the sum of this will be 430 and whatever the sum of this is this is 750 and then this is 3 25. so this vector we will call it some points will have these four values now once we have this vector it will be something like this 68 430 750 and 325. so once we have this vector we will find the maximum and the minimum value the minimum value will be this so we will say that this is our zero zero and the maximum point will be this 750 so we will say that this is our width and height this is the most corner point so this should be the last point and then we will have another one for what you call subtraction of points and from that we will say that the maximum one will be point number one and the minimum one will be point number two so that is how we will do this i hope that is clear you can print out these values and you will understand them more so what we will do that we will simply add our points so we will say points at let's say number zero dot x plus points at number one sorry at number zero dot y so this is the submission of our first point so then we have to do the submission of the second point right so instead of zero we will put one and so on and once we do that we will save the value in the points so we will say some points then we will say dot pushback and we will push the sum value over here right so this is what we are going to do but instead of writing each one of these one by one what we can do is we can put for loop and here we will write four integer i is equals to zero and we know that we will have four values so we will write four and then i plus plus so this way we can instead of writing zero here we can write i and each time it will add those values and it will save it in the sum points then the same thing we can do for the subtraction so for subtraction we are going to write sub and that should be enough so we will subtract it here and that is good to go so now we have all the four values of submission and all the four values of subtraction in some points and sub points so now we need to find the maximum and the minimum value of this what we can do is we can write minimum element and we can write we need the minimum element of our sum point and we need to give a range from where we want to begin and where we want to end so we want to begin at the very beginning and we want to end at the very end and to get the index of this what we can do is we can subtract it from the end point or sorry the beginning point so we will write here sum points dot begin there you go so this part here will give you the index value so let's say our vector was like this 68 i don't know 253 then 750 and then 350. so if this was our vector it will tell us the minimum index so the index answer of this will be 0 because it is here and then if we do the same thing with max then it will give us the max value which will be 750 and then the index of this will be 2 so this is 0 1 and 2. so it will give us the value 2 so this will yield to 0 this will yield 2. so what we can do is we can push this to our new points but we do not need to push the index we do not need to push this what we need to push are the actual points so what we can do is we can get our original points from this index okay once we get these points now we can push it to our new points so we can call these new points dot push back push back and then we can push it at this point now the sequence has to be exactly the same so we should push this for the index 0 this one should be the last value so when we push this we should push it at the very end okay so this one should be index number 3. okay so in between we should have two more uh pushes okay so we need to complete this first before going forward and we need to put a bracket here and then a bracket here and then close this here so i think this is extra yeah there you go so here it should be zero then we will have another one for one and then for two so what are the other two the other two are basically the same thing but using the subtraction points so sub points and the sub points and the sub points so the minimum one will be the second element and the maximum one will be your let me copy that the maximum one uh where is it this here the maximum one will be your element number one so this way we can reorder these values and at the end we can simply write return our new points so let's go back and let's see what difference did it make so we can uh draw the points on our initial points and then we can also draw why is it keeping an error for initial points because of the spelling so then we can draw now we can draw actually this is the function for drawing we can draw the doc points which are our final points so let's change the color for it zero and we will put it green so let's run that and see what happens there you go so now you can see it's a 0 1 2 and 3 exactly as we needed earlier this was three but now this is index one okay so earlier this was index one but now this is index two as it should have been so we can remove this draw to make it a little bit cleaner and there you go so this is our image now 0 1 2 and 3. so now we can use these points to actually work now we are confident that whatever image we use it will always have this as 0 this as 1 this s2 and this s3 so that being said now we can create our working function so here we will call our verb so we will call it image verb were is equals to get work and we will send in our original image image or a chain now and then we will send in the doc points the points based on which we want to work and we will also send the width and the height so this is the width and the height of the a4 paper so what exactly is the width and the height now the width and the height of a4 paper is quite small so instead we are going to multiply it by two and use those as pixels so we will write here float and width multiplied by 2 will be 420 and the height multiplied by 2 will be 596. so these are the dimensions of our a4 paper multiplied by two and we can go back here so we have the width and the height and then we have the original image and now we need to define this and this so image work we can go up and define here image work and oh is there a problem image work no no problem okay so then we can go down and we can define the function get verb so do we need to output anything yeah we need to output the image itself so we can write here mat and then we will write get work and inside that we need our original image so we will call it let's say image and then we will need our vector vector of points which will be this doc points and then we will need the float of widths and float off the height okay so now we can go on to our chapter number i think five yeah warping images and we can copy all the code from there so this is basically the code that we require we can copy this and come back to project number two and we can paste it here so now we need to change the points over here these points will remain the same because these are 0 0 in the first at the end we have width and height then width and 0 and 0 and height so these are the same but here we need to change them so instead of writing all of this we will change it to our new points so points at zero then points at one and then we can copy this and we can write here three and four so this is our main idea and then we have our matrix we did not define it we can just define it here mat matrix and then we have our image this is fine image output is fine matrix and again width and height is fine so all we need to do now is to return our image warp so that should be good so now we can output our image work so let's copy this and paste it here and we will write image warp over here so let's run this what happened there oh i made the mistake here of writing three and four instead it should be two and three okay so let's run it again there you go so now we are getting the verb now uh because we are already done with this part we can remove the drawing or we can draw it after warping so let's remove this draw and where is the other draw i think it was inside here yeah so we can remove this in the contours because we are drawing the contours so let's remove those and there you go so this is our image worked now we have received the document and we have warped it but you can see here that we have some edges displaying here and that looks bad it doesn't look like an actual document so what can you do we can crop this as we have used in our previous videos or the previous chapter so we can crop it here so let's crop by creating a rectangle so basically what we can do is we can define the exact value that we want to crop so let's call it roi and inside that we will give it a crop value let's say we want to crop five pixels from each side so we will write here five and then we will write here five and then we have to write the width and the height so the width will be w uh then we will subtract 5 from it and we also have to subtract this 5 from it so we need to multiply it by 2 so 2 multiplied by 5 and then we will have height minus 2 multiplied by 5. so this is how you can create this rectangle and then all you have to do is you have to write image crop is equals to image work and then you have to say roi okay so here we haven't defined image crop so we can go at the top and we can define it here and at the bottom we can display this so if we write here image crop and here we can write crop as well okay i found out that we are displaying both of these with the same name so we need to change this okay so let's run this oh yeah the width and the height i didn't define so it will not be properly done or is it already defined so yeah it's already defined the width and the height so we need to we don't need to change it okay okay so yeah so now you can see that the pixels this is much better than this image because now it is uh cropped from the sides but what if i want to change it again i will have to replace five each time so instead we can declare a variable so we can say let's say crop value crop val and we can give it a value of let's say 10 and then over here we can replace all the five with crop value there you go so now we can run this and there you go so now it has been cropped further so let's put a crazy amount of crop let's put hundreds and there you go so now it's really really cropped so that is the basic idea so what you can do is you can put it as a at a reasonable value let's say five and what we will do now because we are reaching the end we will remove this resize so now it will do it on the original image so our final image will be much cleaner there you go now it's much sharper than the previous images that we saw because it's getting from the big image so this is how basically you can do this what you can also do is you can add a webcam to this instead of getting it from here you can add a video and you can run it with that as well so i want you to try this out by yourself and later on i can add the code on my website but i really want you to try it out so that you understand it how you can use uh with the video i will add a sample footage of that as well so you get encouraged but do try it out with the video as well it will be in real time in this project we are going to learn how to detect vehicle number plates and how to crop these regions and save it in an external file now the method we are going to use is based on the viola jones method that we used in chapter number eight so we are going to copy our code from there and change it so first of all we need to add a file so we will go to we will right click to our project and we will add a new item we will call it project 3 and then we will go to chapter 8 and copy all of the code from there and we will paste it here so we can call this project 3. and then we need the code from chapter number one because we are going to use a webcam instead of just an image so the webcam code is over here so we can copy all of this and we can paste it here now what are the things that we need to replace first of all instead of the image we need the video capture device and we just need to declare our matte image so we will replace this with this and then we need to add a while loop and we need to add this capture read so we are going to remove the while loop and we will paste it so all of this is about loading so we do not need to load it again and again even the vector we do not need to declare again again so we can paste it here because we do need to detect the faces again and again so we can write the while loop here and then we can close it here also we will need to change the 0 to 1 so that it can repeat again and again so the rest of it we can delete so we can remove that so there you go so now if we run one more thing i need to change this to one because i'm using two webcams okay there you go so now our webcam is running so we can just go back and now all of this is regarding face detection so we need to change it to plate detection so it's not necessary but it's good to use the correct naming so we can replace it wherever it is required and there you go so you can do a find and replace but i prefer doing it this way because i know exactly what i'm changing and what needs to be changed as well so then we have to uh change these faces to plates blades so we will change it here here here and here then we need to change the the hard cascade file so we will go over resources and over here we will take the russian number plate we will copy the name for this and over here we are going to paste it there you go and now if we run it it should be able to detect the number plates so actually let's go to google and google some recreates so here i have googled some vehicle number plates uh russian number plates so we will test out with this so let's run this first and there you go so now we can try it out there you go so now it's detecting the number plate over here and then over here if we go down maybe click on this image yeah it's detecting this as well you can play around with the values to find out the optimum ones so there you go so it might give some false detections as well as you can see here so you can make it you can change the parameters to find the exact values that suit your needs so you can change these values over here okay so once we get our number plate what we can do is we can crop the region that is for the number plate itself so before we actually draw the rectangle we should crop it so here we will say that our new image image crop is equals to the original image cropped at the plates points so plates is basically a rectangle right it has x y width and height so we don't need to change anything we just need to pass that in to our image and it should crop it so what we can do is we can display this so we can write i am show and we can display now for each crop we need a different name so instead we can use for example i for this reason so we can write here to understore string and we can write the value of i inside and then we can write that we want to crop our image we want to display our image crop so even if it has two or three it will display all of them properly so let's run this and open up our number plates here and let's see if it detects and crops both of them or not so there you go so now as it is running it will crop and it will detect so right now it's overlapping because let me try to squeeze it down so let's go a little bit higher there you go so now if it detects two plates it will show the result of these two plates so then what we can do is we can save these plates so to save it we will go to resources we will open that up and over here we are going to create a new folder called let's say plates okay and here when we are getting our image crop instead of showing it we can save it so we can write here i am right and then we are going to give it a name so the name will be our resources resources slash plates slash and then we will add a name so you can write a timestamp if you want a very unique name and if you want to write all the number plates but for the sake of it for simplicity we are just going to write this 0 and 1 as the name so we will write this and then we will also write at the end we will write dot png so this will be the name and then we need to give in our image so we will write image crop so we need to store that image so let's run this and we will open up our resources folder and in the plates right now we don't have anything because no number plates are detected let's keep that here and then let's open up vehicle number plates and let's try it out so now you can see we have two images that are shown let's see let's scale them up and there you go so now you can see these two images were detecting and cropped and they were saved so the same way you can detect more images and it will save all of them so here they are saved as 0 and 1 but if you want you can add a timestamp so it will be a very unique name so you will be able to add a lot of different number plates so this was the basic idea of how you can implement how easily you can implement the vehicle number plate detection using the hard cascade files now if you want to detect other objects you can find the files for those or if you want you can create your own harcascade file using your own data i have a complete video on that uh you i will put the link in the description and in the course as well so that if you want to create your own cascade file you can find that and you can follow that follow that process so this brings us to the end of our course i hope you have learned something new if you like the video if you like the course give it a thumbs up and leave some comments below and if you haven't subscribed yet do subscribe because i upload videos on a weekly basis and do turn on that notification as well so you keep updated so that's it for today and i will see you in the next one
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 1,220,308
Rating: 4.8649526 out of 5
Keywords: object detection, face, haar, opencv tutorial, computer vision, opencv tutorial for beginners, Learn OpenCV, install opencv, opencv projects, opencv object detection, learn python, opencv, computer vision basics, image processing, learn computer vision, opencv c++, c++ opencv tutorial, c++ opencv course, c++ course, face detection c++ opencv, Learn opencv c++, computer vision c++, c++ tutorial for begineers, opencv projects c++
Id: 2FYm3GOonhk
Channel Id: undefined
Length: 237min 3sec (14223 seconds)
Published: Sun Dec 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.