Wednesday, July 3, 2013

Activity 6 Enhancement by Histogram Manipulation

First and foremost, I really, really had a hard time with this activity. I was messing up with the program left and right. Why? There were two parts that I had trouble with:

(Ignore my ranting here and skip to the introduction if you don't want to read this part)

1. The back-projecting of the pixels between the desired CDF and original CDF
2. Faster way of switching pixels in the image

Don't worry. This blog contains all the cheat codes so you won't spend the midnight oil like I did, so if you want some challenge DO NOT READ my blog... I'm just joking.

Introduction:

Images believe it or not have a distribution function. The pixels, specifically, make up the probability distribution function in which a histogram of the image can be created. Pixel colors range from 0 to 255. 0 being totally black and 255 being sharpest white. Between these two values, 254 shades of gray (no pun intended) can be technically observed.

Now let us talk about Histogram Manipulation!
Histogram manipulation is one way of improving the quality of an image, enhancing certain image features, or mimicking the responds of different systems such as the human eye.[1]

To this extent, one can take the probability density function (PDF) of an image by simply counting the number of times each pixel number ranging from 0-255 appeared in the image and plotting No. of pixels vs pixel (0-255). Then one can take the cumulative density function (CDF), which is essentially similar to the probability density function except for two things:

1. It's a cumulative, which means the pixel numbers are increasing.
2. The pixel number is normalized by dividing the y-axis value by the total number of pixel. This means the y-axis always ranges from 0 to 1.

This process can easily be done in scilab, since scilab allows easy conversion of images to a matrix of pixel numbers.
Another process which will be used is the back-projecting of a desired CDF to the orignal CDF of the image. It's similar to reshuffling the image pixels of your choosing by simply deciding what CDF to use.
This back-projection can be understood easily thanks to Mum Jing's figure shown below:




Figure 1 [1]

Now let's get started!
Since I recently watched Monsters University in theaters, I'm going to take the image of Michael "Mike" Wazowski, best friend of Sully. 



 Figure 2 [2]


 Figure 2

 

Figure 3: PDF (left) and CDF (right)

Note that the x-axis is Pixel from 0-255 and y-axis for the PDF is the occurrence of the specific pixel in the image and the y-axis for the CDF ranges from 0 to 1.
Now I'm going to make a CDF that is a straight line like this:
Figure 4

To do this we make use of the code that I tirelessly worked on:

(1)Image = imread('C:\Users\Phil\Desktop\MU.jpg');
(2)im = RGB2Gray(Image);
(3)imwrite(im,'MU.bmp');
(4)pixel = 255
(5) //CODE SNIPPET from: Mum Jing's blogspot of obtaining pixel color (0-255) from an image
(6)val=[];
(7)num=[];
(8)counter=1;
(9)for i=0:1:pixel 
(10)    [x,y]=find(im==i); //finds where im==i 
(11)   val(counter)=i; num(counter)=length(x); //find how many pixels of im have value of i 
(12)    counter=counter+1;
(13)end
(14)//End of Code Snippet
(15)[n,m] = size(im);
(16)nor = num/(n*m);
(17)mat1 = val';
(18)mat2 = nor';
(19)//plot(val, nor);
(20)mat3 = cumsum(mat2);//creates matrix containing both 0-255 and corresponding no.of pixels
(21)oldp = linspace(0,1,pixel+1); // creates a straight line y-axis value (desired CDF)
(22)//input1 = linspace(-10,10,pixel+1)
(23)//oldp = (1+exp(-1*input1))^-1
(24)newp = interp1(oldp, mat1, mat3,'nearest'); //
(25)newm = [mat1;mat3;mat1;oldp;newp];//0-255;0-1(currentCDF);0-255;0-1 (new CDF);0-255
(26)editim = im;
(27)// This part of the code backprojects/replaces the old pixels of the image to the new one.
(28)for i = 1:pixel  
(29)    editim(find(im==newm(1,i)))= newm(5,i); //
(30)end
(31)imwrite(editim,'EditedMU.bmp');
(32)[edity, editx] = imhist(editim);
(33)clf()
(34)plot(editx,edity/(n*m));
(35)sumedit = cumsum(edity/(n*m));
(36)clf();
(37)plot(editx,sumedit);


Tada!! Here is the resulting PDF:
Figure 5

And Figure 6 shows the resulting image. As you can see, there is a distinct difference between this image and the original. The contrast between white and black is greater. This can be attributed to the fact that linearity creates pixels counts which drop to zero (As seen from the PDF).
Therefore there is a loss of information...


Now let's try to create a sigmoid function, defined by:







Here is my poor attempt at trying to replicate the PDF curve of the sigmoid function using GIMP:


(Will still update. Discuss Code and Gimp.....)
References:
1. Soriano, Jing, Activity 6 Enhancement by Histogram Manipulation

No comments:

Post a Comment