Sunday, June 29, 2008

2nd half of 5300 course - Assignment #1 - Dark and Low contrast grey scale corrections...

For this assignment, we were required to take a 'dark image' and a 'low contrast image', and try to make them look a bit clearer using Photoshop and Octave.

I wanted to test the extremes of photoshop's image adjustments via the 'curves' function, and so I took a really dark picture by turning off the flash but adjusting the ISO to its highest setting. The subject in the picture was taken in a dark closet with the door closed.

Here is the resulting picture.



...No this is not a mistake...the picture pretty much looks like a black colour chip...

However, after applying the image/adjust/curves... function in Photoshop, the results are quite interesting. Here is the resulting image:



If you tilt your head to the left, you will see that it is a picture of my son Andre, sitting on his 'play chair', with his hands on his stomach.

Can you see this?

The neat thing about this transformation is that 'detail' that you would not normally see, can now be seen after applying the image/adjust/curves...function in Photoshop.

Here is how the histogram and 'curve' adjustment looked like in Photoshop.



Notice how the histogram shows that the 'image' mainly resides in the 'dark regions', meaning that the image is predominantly 'dark'. Therefore, inorder to make this image 'clearer', we must magnify the dark regions while suppressing the bright regions. This technique is often used in sharpening 'medical scans'.

Also note that the curve produced resembles a 'power law transform' or 'gamma law transform' curve, where gamma is less than 1.

To make this image look clearer by using octave, I employed the following Octave code:

A=imread("andre.jpg");
B=(A(:,:,1)+A(:,:,2)+A(:,:,3))/3;
imwrite("andregray.jpg",B);

By doing this I changed my 'colour' image into a gray scale image.

I then applied the Power-Law / Gamma Transform using the following Octave code:

A=imread("andregray.jpg");
G=mat2gray(c*double(A)/255^0.25);
H=255*G;
imwrite("gammandre0_25.jpg", double(H)/255);

Recall that the Power Law/Gamma Transform = S = c*r^gamma. Here I selected c=1 and r=0.25.

The resulting image is very close to what I came up with using photoshop.



Just for fun. I wanted to see how the 'log' transform could 'improve' a reflected flash image. Here is the image I was working with.



I then tried to apply a 'log' curve to this image, to see if I could reproduce the 'sunglasses effect' and generate increased detail obscured by the 'flash'.

Here is the improved image using Photoshop.



Notice how more detail is evident in the 'transformed' picture. That is, you can see more of the camera lens, the details of my fingers under the flash are more evident, and my son's nose and toes are not as washed out by the flash as before.

However, notice how certain 'shadows'/grey regions become even more darker using this transformation. i.e. see my left arm appears darker and loses its detail.

I tried to create a log transform using Octave but I was not very successful.

The codes I tried were:

a=imread("flashgrey.jpg");
g=mat2gray(5*log(1+(double(a)/255)));
g1=g*255;
imwrite("newlogandre5.jpg",double(g1)/255);

This code resulted in the following image transformation, which you will notice is not much different from the original flash image. The 'sunglass' effect did not get applied here. Recall that the log transform = s = c*log(1+r) and that the 'sunglasses effect' works for 'r' greater than or equal to zero and for bigger 'c'. I tried making c=10, 20, 50 but the images all pretty much looked the same.



I then tried 'shifting' the log function to the 'left' by 'adding 1'.

The code I then tried using was:

g=mat2gray(1+5*log(1+double(a)/255));
g1=g*255;
imwrite("nnewlogandre7.jpg",double(g1)/255);

The resulting image was not any better than the others I tried previously. Which leads me to believe that I am doing something wrong with my log Octave code. Will have to confirm with Prof. Hong Mei.

Here is the picture I got by adding '+1'. That is, the log function I used was s=1+c*log(1+r)



I tried to amend the code by using the following octave commands:

g=mat2gray(2*log(1+(double(a))));
imshow(g);

However, the image looked 'lighter' as opposed to 'darker' and showing even less detail. Pretty much looked the same as the pictures above but washed out a bit more.

I then tried running the following code from Prof. Zhu, thinking that I had to have all of these codes running in order for the programmes to run properly...however the images did not get any better or resemeble the image I cleared up using PhotoShop.

The code I used was:

% read images
im = imread("flashgreysml.jpg");

% calculate image size
imsz = size(im);
NoPixels = imsz(1, 1) * imsz(1, 2);
imagesize = NoPixels*8/8;

% histogram code...
function [pr, r] = im_nhist(im, bitdepth)

% calculate number of pixels
[M, N] = size(im);
NoPixels = M*N;

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

pr = pr/NoPixels;
end

% figure; bar(r, pr); axis tight;
% set(gca, 'xtick', 0:50:255)

% scale the image
f = im2double(im);
min_im = double(min(im(:)));
f = f - min_im;
f = f./max(f(:));
f = 255*f;
fsc = mat2gray(f);

% image negatives
g = mat2gray(2^8 - 1 - f);


% logarithmic transform;
c = 2;
g = mat2gray(c*log(1+f));
figure(1);
subplot(2, 2, 4), imshow(g);
title('logarithmic');

% power-law (Gamma) transform
c = 1;
gammas = [0.04, 0.2, 0.4, 1, 2.5, 10, 25];
for i = 1:length(gammas)
g = mat2gray(c*f.^gammas(i));
figure;
imshow(g);
axis off;
end;

However this code resulted in the following images (I selected the best ones...), which do not resemble the image I enhanced using photoshop:



I then finally tried the contrast stretching function but still did not get desired results...Here is the code and image I came up with.

% contrast-stretching transform;
figure(4);
m = 128;
i = 1;
for E = 0.1:5:10.1
g = 1/(1+(m/(double(im) + 0.0001))^E);
subplot(1, 3, i), imshow(mat2gray(g));
axis off;
i = i+1;
end



Will leave this assignment for now as I must work on my photomosaic and assignment #2.

1 comment:

Unknown said...

It is great that you have tried extreme cases. You did a good job in the first image, i.e., revealing your son in the almost black photo. For the 2nd image,the log or the other functions may not work well. This is because the big patch reflection of the flashlight takes value 255. As long as your gray value transform maps 255 to 255, you will always have that patch of white there. I tried to correct this photo by leveling, i.e., adjusting the min and max gray value range in the image. I get rid some of the flashlight and can see the other eye of Andre and part of the camera better.