Wednesday, December 7, 2016

Eeeevil Segmentation

$HEYYYYY \, I'M \, BACK$
It's been a while since the last post.
Too... lazy... to... blog...

Now we're gonna do IMAGE SEGMENTATION (dun dun dun)
What's that?
I'm sure you have an idea what it is. But for the sake of being complete I will insult your intelligence a bit.
Image segmentation is the process of isolating a part of an image that contains information that we want, or in general just being interesting so to speak. Let's call that part the Region Of Interest (ROI) and the rest of the image becomes the background.

When the image is in grayscale (that's black and white you pleb), it is quite easy to separate certain features.
Take a look at this cheque for example. Yes CHEQUE. It looks sexier than it's Murrican spelling.


Now I want the grey background to be gone and only take the texty stuff.
What to do? BAM! THRESHOLDING.
I simply keep the black parts and make all "non black" pixels white.
Just like that? Yeahp. $EZ$

Here is how that cheque looks like in different thresholds from 50-250 by intervals of 25.
I know that you know by now that I like animated GIFs. And you probably do to. So here.
$Shablam$

Now let's be a bit more scientific than trial and error. 
dude TaE method is so scientific
Epepep. Shhhh shhhh. Let $SCIENCE$ happen. 

So we now take the histogram of the pixel values of the image.

$Histograaaaahm$

Well see that peak in the histogram? That's the background. $I \, think$. So basically everything above, say 160 pixel value is part of the background.

THRESHOOOOLD by 160 pixel value
$AMAZEBALSS$

Looks good brah. How'd you know to pick 160?
Trial and Error....
uhuh... what happened to "SCIENCE"?
Shut up.



Now what if it's colored?
Well we have two methods for that. The $PARAMETRIC$ and $NON-PARAMETRIC$ methods.
We'll get to that later.

Imagine SpongeBob. You probably imagined Patrick too right? Yeah because they are basically inseparable.

You and me forever in the underwater sun. Underwater sun~
But suppose that we really want to separate SpongeBob and Patrick.
Because we are
                        Image result for mermaid man evil

So how do we do it?
Well first we have to understand the $rg \, CHROMATICITY \, SPACE$
This is also known as the normalized chromaticity coordinates (NCC), because we are normalizing the Red,Green, and Blue channels.

Here we separate the three channels of our colored image, R, G, and B.
Now we normalize them
$I = R+G+B$
$r = \frac{R}{I} \qquad g = \frac{G}{I} \qquad i=\frac{B}{I}$

The advantage of this is that we can now represent colors in just two dimensions $r$ and $g$.
The blue channel can easily be reproduced from the other two
$b = 1-r-g$

Here's how that chromaticity space looks like
 

Okay now what?
First we take sample of region of interest (ROI). Say Patrick's skin

 No no don't skin Patrick alive that's



Image result for mermaid man evil











I'm discussing here...


Anyway. From Patrick's skin sample we can now segment our image using two methods.
The first method is called parametric.
Why?
Because we assume a certain distribution of colors, say a Gaussian (Normal) distribution. $p(x) = \frac{1}{\sigma \sqrt{2\pi}} exp [-\frac{(x-\mu)^2}{2 \sigma^2}]$
Then we extract Parameters $\sigma$ and $\mu$ from our ROI sample.
 Actually we need two sets of parameters.
From the normalized red channel: $\sigma_r$, $\mu_r$
From the normalized green channel: $\sigma_g$, $\mu_g$
We use the extracted parameters to obtain the probabilities $p(r)$ and $p(g)$.
Now we multiply the two probabilities and call it $p(r,g)$.
This new probability tells us how likely a pixel in our image belongs to the same group as the ROI sample.

Here is what that combined probability looks like

After thresholding

Returning the color

Well we got a little bit of SpongeBob's tongue there. Can't be helped they're both pink
Hey I want a GIF.
Fine...

Soo about that other method?
Yes yes coming.
In the non parametric method, instead of assuming a distribution, we get the actual 2D histogram (pdf) of the ROI sample. Here's how that 2D histogram would look like.



In the rg color space it's gonna look like this


Bro that color space looks upside down
Yeah well that's just how Scilab plots.
Maybe in the future I'll try to fix that.
But for now $DEAL \, WITH \, IT$

So basically for whatever color exists in that histogram is part of our Region of Interest. So we go back to our image display only the colors that match our histogram.

Well I've taken so much space so here's your GIF

$SPLAKATOOM$

 Well compared to the parametric method, this one is a bit rough. The good thing is that it preserves a bit more features like Patrick's belly button, and SpongeBobs (tongue) cleavage.




Hehey we're done.
That's cool and all.
I'm not really aiming high here.
Cramming's bad mkay~

But I can say Mission Accomplished!
So I'm giving myself a full $10/10$ for this one.


Special thanks to:
Ms. Tisza Tronos' blog  for helping me work my problems in the non parametric method
Louie Rubio for pointing out that Scilab's windows can be merged like in Spyder wich makes me respect Scilab a bit more. And also for pointing out Ms. Trono's blog

No comments:

Post a Comment