scikit-image: segmentation and regionprops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi my name is stephane faroult I'm a researcher at UC Berkeley at the Berkeley Institute for data science and the founder of psychedelic and here I'd like to show you how to segment a photograph of coins using psychic disorder shed algorithm along with region properties we start by importing psychic image and checking the version I'll be working with version zero 13 we then import numpy using the standard convention numpy as NP matplotlib inline ensures that the plots that we make appear inside of the Jupiter notebook and here we implode import MATLAB lips pipe lot API as PLT so let's start by getting hold of some test data so from SK image we import the data module and inside of the data sub module we call the function coins which loads an image from disk remember to execute that cell and let's have a look at this image okay so there's our test data and what I'd like to do in this exercise is isolate the coins from the darker background so before I start I'm going to smooth the image out a little bit denoise it so from psychic image I import the filter sub module and I apply a median filter of size 5 by 5 on on that image so let's visualize that here we go so you can see that the first image has a lot of detail and after applying the structuring element that's what selim stands for of 5 by 5 and during a median filter the image is a lot smoother so that should get rid of most of the noise for us now our next step is to try and find the edges of the coins so for that we're going to be using the canny edge detector that lives inside of psychic images feature sub-module so from Eska image import feature and call feature dot Kenny and the Sigma parameter here adjusts the the width of the Kenny or how sensitive it is to edges so we can play around with that parameter a bit but you can see that we get fairly good edges of those coins out if we make Sigma equal to one we get more noise more noisy image more edges out if we set it equal to five fewer and then we see that we don't quite capture the outlines of most of the coins so three is about a good value there still I have some gaps over here in some of the coins but that should be okay alright so we're going to be using a watershed algorithm which means that we're going to imagine that this edge image here is like a landscape and we're going to fill that landscape with water and so we're going to place try and place fountains inside of each coin and then we're going to switch on those fountains and let them pour water into this image landscape and then where the water levels join that will be the new regions so in order to do that we need to convert this edge image into a landscape and for that we use the distance transform as implemented in Syfy's in the image module so this might be a good place to comment on interaction between sci-fi and cycle image most of the libraries inside of the sci-fi ecosystem work with one another through numpy arrays and that standard API allows us to reuse site by here as well without a problem all right so let's call the distance transform this is the Euclidean distance transform hence the EDT and we call it on the negative of the edges so the distance transform is defined as the distance to the closest background pixel so let's just apply it to the edges as we have it here and you see that we get the same image back so what's happening here well it's telling us that the background is a distance of zero away from the background itself but any edge pixel is a distance of 1 away from the background so that makes sense if we applied to the inverse of the edge mask so we make the foreground background and the background foreground now we have a much more interesting result so this tells us this map that we see here tells us how far are we away from a coin edge and you can see that in the middle of the coin were fairly far away and outside of the coin were fairly far away but on the edge of the coin itself were very close to the edge of that coin okay so this will be the landscape that we will now fill up using watershed so in order to do that we first need to find the locations of those fountains so we're going to be using the peak local max function from the features sub module and that will find Peaks local peaks in this landscape for us so let's execute that and visualize the peaks not sure if you can see but there's black image that you see here has got peaks inside of it those were the peaks detected we can probably visualize them better by looking at the actual positions and plotting those positions so for that we're going to be calling peak local max again but we're going to be setting the indices flag to true which means that it will return the positions of the peaks instead of the peak mask visual is debuff so you can see if we execute that I'm just printing the first five but you can see these are the coordinates of the Peaks coming back so let's plot that in and matplotlib okay we're only looking at five of them here we want to be looking it's left the colon out there so we want to be looking at all the row corn all the x-coordinates and all the y-coordinates so the peaks are returned in row column format so each entry here is a row and a column coordinate mat Kotlik lib takes x and y values so we take the second column and the first column the x and y values let's see what's going on here x y must have the same dimensions we want the first column second column and in the first column there we go alright so there you see all the peaks that we just found visualized so this will be the positions of the fountains that we will use to flood this landscape so next we're going to be labeling each of these features we're going to give them each individual number we're going to be starting with zero and counting upward this number is used by the watershed to to label the region that forms around that little fountain so each point gets a new integer label and then we can call our watershed function we're going to be calling it on the negative of the distance map let's scroll back to that quickly so we're going to be inverting this map so that the low areas are high and the higher is alone in other words the center of the coin will become a trough and the ring around the the edge of the coin will become a beak that's so that this landscape can fill up with the water spouting from the center of each coin okay so let's execute that watershed on our distance map and with the given labeled markers okay so this is the result we visualized that using the mark boundaries function from the segmentation sub module so that just draws lines around each each area that was found each area of the segmentation we can also use the label to RGB function found inside of the color sub module and that gives us a slightly different visualization of the labeling all right so we can see that the the background here definitely well separated from the different coins some of the coins got split in half like this one over here and this one over here maybe that one over there as well yeah and then the background itself is separated into various regions so now the question becomes how can we merge the appropriate regions together so let's let's do another visualization of the segmentation we've done we're going to be calling label to RGB again as we did before but we're going to be asking it to instead of using colors to represent areas to average the pixel values underlying those areas and to display those so when we do that we see that the coins as we've observed before are lighter than the background in general so we should be able to to split foreground and background just by looking their intensities okay so to do that let's first take a look at all the different regions identified here so for that we're going to be using region props those will be calculated for each object that you see in here so that object will have a region property a set of region properties calculated so we'll each coin that you see here and for the coins that have been segmented into multiple parts will have a region a set of reading properties for each so let's look quickly at region props giving indication of the kinds of attributes that we can compute for each region so you can compute things like area the bounding box the bounding box area the centroid the coordinates covered by that that object and and quite a few others so you can look at measured region props at the dock string for that function to learn more about those properties so let's compute them and they get stored in a list list of region properties and now we're going to be computing the mean intensity for each of these regions and draw up a histogram so you see that there is a very clear separation here these are objects the mean intensities of objects associated with a background and here we have the main intensities of objects associated with coins so if we split them here around the 110 line we should have a pretty good segmentation so that's how you would do it by hand I think you know often we we want these processes to be automated so instead I'm going to be using psych and here and I'm going to be doing the stirring of the background and the foreground intensities and then to be to ask so I could learn to do that to make that decision for us on what is foreground and background so from psychic learns cluster sub-module we import k-means and we tell k-means were interested in learning about two clusters the I send in the region means as I convert those into an array and scikit-learn expects them to be in a column format so let's just let's split that cell over there it's executed region means so you can see that I've reshaped it into one long column that's what scikit-learn expects alright so now we have our k-means with two clusters and we fit that to the data here there's I can just reuse that previous variable so I'm going to fit it to the means and then I'm going to be to display the the centroids found for the two clauses and for the two clusters and there you see that it found two clusters one cluster centered around 160 and the other cluster centered here around 58 so that's our foreground and background and now I can use I can ask the model to predict label for each of my regions and that gives me the label either foreground or background now all that remains is to label the image appropriately so we're going to be copying our original labels and then we're going to take the combination of our predicted labels with the different regions and we're going to real able the image according to the coordinates of each region we're going to assign that too whether its background or foreground this cell is quite dense so it might be worth just looking at it for a few seconds so we execute and weary display again with label to RGB now on our newly classified labels and here we go the foreground perfectly isolated from the background alright so that's it for now I look forward to telling you more about cycle image in the near future thank you for listening [Music]
Info
Channel: Stéfan van der Walt
Views: 19,136
Rating: undefined out of 5
Keywords: python, scikit-image
Id: STnoJ3YCWus
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Thu Sep 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.