23b - Image segmentation using color spaces​ - in python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i'm going to talk about something very basic as it relates to image segmentation which is using color spaces for segmenting your images and apparently i missed this topic completely when i covered the histogram based segmentation more than two three years ago or something and recently quite a few started asking questions where well that kind of relate to this type of a solution so here we go and do not forget to hit the subscribe button and for those of you feeling extra generous you can show your generosity by hitting the thanks button down below okay now let me go ahead and jump into the code but the goal is to do exactly what you see on the screen you have a bunch of colors in your image and you would like to segment pixels based on the specific color i do not recommend segmenting using the rgb color so the the approach that we're going to follow is convert rgb into a different color space and then use that color space for segmentation because you have much more control when you do that so let's cover all of that it should be pretty quick and short video so let's go ahead and jump into the code and the image we are going to segment is basically the one that you saw on the primary screen and here we can choose any of the colors let's go ahead and choose the blue one by the way you can choose the yellow color but then again if you want to segment not just the pixels but the objects then you know that you should be using watershed to separate these two objects that would be probably combined or joined together depending upon how you choose your color but i covered watershed in the past so go ahead and check that video so let me close this and get started with the code we are going to use opencv library primarily and i'm also going to use psycit image for measuring and also for reading the images and all of these should be pretty familiar to you the regular viewer of my channel again hit the subscribe button if you are not a subscriber so let's go ahead and import pipelot for plotting purposes so we can view the images open cv and numpy obviously we need to handle some numbers and from scikit image i'm going to import i o and measure methods so there you go so let's start by importing our image i call my image marble star jpeg so i'm using io.imri and if you want to see how the image looks like go ahead and plot it should be looking exactly the same unless you use opencv to read the images i i'm using psychic image for a reason because when i read images using psychic image it reads them as rgb so the colors look exactly the same but if you use opencv then it reads images as bgr so do not forget to convert them into rgb so there's there is a method for conversion of bgr to rgb so i don't want to take care of all of that so i'm just using psychic image for reading my images and once it's in the code it's basically a numpy array and three channels three channels representing r g and b red green and blue okay ideally you can actually go and just choose the blue channel and try to segment these objects using the blue channel but you have much better control if you if you move from or if you convert your color space from rgb to hsp hue saturation and uh i'll talk about this in a second in fact i have a link up here that we will be visiting anything in a minute okay so first thing first how do you convert rgb to hsv it's very straightforward if you're using opencv it's just cbt.cvt color and we want to convert rgb to hsv and by the way if you are using opencv to read your image there is a method for bgr to hsv okay so again please be familiar with opencv it's got a lot of cool stuff okay let's convert this and when we do that if you look at your hsb image right there it should be exactly the same dimensions but the numbers look different because now we are in the hue saturation space so now we can actually create a mask or threshold the image by selecting only the ones that we care about in this case we only care about the blue ones the blue marbles so what color how do you pick your blue uh color again there are various resources for hsv space but this one seems to be a pretty straightforward one i just did a quick google search and i found this and i really love the way this this person actually presented the result right here so think of this as i mean this is hsp space represented right there from 0 to 180 is the hue right so that's the hue space and it's going all the way from red through green through blue through all the way back to red so that's zero to 180. now the saturation goes from zero to 255. if you look at zero saturation that's basically white up there there's no saturation right so that's white as you increase the saturation it gets more and more bright and it goes all the way from 0 to 55 and the v is set to range 20 to 255 according to this person uh we'll just keep v uh it's it's for now we're not using v for segmentation we're just using h and s but again read more about what hsb space is and you can choose the range even more precisely rather than the way i'm mentioning here okay i mean we are going to pick it in a very nice way don't get me wrong so let's with that information let's come back now let's say in fact let's not come back i want to segment the marbles that are in blue color in fact they are kind of around that colorish right this color so how do i pick those and the color actually changes the saturation a little bit in fact if you come back here if you look at the plot it's dark blue around the edges it's slightly different colors so let's come back here and say okay it's somewhere around here so i'm going to select my h from about 100 to 120 yeah i can go to 130 or 125ish but let's do 100 to ish and how about our saturation about somewhere around here above that it's kind of white so let's go around 100 to 255. you see the scale is going this way that's 100 that's 255. so if i go back i am going from 100 to 120 100 to 120 maybe i could have gone to 125 but let's leave it to 100 to 120. in the h space and in the s space i'm going from 90 to 255 yeah so that gives us the blue mask so let's go ahead and define that and we are applying that blue mask onto our hsb image right there so let's go ahead and apply it and let's check our mask how does it look like there you go so that's our original image and it's picking all the blue marbles pretty nicely except you see how you have like these reflection right there the tiny dots and that again this is where deep learning can really help right so if you train a whole bunch of images on images that look like this then that entire object would have been segmented using for example mask or cnn but now we're looking at simple ways easy ways of doing things so here we have like the objects kind of segmented except we have these little holes so we know how to fill those let's go ahead and use the binary closing operation which is like dilation followed by erosion and when you do that then some of these will be gone because when you dilate the pixels these get closed and when you erode then there is no i mean the closed ones are not going to be routed because they're already closed right so that function is available as part of scipy i'm going to use nd as part of scipy and again binary closing is available even also in opencv i believe and also psychic image but this one is readily available so let's go ahead and use it and i'm going to use a kernel of 7x7 you you need to use large enough so these are closed if you only use 3x3 then some of those are probably still left over so this is experimental based so let's go ahead and view and there you go all of those are gone if you actually do five by five again i'll let you experiment i don't want to bore you with uh little things like that but again go ahead and experiment but now these are all nicely segmented okay so now that they're segmented let's convert them into labels so where each object is given a unique label value so i can go ahead and extract the matrix so how do you do that again measure dot label that's exactly why i imported measure from psychic image so when i do measure dot label on the closed mask image right there now i should get a labeled image now let me go ahead and plot the labeled image you can see how each one of these is in a different color because each object here is a different label has a different label once you have a labeled image then you can easily extract metrics first of all let's go ahead and visualize it and again psychic image dot color has labeled to rgb so it converts your label image into rgb so let's go ahead and do that and now let's go ahead and plot the overlay and label to rgb label image and my image equals to image all this is doing is it looks at what your original image is and overlays the labeled image on top of that in rgb so that's exactly what that function does and now let's go ahead and plot it so you can see exactly how the result looks like so all the ones that are not segmented like are not relevant is in black and white and the ones that are actually blue marbles are in different colors each color represents unique object okay i don't want you to misinterpret these as different color marbles they're all the blue ones that we segmented now let's go ahead and report uh the object parameters what is the size what is the distribution so you know how to do that as part of psychic image measure we have region props and region props table this directly captures it into the table format and there are various metrics that you can capture i am only capturing the label number area diameter mean intensity and solidity solidity tells me how rounded the objects are if it is one then it is perfectly circle if it's close to one like point nine or something then it is circular and so on okay so let's go ahead and extract the properties and let's go ahead and capture that as part of our pandas data frame and print the first five rows so there you go so my labels are 1 2 3 4 5 and this is the area of each of these marbles and the mean intensity right there and the solidity is almost like 0.9697 right so that's almost rounded here you go a quick segmentation you don't need machine learning you don't need machine learning training and all that kind of stuff you know of course if you have enough images where you have a lot of these marbles then you can use mask or cnn and i'm going to cover exactly that topic stay tuned to this uh you know to the to my channel again depending on when you're watching it i may have already covered mass garcia and just search it on my channel and yeah good luck learning just hit the subscribe button and again hit the thanks button if you're feeling generous thank you guys
Info
Channel: DigitalSreeni
Views: 18,944
Rating: undefined out of 5
Keywords: microscopy, python, image processing
Id: 4hPl7GMnz5I
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Fri Aug 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.