Hi and welcome back to Section 19, where we take a look at histograms and K means clustering for the
dominant cause of an image, so open that notebook and scroll up to the top.
And I'll tell you about this lesson before we dive in.
So but firstly, let's just look at our images and the functions and libraries.
So what we're going to talk about is histograms and what a histogram is.
It's basically a graph, a bar chart.
If you if it looks similar to the bar chart, basically, but it doesn't have to be a bar chart that
can be a line graph as well.
And a histogram gives us basically the distribution of something.
All right.
So in the case of an images we're going to give, we're going to go through a distribution of the colors.
So the amount of colors that, oh, that sets in value, it's going to show.
So maybe, maybe a verbal explanation is not the best, but maybe even if we see it.
So let's go down and run this code.
So what we're going to do, we're going to load of input image.
We're going to show the original image here, which is this and then we're going to use the plotting
function from that bottle, which we imported right here to plot a histogram.
We use Bravo, Bravo.
This basically flattens our imagery.
So we just have it as big one big one dimensional array.
And then we just specify this is this is how much bins we want.
This is orange and we just do appeal to our show, which actually this should be in another line.
I don't actually like programming like that to Java or C++ habit.
And then what we do is separate the two channels here.
But this is just an array or set that's telling us is BGR so that when we enumerate over it, that means
looping through it.
Here we can just specify the labels here so we can just see code color, equal color, so we can actually
specify the color and types here, which is pretty cool to do.
And again, we set the limits here, and then we use the CB2 calc highest here to plot the individual
lines here.
So we're actually doing two histograms in this code.
We're doing one which just shows the overall image, the overall histogram, which is basically a brightness,
disrupt distribution.
So you can see this image is a fairly dark image.
So imagine now before I move down to the axis, imagine this the x axis had a bottom axis.
Imagine this was telling us that this was a range from zero to 255.
And it basically was telling us that if you see a spike in a line like this, it means there's a lot
of dark pixels or if it's a spike in and hence a lot of bright pixels.
So what do you anticipate this image will look like?
There it is.
You can see these are the brightness.
How much of each brightness was in the image, each pixel brightness.
So you can see a lot of the pixels in this image are basically under like 60 in terms of the brightness
and the intensity, almost a hundred and twenty thousand of them with this density here.
Remember, this is a fairly large image, so you can expect to have that that among the pixels.
This will probably close to a million pixels, so you can see this is the distribution of it here.
So what if we wanted to break this down by color?
That's what this look does.
That's what we did in this part of the code here.
So I may have committed this out just because I wasn't using it before, but it's just to show you that
we can use to caucus to actually get the histogram in the video histogram of an image I got.
You put the image into array just like this isalso array as well.
And then we specified range and we wanted to beans beans, basically meaning what spacing we want here.
So we're we're doing it to every pixel.
And this has a colored but so you can see, no, this image definitely does seem to have a more warm
ish tint to it.
That's why it is more red to it and blue.
And you can see this is probably the least amount of blue in terms of dark.
Blue is at least visible as more dark than the screen.
So this is a pretty cool way to analyze images.
So what what?
What do we do with this?
Well, one cool thing we can do with this before everyone.
This is just the parameters that go into calculus.
If you wanted to see it for yourself, we just kind of went through it, we believe before in code.
So what?
It's for your information, it's right here.
So back to this.
So what we're going to do, we're going to take another image before I move on to pretty cool exercise
and we're going to compare the histogram of this image to the other one.
You can see this one has a nice spike of end, which is the sky pixels most likely.
And you can see as a gradual, dark and darkening both in the beginning.
There's a lot of blacks, probably as well in image, which is why this little spike in beginning is
for.
And then again, it kind of goes smoothly down with a little spike it in just probably the fringes of
the mountains.
You can see the cold.
The situation is very different and you can see again because it's a warm looking image.
There's a lot of red in the image and spiking in the end.
Much more than blue is actually no blue spike in the end.
So it's an interesting way to analyze images.
So what if no, we wanted to break down this image into its individual color components.
Let's say we wanted to get the five most dominant colors out of this image.
Well, that's what we're going to do.
We're going to use K means clustering to do that.
No, I won't go into detail of what K means clustering is.
It's basically a clustering algorithm that groups pixels of similar value together.
That's essentially what it does.
So we have some helper functions here, which we'll go to just now.
But what we do, we input key means from Skillings cluster.
Hmm.
And then we lowered the image convey the image to RGV.
Then we reshape the image accordingly.
And the reason we reshape this image is because we do need it to be in a certain format for the CEOs
that function.
So we run, we create documents, clusters.
We're going to go five clusters here.
So we're going to get the five most dominant colors out of this image.
And then we run the K means clustering model on this.
So we just do Kielty dot fit as we created a seal.
The object, which is a key, means clustering object.
And then we pass the input to it, which is which has been reshaped into this format here.
If you do want to inspect what the ship is, you can.
And actually, let's do that right now so we can see the difference in shape so we can do image dot
shape and then we can do it again here to see what changes after it.
So let's print this.
So you can see this is the shape here, so what do you think happened?
Basically, what happened is that it flattened this axis here.
So instead of having to to a three dimensional image, it has a two dimensional image.
Yes, it flattened the axis here for each color components.
And that's why it's still tree the depth of tree.
So you can see we got an error here, and that's basically because we didn't run this block of code
before, which is the helper functions that we need to complete this exercise so we won't come back
to this.
You can see after we get after we get the silty object, and this year we pass it to the this century
algorithm function.
So what does that do?
Centroid algorithm creates a histogram of for the clusters based on the pixels of each cluster.
So after we do care clustering on it, we now need to actually grouped the clusters together.
So this is what this function does, and it returns the histogram right here.
Next, let's move on.
We do the plot colors.
This is the one that takes the output of this its highest, and it takes a census here and then just
plots bar graph where we actually do do a distribution again of, well, it's not really a bar graph,
it just a ball distribution like a bar shaped plot, and it gives us the distribution of the colors
here.
So you can see in this image here, you can see this probably look kind of a brown looking color.
Then there's some dark green.
There's some maybe some yellows, some light colors over here.
So let's take a look and see what ki-moon's did with the colors.
So let's run this code now, and you can see this good will take some time to run because it has to
perform the ki-moon's clustering on roughly.
This is about a million two million pixels, so it does take some time to run.
OK, so there we go, and now we can see the dominant colors here.
You can see there's a sky color as the dominant color.
This is olive green color, which is probably the average color that's of these top mountains here.
Then this dark kind of mostly looking green, which is probably to set the dark horse on the Valkyries.
Here, then, is a light olive green, which is probably this area here.
And then there's a brown, which is probably these areas here, as well as some of the flowers here.
So it's a really cool way to do this so we can now break up images into color components like that.
And if you wanted, you can do more clusters in this, which would be interesting as well.
So now let's run off in a different image.
This is a picture of me playing beach volleyball in Barbados, actually with some friends.
So you can see this.
This mystic is the code where I renamed the functions to Camel Case, and I did not update that properly,
so we'll fix that.
And now we run this, and it should run faster because this is a much smaller image.
There we go.
So you can see this make sense.
Is this blue for the water?
There's black, which is my hair and beard, as well as a bit of the background, perhaps.
And then this is brown, which is matching their skin tone.
So this is a pretty nifty and pretty cool.
So that concludes this lesson.
Oh, good.
Now, move on to the next lesson, which is comparing images so we can actually see the differences
between images.
Let's see how much to how much they change.
And then take a look at structural similarity of images.
So I'll see you in the next lesson.
Thank you.