Thursday, July 3, 2008

Assignment #2 - historgram equalization...

Here is my original picture.

a=imread("gdarkim.jpg");
A=double(a)/255;
imshow(im);



I chose this image because it is predominantly a dark picture with little observable detail in its present state. It was taken inside a moving car so you can see a slight blur and some smudges on the windshield. What I want to do is to produce a 'histogram equalization transform' for this picture to enhance it. Here are the steps to do it.


The histogram of the original image looks like this.



I used the following octave code to obtain it:

hist(b(:));

Next I need to normalize the histogram by using the octave code:

b=hist(A(:),256,1);
plot(b);

[Prof Zhu, I am not sure if I am normalizing the histogram properly here...I have a note that you wrote down for me in my notebook and I wasn't sure if this was a normalizing function in octave or if this simply plots the histogram of the original image.]

[I also tried using your matlab code for the im_nhist process but kept getting error messages in octave:

octave:171> function [pr, r] = im_nhist(im, bitdepth)
>
> M=375;
> N=500;
>
> r = 0:2^bitdepth-1;
> pr = zeros(size(r));
> for y = 1:M
> for x = 1:N
> rxy = im(y, x);
> pr(rxy+1) = pr(rxy+1) + 1;
> end
> end
> end
octave:172>
octave:172>
octave:172> % calculate the normalized histogram of the image
octave:172> [pr, r] = im_nhist(im, 8);
error: expecting integer index, found 1.517647
error: evaluating binary operator `+' near line 11, column 31
error: evaluating assignment expression near line 11, column 19
error: evaluating for command near line 9, column 5
error: evaluating for command near line 8, column 1
error: called from `im_nhist'
]

The normalized histogram using "b=hist(A(:),256,1);" as the normalizing process looks like this:



Next, I have to calculate the normalized histogram of the image by using:

r=0:255;
s = zeros(size(r));
for k = 1:256;
s(k) = round((2^8-1)*sum(b(1:k)));
end;
C= zeros(size(A));
for y = 1:375
for x = 1:500
C(y, x) = s(A(y, x)+1);
end;
end;

imshow(double(C)/255)

The image I get is:



I notice a couple of things. First of all it is 'lighter' i.e. it is not as dark as the original image. However, it is very grainy and not very clear or detailed. I'm not sure if this is just a natural side-effect of the histogram equalization with a bright background and dark foreground or if I am doing something wrong here.

If I did do something wrong then I am sure it has to do something with the normalization process...which I wasn't entirely sure if I was doing correctly in the first place.

I can't seem to let the 'flash' picture of my son go...I tried to normalize the histogram of this image to see what I could get.

Recall that the original image looks like this:



After applying the histogram equalization code I get the following image:




Notice how the flash washes out more of the image? This makes sense as this process tends to enhance the 'lighter' parts of the image and the darker parts.

The original and then normalized histograms look like this:




After these 2 experiments I believe that histogram equalization will not work well for images with a big difference between the dark and light regions, as in the 2 examples I used. I think that if I use a 'darkish' image with some light regions to it, the equalization process will not look grainy or washed out.

Here is the new picture I used and applied the histogram equalization to. Notice how the picture is no longer grainy and enhances the light and dark regions enough to make the picture crisper and with more detail.



The first image is the original and the second image is the 'fixed version'. Notice how the 2nd image does not look as 'drab' as the first image and that the stones on the shore appear to 'pop' out more with enhanced detail. Also, the sky in the 'fixed version' does not look as monotone as the original. There is a hint of the sun observed in the sky as well which further enhances the image.

Therefore, I believe if you try to histogram equalize an image that has a dark foreground and a bright background, it will yield an image that will be lighter but grainy i.e. not clear. Thus, if you want to select a picture that will benefit from histogram equalization, it must not have too much of a major difference between the dark and light regions, as was the case in my 'water' image used.

1 comment:

Unknown said...

First of all, I like your explaination on how your photos are taken.

The error massage that you got from im_nhist is because the gray values of your image are floating point numbers. They are scaled to [0, 1] which don't correspond to valid index number.

Can you try to think more why an image with many pixels of dark or white won't be good for histogram equalization?