OpenCV Python Tutorial For Beginners 23 - Find and Draw Contours with OpenCV in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the next video on open CV tutorial for beginners using Python in this video we are going to understand what contours are and we are going to see how to find contours and how to draw contours so first of all what are contours so contours can be explained as the curve joining all the continuous point along the boundary which are having the same color or intensity now contours can be a useful tool for shape analysis or object detection or object recognition now for better accuracy we generally use binary image for finding the contours so first of all we are going to generate the binary image and then before finding out the contours we are going to apply the threshold or canny edge detection to find the contours on the image so let's start with an example so here I have a simple code which reads an image and then converts this image into a gray scale image and then I'm just showing both the images using I am show method so let's run this code and let's see what result we get so this is the original image with these colors and after the conversion of this image to the grayscale image this is the result which we are getting and then we are going to find out the threshold or the canny edge so in this example we are going to just apply the threshold so for applying the threshold on this image we are going to define first of all two variable ret comma Thresh is equal to C v2 dot threshold so there is a method called threshold which we have already seen how threshold work in detail in the previous videos so the first argument which this threshold method takes is the image so we are going to pass our grayscale image as the source the second argument is the threshold value so because it's a binary image let's set the threshold to 127 which is around half of the 255 right the third argument is the maximum values on the maximum value here will be 255 the next argument will be the type and type here will be 0 so this is going to give us the trash hold value for this greyscale image and after finding out the threshold of this image we are going to find out the contours so for this we are going to define two variables one is contours and the second is the hierarchy because the method which we are going to use which is CB to dot find contours this is the method it's going to give us these two values contours and the hierarchy and we are going to see what our contours and hierarchy in details after applying this method on this image so the first argument will be the Thresh which we got using this threshold method the second argument will be the contour mode so this is called the contour retrieval mode also and there can be several possibilities here which we can apply but for simplicity at the end in the most common case we use retr underscore tree here okay as the mode the third argument here will be the method which we want to apply and this is also called the contour approximation method and here also several possibilities are possible but for now what we are going to use here is this will be CB 2 dot aprox nun so now as you are seeing here this fine contour method gives us contours and hierarchy so the contour is a Python list of all contours in the image and each individual contour is an umpire array of X comma y coordinates of boundary points of the object and the hierarchy is the optional output vector which is containing the information about image topology and this we are going to see in the later videos so for now we are only concerned about finding out the contours so for this as I said this contains the number of contours right so we can print out these number of contours is equal to and then we are going to just convert this number into the string and there is a method called length and then inside this length method we are going to pass our contour variable so this line is going to print out the number of contours which are found inside the image which we are providing so let's run this code and let's see what result we get so we already know that this gives us a grayscale image and the original image but we are interested in this print message and the number of contours which are found is nine inside the source image which we are providing here now we already found out the number of contours now we need to draw these contours on the image itself so how can we achieve this but before this lets see the individual contour also so I'm going to just print out the value of the first contour which will be at index 0 so let's run it once again and let's see what happens so we are running this code once again and you can see after printing out the number of contours it's going to give us the numpy array of the x and y coordinates so if we plot or join all these x and y coordinates we are going to get the boundary of the contour so now we are going to just take these contours and pass it to a method called draw contours which is going to draw or join all these coordinates of those contours so to get this we are going to just say cb2 dot draw contours and then the first argument here will be our original image because we want to draw the contours on our original image so this will be the IMG and it's the original image and the second argument will be the contours so we are going to just pass the contours which we found inside the image the third argument will be the contours indexes so if we just gave here minus one then it's going to draw all the nine contours which were found inside the image these all contours so first of all we will give minus one here as an argument and then we will see how to give other arguments as the numbers here also the fourth argument here will be I think the color so we are going to just give the color 0 comma 255 comma 0 let's say and the next argument will be the thickness so we are going to take the thickness 3 here so using this method what we have achieved is we have drawn the contours on the original image so let's run this code once again and let's see what result we get so you can see this was the grayscale image and this we have used for finding out the contours but the interesting image here is this one and here you can see all the contours are drawn on this image so all the green lines or green boundaries are all contours so because we have given minus 1 it has drawn all the contours on this image and we can also give the contour index so let's say we just want to find out the contour 0 which will be the first contour which is found inside the image we are going to just run this code once again and the first contour which was find out found out a this contours this P contour right in a similar way we can go up to 8 so 0 1 and let's rerun this code again you will see that the second contour is this contour so this whole contour from the boundary of this image is the second contour and in a similar way you can go let's say - I'm going to run this code once again you will see the next contour here and similarly you can go up to the index 8 because the total number of contours are 9 and we are starting from the index 0 that's why we can go up to 8 so this value depends on the number of contours ok so because we found out the number of contours are 9 so that's why we can go up to 8 and let's run this code and the last contour which was find out and we have drawn this contour here on this blue circle right now if we go beyond this index let's say we gave 9 here we are going to get the error right so you can go up to 8 here and if you want to just draw all the contours then you can just give -1 here and it's going to draw all the control on the image which you are providing so this is how you can find out the contours and draw contours on the images using fine contour and draw contour methods in OpenCV I hope you have enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 97,481
Rating: undefined out of 5
Keywords: OpenCV Tutorial, Python (Programming Language), Python 3.6, Python, Python Tutorial, Python course, Online Course, OpenCV, OpenCV Tutorial for Beginners, Computer Vision, Computer Vision Basics, Computer Vision Tutorial, Windows, Linux, Image Processing, OpenCV Python Tutorial, OpenCV Python, blurring images, Laplacian, Image Pyramids, Gaussian Pyramid, Contours, findContours, drawContours, Draw Contours, threshold
Id: FbR9Xr0TVdY
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Wed Jun 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.