HW2 Image Processing
HW2 Image Processing
HW2 Image Processing
Assignment #2
Contrast Enhancement!
1. In many vision applications, it is useful to be able to separate out the regions of the image
corresponding to objects in which we are interested, from the regions of the image that correspond to
background. Thresholding often provides an easy and convenient way to perform this segmentation.
a. Load the image wdg1.jpg and depict its 256-bin histogram. What can you infer from the
provided image and its histogram?
b. Based on the histogram, choose an appropriate threshold value and convert the image to an
binary image. Name the resultant image as wdg_bin.bmp and write it into the output directory.
Explain your observations.
c. Load wdg2.jpg and depict its 256-bin histogram. Whats the problem with this image and its
histogram? Can we use the simple thresholding scheme to segregate foreground and background?
Why?
Like global thresholding, adaptive thresholding is used to separate desirable foreground image objects
from the background based on the difference in pixel intensities of each region. Local adaptive
thresholding selects an individual threshold for each pixel based on the range of intensity values in its
local neighborhood.
d. Load the image son.jpg and depict its 256-bin histogram. Choose an arbitrary threshold value and
binarize the image. Show the result and discuss why this method fails.
e. Write a program to apply the mean of 7x7 neighboring pixel values as the threshold value for each
pixel. Name the result as son_mean.bmp and write it into the output directory. Discuss the result.
f. The result can be improved if mean-C (mean minus C) is employed, where C is a constant. Binarize
the image with 7x7 neighborhood and set C=7. Name the result as son_Cmean.bmp and write it into
the output directory. Discuss your observations.
g. Instead of the mean-c, use median-c statistic. Set the neighborhood size as 7x7 and C=4. Name the
results as son_median.bmp and write it into the output directory.
Mean-C
2. Contrast stretching is a simple image enhancement technique that attempts to improve the contrast
in an image by stretching the range of intensity values it contains to span a desired range of values.
a. Load the image wom.jpg and depict its 256-bin histogram. As you know, the contrast of the
provided image is weak. Justify this fact according to the form of the histogram.
b. The simplest form of contrast stretching can be applied by using the following expression to
calculate the new pixel value of each pixel:
b a
pout pin c
a
dc
where a and b are the lower and upper limits of the image (say 0 and 255). In addition, c and d are
the lowest and highest pixel values currently present in the image. p out and pin are the output and
input pixel values for any element. Values below 0 are set to 0 and values about 255 are set to 255.
Write a program to apply the preceding normalization expression on each pixel. Depict the
histogram of the resultant image and discuss your observations. Compare the histograms of the
image before and after operation. Name the resultant image as wom_strech.bmp and write it into
the output directory. You are not allowed to use MATLAB built-in functions for contrast stretching.
c. What is the probable problem with setting c and d equal to the lowest and highest pixel values of
the image? (Hint: outlier). To overcome this issue, scrutinize the image histogram and select different
values for c and d. Re-execute the program with new values and include the best result in your report
(Hint: the 5th and 95th percentile in the histogram may be good choices ).
d. Imagine you have an image taken in low light levels and which, as a result, has low contrast. What
are the advantages of using contrast stretching to improve the contrast, rather than simply scaling
the image by a factor of, say, three?
Contrast Stretching
The dynamic range of an image can be compressed by replacing each pixel value with its logarithm.
This has the effect that low intensity pixel values are enhanced.
e. Load the image man.jpg and depict its 256-histogram. What can you infer from the raw image
and its histogram?
f. Apply logarithm operator to the image and depict the histogram of the resultant image. Name the
output as man_log.bmp and write it into the output directory. Discuss your observations. Does the
operator enhance the image? Why?
g. Load the image svs.jpg and apply the logarithm operator upon it. Include the results and its
histogram in your report. Does it improve the image contrast? Why?
Logarithm Operator
Logarithm Operator
3. Equalize the histogram of the 8 8 image below. You should not use MATLAB, instead calculate it by
hand. The image has grey levels 0, 1,, 7. You can either type your solution or scan your handwritten
solution. Include all the arithmetic details in your solution.
4. Given a 256 256 pixels image with eight gray levels, whose gray-level distribution is given in the
following table:
It is desired that the original histogram is changed to approach the histogram corresponding to the table
below. Answer the following questions. You can submit the typed or scanned version of your solution.
5. Suppose we are given an image with pixel intensities in the interval [0, 1]. The values of pixels in the
image, r, are characterized by a probability density function (pdf), pr(r) = 2r for r in [0, 1], and pr(r) = 0,
otherwise. Our task is to transform the intensity levels of this image, so the pixel values in the new image,
z, are characterized by another pdf, pz(z) = 3z2 for z in [0, 1], and pz(z) = 0, otherwise. The plots of pr(r) and
pz(z) are shown in Figure 6. Assuming that pixel values can take continuous real values in [0, 1], find this
transformation in terms of input r and output z values.
Figure 6.
6. Histogram modeling techniques (e.g. histogram equalization) provide a sophisticated method for
modifying the dynamic range and contrast of an image by altering that image such that its intensity
histogram has a desired shape.
a. Load the image moon.jpg and depict its 256-histogram. What can you infer from the image and
its histogram?
b. Write a program to apply histogram equalization technique on the image. You are not allowed to
use MATLAB built-in function for histogram equalization. Compare the histograms before and after
operation. Name the resultant image as moon_equal.bmp and write it into the output directory.
Discuss your observations.
c. Load the images pigeon.jpg and soldier.jpg. Depict their 256-bin histogram and include them
in your report. Write a program to make the histogram of pigeon.jpg similar to that of soldier.jpg.
You are now allowed to use MATLAB built-in function for histogram matching. Include the histograms
of the images after the operation and compare their corresponding grayscale representations. Name
the resultant image as pigeon_matching.bmp and write it into the output directory.
Histogram Equalization
Histogram Matching
7. A modification of histogram equalization called the Adaptive Histogram Equalization can be used on
the images for which the conventional histogram equalization method poorly works. Adaptive histogram
equalization works by considering only small regions (defined by the neighborhood window size) and
based on their local cdf, performs contrast enhancement of the central pixel using the same method as
conventional (Global) Histogram Equalization for the central pixel. In other words, we scan the image
with a window inside which we modify the histogram but we alter only the value of the central pixel.
Clearly, such a method is costly and various algorithms have been devised to make it more efficient.
a. Implement Adaptive Histogram Equalization and verify your program on boy.jpg. Include image
histogram before and after operation in your report. Set the window size equal to 15x15. Name the
resultant image as boy_AHL_15.bmp and write it into the output directory. You are not allowed to
use MATLAB built-in function for adaptive histogram equalization.
b. Repeat part a for window sizes: 5x5, 41x41, and 81x81. Include the resultant images and their
histograms in your report. Discuss your observations.
Adaptive Histogram
Equalization
Window Size 81x81
8. In this problem you are supposed to solve some filtering task by your hand. You are not allowed to use
MATLAB or any other programming software. You can either type your solution or scan it.
a. Spatially filter (convolve) the image on the left with the 3 x 3 mask (kernel) shown. State the
assumptions you make for the pixels outside the source image.
b. What is the result of mean filtering (averaging pixels with their 8-connected neighbors) for the
following image?
c. What is the result of median filtering (using 8-connected neighbors) for the following image?
9. Load the images car.jpg and wheel.jpg. As you see, the car has several instance of wheel. Write
a program for detecting the object in the given image. Detection can be implemented by computing
the correlation between the template and the image. The image location that have the maximum
correlation value can be interpreted as object detections. Visualize the correlation results and the
locations where the object is detected (generating a binary map is a good idea in which the object are
set to one and zero elsewhere). Note that the correlation is the same as convolution except that it does
not rotate the template before element-by-element arithmetic operations.
Figure 9.
10. Apply the following filters on the image snake.jpg. Name the resultant images as snake_a.jpg to
snake_p.jpg and write them into the output directory. Explain and justify the effect of each filter
according to its result. You can use built-in MATLAB functions in this problem.
19
19
19
19
19
19
118
118
118
118
118
118
17
17
17
17
17
17
19
19
19
118
118
118
17
17
17
(a)
(b)
(c)
18
(d)
(e)
(f)
(g)
(h)
1
1
1
1
8
1
(i)
-1
-1
(k)
(l)
(m)
13
0
0
13
0
0
13
(n)
(j)
(o)
(p)
11. Load the image skeleton.jpg. In this problem you have to sharpen the provided image and bring
out more skeletal detail. There are two problems with the provided image: first, the dynamic range of the
image is too narrow and second, high noise content makes the image difficult to enhance. As you
learned in the class, one way to overcome these issues is to exploit a combination of spatial
enhancement methods.
a. Implement the strategy which was presented in the class and discuss the advantage of each step.
Name the result as skeletol_combine.bmp and write it into the output directory.
Combining Spatial
Enhancement Methods
12. Load the image house.jpg. Write a program to apply the following filters on the provided image
where fx or fy are the gradient of the original image in x or y direction. Examine the resultant images
precisely and justify the appearance of the outputs according to its corresponding filter.
1) g fx and g fy
2) g fx fy
3) g fx fy
Figure 11.
13. Write a program to hide a message image into a cover image. It is trivial that the two images must
be the same size. While containing the message image, the cover image must show the least artifact or
loss of information. In the other hand, the significant information of the message image is necessary to
be maintained as well.
a. Load two images lena.jpg and Barbara.jpg as cover and message images, respectively, and
hide the most significant bit of message image into the cover image. Note that the appearance of
the cover image must not be changed considerably. Name the resultant image as
lena_watermarked.bmp and write it into the output directory. Explain what you have done. Wellcommented code is of high importance.
b. Write a program to extract the message image. To do so, your program must use only the
watermarked and raw cover images. Verify your program on the resultant watermarked cover
image yielded from the part a. Name the extracted image as Barbara_extracted.bmp and write
it into the output directory. Explain your observation.
c. Rescale the message image by factor 0.5 and repeat the above procedure. Since your message
is smaller than the cover image, it is expected to save more information of it during the watermarking
and extraction procedures. Explain what you have done.
Cover
Message
Watermarked Image
Hiding
Watermarked Cover
Raw Cover
Extracted Message
Extract
Figure 12.
14. Fig.13 (left-side) shows a grayscale linear wedge input. Fig.13 (right-side) shows the projected image
captured by a camera. Write a program to implement the gamma correction upon the input.jpg such
that the grayscale of the captured image will be linearly distributed, as much linearly as possible. Show
the corrected input image and the image that will be captured after the gamma correction, according
to your predication.
15. Study the following paper and explain the main idea and its algorithm in at most two pages. Your
report must clearly contain the shortcomings of previous methods and the contributions of the proposed
method. Try to provide a compact and informative report.
Chen, ZhiYu, et al. "Gray-level grouping (GLG): an automatic method for optimized
image contrast Enhancement-part I: the basic method." Image Processing, IEEE
Transactions on 15.8 (2006): 2290-2302.