Time for action – using imtool to pinpoint differences
Now, it is time to combine the function written in the previous section with what you learned about imtool
in the previous chapter, in order to pinpoint the areas of the image where adapthisteq
provides superior results. The following steps will help you combine the functions:
- First, let's load the image we will be using:
>> img = imread('holiday_image2.bmp');
- Then you should enhance the image using the two versions of histogram equalization:
>> img_he = ContrastEnhancement(img,1); >> img_ahe = ContrastEnhancement(img,3);
- Now, let's use
imtool
for the first and second result:>> imtool(img_he) >> imtool(img_ahe)
First, we zoom in using the magnifying glass icon with the plus sign. Once selected, we use the mouse to select the parts in the two images where
adapthisteq
enhances details more (we can enter the same zoom factor): - Then, we zoom out and zoom in on again the left area of the image so that we pinpoint the shadowed areas' enhancement:
What just happened?
This time we combined knowledge acquired in this chapter, with what we learned in the previous one. First, we followed the steps shown earlier to perform two different kinds of enhancement on our image. Then, we used imtool
to zoom into the areas where the differences between the two chosen methods are more apparent. The results strengthens our previous view, which was the CLAHE method preferable to all other methods presented in this chapter, as it provides better results both in very bright and very dark areas.
Have a go hero – writing a function to enhance an image area
Now it's time we dive into deeper waters. Let's say you want to create a function that enhances a certain area of the image, using any of the methods above. This task will need you to combine several pieces of knowledge acquired so far and also use some settings we haven't used so far.
To get you started, here is the rationale you have to follow: first, you have to find a way to cut a specific part of the image that you want to enhance. Then, you will use one of the enhancement methods on the cropped area, and finally you will have to reattach the area in its original position.
In the beginning of the function, you should crop a rectangle area of the input image while knowing the coordinates of the rectangle. Using the help imcrop
command, you can find the following way to call the function:
[I2 RECT] = imcrop(...) returns the cropping rectangle in addition to the cropped image.
This description suggests that you can use imcrop
with two outputs; the cropped image I2
and the cropped rectangle coordinates in RECT
. This way, you can then use matrix RECT
to replace the selected rectangular area of the original image with the enhanced cropped image.
Let's say that your function will be named CroppedContrastEnhancement.m
. We'll start you off with its definition, and input/output description, and you can do the rest:
function [output] = CroppedContrastEnhancement(input,method) % Function that performs area-based image contrast enhancement with % methodsincorporated in MATLAB toolboxes % Inputs: % input - Input image % method - Enhancement method (1: histeq, 2: imadjust, % 3: adapthisteq) % Output: % output - Output image (with enhanced contrast)
To see if your function works, you could run it for our holiday image and select an area to perform histogram equalization on:
>> img = imread('holiday_image2.bmp'); >> img2=CroppedContrastEnhancement(img,1);
Then, you have to define the area you want to enhance:
Having selected the area you should be able to double-click on it to generate the result. The resulting image will be as follows:
>> imshow(img2);