Act 5. Enhancement by Histogram Manipulation
I’ve owned a digital camera for quite a while now and since then I have always wondered what the histogram option is for. I never really tried to understand it nor research about it, all I know was that it displays a graph and since it is named histogram it must be a graph of frequency of something. And then there was the third activity, my first encounter with histogram for image processing. From A3 – Image Types and Formats by Dr. Soriano it was stated that ” the histogram of a grayscale image is a plot which represents the number of pixels in an image having a certain grayscale value”. In other words, it is a graph that shows how many times a certain color value appeared in the whole image. For this activity, we will be using Scilab to enhance a grayscale image by manipulating its histogram.
Before we go on with the image manipulation, let me define some terms we will be dealing with for this activity:
- Histogram – already given above
- Probability Distribution Function (PDF) – this is the histogram divided by the number of pixels in an image
- Cumulative Distribution Function (CDF) – the cumulative sum or integral of the PDF
And so we begin with the enhancement by histogram manipulation. The first task is to choose an image to be used preferably something dark. I chose an image I used for the 3rd activity although it is not a particularly problematic image it would do just fine.
I cropped the original image and made it smaller to save some time processing it since my laptop is not so young anymore.
In our previous activities, I used the command histplot() to obtain a histogram of the image however, histplot only shows the histogram and does not return its actual values. The command tabul() is a good alternative, unlike the first command this returns a tabulation of frequencies so plotting it afterwards will be necessary if you need/want to see how the histogram looks. Note that these values still refers to the histogram, to obtain the PDF we must first determine the total number of pixels in the image. Since we have a rectangular image, getting this value can be easily done by getting number of pixels in one row and one column and multiplying them. I did this by using the command size(). We then divide the histogram with this value to get the PDF, cumsum() was then applied to get the CDF.
To be able to enhance our image we generate a new CDF as we desire, say for example a linear CDF. We use backprojection to see the equivalent image of our new CDF this is done by:
- first getting the gray scale value of a pixel then finding its CDF value
- trace the CDF value in the y-axis of the desired CDF graph
- obtain the equivalent gray scale value ( in the x-axis) in the new CDF graph and use it in place of the old one
- do this for all pixels
The results I obtained were as follows:
We could see that the original image is not so different from the 2nd one which uses the function ‘y=x’ this means that the original CDF is approximately linear. The third and fourth CDF’s (logx and x^3 respectively) show extreme effect to the image one being too bright while the other too dark. The fifth one is quite ok butI think the linear CDF is still better.
However, the enhancement above is not really categorically an ‘enhancement’ since the original image is better (or almost the same) with the processed images. It was actually more of an exploration of the technique presented in this activity so here is another application of the histogram manipulation technique, this time with an old picture of mine together with a couple of friends from highschool.
See the ‘enhancement’ now? I used ‘y=x^.7’ for my desired CDF.
If you are not into programming, you could also use software for editing images such as the well known Adobe Photoshop and the free software Gimp.
—
Scilab codes:
stacksize(10000000);
image= gray_imread(‘D:\files\kaye\186\grayscale2.jpg’);
[imx,imy] = size(image);
scf(0), subplot(1,2,1), imshow(image);
histogram = tabul(image,’i’);
s=imx*imy;
PDF = histogram/s;
scf(1), subplot(2,2,1), plot(PDF); ;
cdf=cumsum(PDF);
CDF=cdf/max(cdf);
subplot(2,2,2), plot(CDF(:,1), CDF(:,2));
x=[]
y=[]
for i= 1:length(CDF(:,1));
x(i)=i;
y(i)=i^.7; // vary for other CDF results
end
y=y/max(y);
CDF2=[x,y];
scf(1),subplot(1,2,1), plot(x,y);
adjusted = [];
for i=1:imx
for j=1:imy
a= find(histogram(:,1)==image(i,j));
adjusted(i,j)= y(a);
end
end
[adjx,adjy]=size(adjusted);
s_adj=adjx*adjy;
PDF2=(tabul(adjusted, ‘i’))/s_adj;
subplot(1,2,2), plot(PDF2(:,1),PDF2(:,2));
scf(0),subplot(1,2,2), imshow(adjusted);
—
So there, next time you take a picture and you find it too dark or too bright or just too boring try playing around with the histogram. Enjoy 😀
I would like to thank Gladys, Tisza, and Rob for their insights and tips.
I give myself a grade of 9 for this activity since I have completed the requirements but failed to submit on time.
Sources:
[1] Dr. Maricor Soriano. “A4 – Enhancement by Histogram Manipulation”, 2010
[2] Dr. Maricor Soriano. “A3 – Image Types and Formats”, 2010
[3]http://www.pixelperfectdigital.com/free_stock_photos/data/509/medium/grayscale_flower.jpg