OpenCV Python Tutorial For Beginners 17 - Morphological Transformations

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 discuss about morphological transformations in open CV so we will discuss different morphological operations like erosion dilation opening and closing methods etc but first of all what are morphological transformations so morphological transformations are some simple operations based on the image shape now morphological transformation is normally performed on a binary image and when we perform morphological transformation there are two things which are required first is the original image and second is called a structuring element or a kernel which decides the nature of the operation now there are different type of morphological transformations and we are going to see them one by one now to start with I have this simple code which reads the image using open CV I am read method and we are just loading or showing this image using matplotlib now if you are unfamiliar with matplotlib and how to use matplotlib to show images in the last video I have explained this topic in details so if you want to see that video about matplotlib you can see it and this is the code I have used in the last video also and and I have explained this code in details in the last video so if you are confused what this code is doing just see the last video now there is one important thing to notice here is I am reading this image in a grayscale mode okay so either you can provide here as the second argument of I am read CV 2 dot I am read underscore grayscale or you can provide simply zero here in order to read this image in grayscale so let's run this code and let's see what it does so as expected it's just opening the image in the grayscale mode using matplotlib now as I said normally we perform the morphological transformations on the binary images so that's why we need to provide a mask to our image using the simple thresholding so let's just do that so I'm going to just write underscore comma the mask so I'm going to name my variable as mask here and then I'm going to just write cb2 dot threshold and this threshold take few argument as you might already guess first is the image itself second argument is the value of the threshold so for now I'm going to just provide the threshold of 220 here the maximum value of threshold will be 255 then the next argument here is the type of the threshold so we are going to provide C v2 dot thrash binary inverse so this is our mask so let's load the mask in the matplotlib window so I'm going to just provide in this titles array one more title which is mask and then we are going to see how this image looks like after the mask okay and here the range I'm going to increase it to two because now the array is of two elements and the subplot is also let's say 1 by 2 so we want to show two images and I'm going to just run this code and you can see this was the image which was a grayscale image and the second image is the masked image now if you see this image carefully let me just just increase the size of this and if you see this image carefully after masking there are some black dots here on the balls and let's say we want to remove these dots which are there in between this white area this black dot or this black dot or you can see some black dots are there inside your ball in the bite area and we want to remove these dots from the balls for this we are going to use the dilation transformation so first of all what we are going to do is we are going to just write dilation which will be our variable name and then we are going to use this method called CB to dot dilate okay so this method uses the source which is mask in our case and then the second thing is the kernel okay so let me explain what the kernel is so a kernel is normally a square or some shape which we want to apply on the image so we are going to define a kernel of numpy once which means we want to apply white square on our balls so you can see when we run our code once again it shows us error because this kernel is undefined so let me define this kernel first of all so I'm going to just say kernel is equal to and P dot once and then we are going to define the shape of this kernel let's say this is of 2 comma 2 size and then we will just say NP dot u int 8 so this is our colonel and colonel in this case is nothing but a 2x2 square sheep and this square shape kernel is going to be applied on our image wherever these black dots are there so now we have defined this kernel so let's see after this kernel is applied on our masked image how it looks like so I'm going to just add one more title here which is dilation and then I'm going to add the image after the dilation is applied on our image and then we are just going to increase the range to three because now we have three images and let's say this plot contains images one by three so one row and three columns right so I'm going to just run this code once again and now you can see all these three images first was the original image second is the masked image and the third one is the image which we got after we applied the dilation let me just increase the size of this image somehow so now you can see that for example here there was a black dot and now it's reduced right the size of this black dot is reduced here also there was a black dot but its size also is reduced but still we can see these black dots here right so how we can remove these black dots completely so there is a third parameter which we can provide to this dilate method it's called iteration so number of iterations so we can just provide iterations is equal to whatever the number of times we want to perform dilation on our image by default it's 1 and you can provide let's say 2 here and let's see what is the result now so I'm going to just run this code again and now you can see those black dots which we can see here on the masked image are now gradually gone but still I can see some little dots on the images the small dots are already gone right so now what we can do here is we can increase the size of the rectangle so this rectangle is applied to our area which have these spots so we can increase the size of the rectangle and the bigger the rectangle is the better the result will be but there will be a problem which I'm going to show you so let's run this code and you can see now all the black dots from our image is gone so there was a black dot here which you don't see anymore and there was a black dot here here here and here and we don't see these black dots here but you might also observe that the size of this white area is also increased after we applied the dilation on this masked image so now this ball and this ball in the result after the dilation is merging here right so you can see it's merging because the size of our kernel is a big and when we apply dilation the pixel element is one if at least one pixel under the kernel is one that's why the shape of these balls are increasing so let's see how our next morphological transformation works which is called erosion and after that I'm going to explain you how this erosion works and what is erosion so I'm going to just declare a variable called erosion and I'm going to just call a method called C v2 dot erode so the method name is a road and the first argument here is the source the second argument here is the kernel as we have seen in dilate method and the third argument is the optional argument which is the iterations so for now we just apply one iteration which is by default also one and now we are going to just add this image to our matplotlib window so I'm going to add the title and the image and now I will increase the range of the array to four and let's say now we want two by two matrix of these images right so let's run this code and let's see what happens so now you can see four results here and first was the original image second was the masked image third was the dilation so all the spots in the balls which are black are gone using the dilation but the size was increased and using the erosion you can see the sides of the ball eroded so the basic idea of erosion is just like soil erosion it erodes away the boundary of the foreground object so when this erosion is applied the kernel which we have defined slides through all the image and a pixel in the original image either 1 or 0 will be considered as 1 only if all the pixels under the kernel is 1 otherwise it is eroded and this means this value will be set to 0 which means this will be a black area so let's increase the number of iterations here so let's say we want to like the erosion two times on the same image and I'm going to just run this code once again and you can see now these balls are eroded more let's say we want to increase this to five times and then run the code and you can see now these balls are really small because we have applied this erosion multiple number of times so let's say this is one once again and let's make this size of our kernel small two-by-two rectangle size right so you can see now our result is better because all the spots from these balls are gone and these balls are not so much eroded now there are two more morphological transformation methods which are called opening and closing so we are going to first of all see how opening works I'm going to define a variable called opening and then I will call CB to dot more for logy X okay and then we will provide the source which is mask the second method is the type of morphological operation which we want to perform so in this we are going to just call C v2 dot and then we can specify which type of morphological operation we want to perform on the image so just right morph and then the type of operation so we want to perform the morph open for the opening right and then the third argument here is the kernel which we have defined and now we are going to just add this opening to our matplotlib window let's add this and then let's do five here and then let's say our my plot lab is going to show these images in two by three format so let's run this code and let's see what happens and let me increase the size of this image now and this is the result of the opening so what is opening in morphological transformations so opening is just another name of erosion followed by dilation so when you perform this opening morphological operation first of all erosion is performed on the image and then the dilation will be performed on the image so you can see the effect of the erosion followed by the dilation still you see some spots here which can go if you can just increase the size of this block so let's rerun the code let's see what happens so now this image somehow looks better than the older image so opening is the erosion followed by dilation now there is a closing method also which is just the opposite of opening in the closing morphological transformation dilation is performed first on the image and then it is followed by the erosion so let's see if we get the better result when we perform the closing morphological operations and the morphological operation here will be close and run this code and now you can see the result here so in closing as I said first of all that dilation is applied and then the erosion is applied in the opening first of all erosion is applied and then the dilation will be applied now there are different type of morphological operations you can apply using this morphology X so for example I'm going to just use some of them so the main morphological operations other than opening and closing is let's say morphological gradient so I'm going to just say M G form of logical gradient and you just need to change the second argument here so CV to mauve underscore morphological gradient so we are going to just call this morph gradient and it's going to apply the morphological gradient and then the next is the top hat and the black hat so there are different morphological techniques you can apply so I'm going to show you one more and then I will leave you with the other techniques so th for top hat and here also the second argument you just need to change it to top hat right otherwise you can see there are so many number of techniques you can apply on your image so there is gradient close open we have already seen black hat cross dilate ellipse erode hit miss rect and then top hat which is we are going to use right now right and then we can just add these two things to our list of titles and list of images so M G and then we have th for top hat and now we have eight images so range is increased to eight and let's say we just want to show them in two by four matrix here in the matplotlib window so you can see this is the result of morphological gradient so morphological gradient is the difference between the dilation and erosion of an image and this is the result of top-hat that means it is the difference between the image and the opening of an image so this is how you can perform some of the morphological operations on the images now I will show you one more example I have a image called J dot PNG so I'm going to just load this image also and because this J dot PNG is already a binary image I don't need to apply this mask here so instead of this mask I can just directly use our image variable so I'm going to just write this now let's load this image two times because we already have defined this mask variable inside our Titleist and image list and now I'm going to just run this code so the original image of this J dot PNG looks like this and after we applied the dilation you can see the dilation increases the area of this J the erosion just erodes away the corners of this J right opening is going to apply the erosion first followed by the dilation and closing is going to first of all perform the dilation followed by the erosion this morphological gradient is going to give you the difference between the dilation and erosion of the image so it's going to give you this kind of result and you can see the top hat result here which is the difference between the input image and the opening of the image so this is how you can use different type of morphological transformations on your images using open CV I hope you've enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 78,276
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, Image Thresholding, OpenCV Threshold, matplotlib, matplotlib with OpenCV, 2D plotting library, Matplotlib RGB
Id: xSzsD4kXhRw
Channel Id: undefined
Length: 22min 17sec (1337 seconds)
Published: Thu May 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.