Time for action – restoring your ancestors' photographs
First off, let me introduce you to my great-grandmother, whose picture will be used for restoration tasks. Of course, you can use your own ancestors' photographs. The following steps will help you restore your old photographs:
- Read and show the image in the usual manner, checking also its size:
>> ggm = imread('grandma.bmp'); >> imshow(ggm) >> size(ggm)
- Unfortunately, this image has two disadvantages; it is very large (2048 x 1536) and also it is in color (last dimension in size is equal to 3). So, let's try to make our lives a little easier for the time being, by resizing the image and transforming it to grayscale. Both steps can be done in the same command:
>> ggm_gray = rgb2gray(imresize(ggm,0.25));
- Now that we have a grayscale image sized 512 x 384, we can save it as
graygrandma.bmp
(just the first two inputs will suffice forimwrite
):>> imwrite(ggm_gray,'graygrandma.bmp');
An obvious flaw of this picture is the flash glare caused by our camera. This glow will lead to suboptimal results by our contrast enhancement methods. Till now, we haven't learned a way to remove such noise, however the thresholding techniques presented in this chapter can be used to isolate the flash. You could try verifying this on your own as a practice exercise.
- Just so we are ready to take advantage of this result in the following chapters, we will try to perform such an isolation. It is obvious that the image brightness in the glowing area caused by our flash has very high values. Let's try to segment this area using a high brightness threshold (for example,
t=220
). This process can be performed using the following command lines:>> subplot(1,2,1),imshow(ggm_gray),title(''Original image''); >> subplot(1,2,2),imshow(ggm_gray>220),title(''Thresholded image using t=220'');
The resulting image will be:
Obviously, the image area highlighted by the segmentation process is approximately equal to the glow produced by the flash. This result will prove useful in later chapters.
- For the time being, we should crop the image part above the flash and play with the contrast enhancement methods learned so far, to see which result we like more:
>> ggm_cr = imcrop(ggm_gray); >> subplot(2,2,1) >> imshow(ggm_cr) >> title('Original Image') >> subplot(2,2,2) >> imshow(ContrastEnhancement(ggm_cr,1)) >> title('Histeq result') >> subplot(2,2,3) >> imshow(ContrastEnhancement(ggm_cr,2)) >> title('Imadjust result') >> subplot(2,2,4) >> imshow(ContrastEnhancement(ggm_cr,3)) >> title('Adapthisteq result')
For the time being, we will have to settle with this artistic contrast enhancement. Of course, which result is more pleasing to the eye is a rather subjective matter, but probably most people would agree that the original image is a little flat, while the one produced by imadjust
looks a little more realistic, and the one produced by adapthisteq
looks more artistic. The image produced using histeq
has a rather disturbing high contrast and should be fine-tuned.
What just happened?
In this exercise, we have mixed some of the steps described both in this and the previous chapter in order to begin restoring an old picture, which has faded over time and been distorted by the flash of our camera. The restoration of such an image is a rather complex task, which will span in multiple chapters, but the first steps towards its completion were covered here. The image was transformed to grayscale and resized and then it was thresholded to pinpoint the area distorted by the flash glare. This area will be processed in the following chapters. Finally, the image was cropped to exclude the glare and its contrast was enhanced using the function we wrote in previous sections.
Pop quiz – contrasting enhancement methods
Q1. Which of the following facts are true?
- MATLAB will produce an error if you have matrices growing inside a loop.
- The histogram equalization method tends to result in evenly distributed histograms.
- The
Imadjust
function allows a specific percentage of pixel values to be saturated at low and high intensities. Adapthisteq
performs global histogram equalization.- Using
for loops
is the fastest way to threshold an image in MATLAB. - Using
im2bw
to perform thresholding suggests that the threshold is set in the range0
to1
. - An automated way to define the brightness threshold value for a grayscale image is by using
graythresh
.