CS 111: Programing Assignment 4 This programming assignment is focused on geometric transformation and histogram processes. You should submit the output images in a ZIP file (not rar or other file compression conventions) and drop it into EEE dropbox for PA4. You will NOT allowed to use any OpenCV functions unless you are told to do in pa4.cpp. In this assignment you only work with gray images. So, use CV_LOAD_IMAGE_GRAYSCALE when reading your images. Histogram Equalization: I. Histogram Matching a. Histogram equalization is a special case of histogram matching. In histogram matching we have a current histogram (the histogram of our image) and a target histogram. The goal is to change the pixel values of the current image such that the current histogram becomes similar to the target histogram. b. To perform histogram matching, we first calculate the Cumlative Distrbution Function (CDF) of both current and target histograms. If we devide the histogram of an imge by the number of pixels in that image, we get a Probaiblity Density Function (PDF). The sum of PDF values is 1. If an image has H pixels and the histogram is: Then, by dividing each bin by the number of pixels you will get the PDF as: Page 1
Then, by calculating the cumulative sum CDF(h) = h 0 PDF(i) is: c. Once we have the CDF of the current and target histograms, then we can find the mapping to perform the histogram matching. For each gray level in the current image you should find its CDF value, and then find its CDF value equivalent gray level in the target CDF. For example, in the image below a gray level x is chosen. It s CDF value is equal to CDF value of gray level y. Therefore, x should be mapped into y. Assume the current CDF is shown by function F and the target CDF is function G. Then the relation between x and y is y = G 1 (F(x)) II. Histogram Equalization a. It is desirable that the probibility distribution of gray levels be equally distributed. If so, the image utilizes the dynamic range of possible values (e.g. 0-255) and will have optimal contrast. The equally distributed histogram PDF and CDF are as follows: Page 2
b. Here, when you are working with discrete values, the PDF and CDF will be discrete functions (bar histogram in earlier images) and when you want to find the equall CDF value between the current and target CDF they may not match. In this assignment you should search for closest CDF value. Absolutely, you can implement smart search algorithms since the CDF is always non-decreasing, however in this assignment you can just do simple linear search. c. You should write the function Mat HistogramEqualization(Mat I) that gets an image at input and return the histogram equalized output image. This function is composed of multiple functions that you should complete for this section i. FindPDF: Input is the gray image and output is the histogram in the format of pdf ii. FindCDF: Input is the pdf and output is the CDF iii. FindEqualMapping: Input is the cdf and output is the mapping after equalization iv. ApplyEqualization: Input is the image and mapping, it applies the mapping on the image and returns the eualized image. Mat FindPDF(Mat I); Mat FindCDF(Mat pdf); Mat FindEqualMapping(Mat cdf); Mat ApplyEqualization(Mat I, Mat mapping) Mat HistogramEqualization(Mat I) { Mat currpdf = FindPDF(I); Mat currcdf = FindCDF(currPDF); Mat eqmap = FindEqualMapping(currCDF); Mat output = ApplyEqualization(I, eqmap); return output; } Page 3
Complete all four functions that compose the HistogramEqualization function in pa4.cpp, and apply it on the input image aerial.png. You should submit the output image aerial_eq.png along with the codes in pa4.cpp in your submitted zip file. Geometric Transformation III. Bi-linear Interpolation a. In geometric transformation, we assume the image a 2D function that has a value for each point on the 2D grid. b. The transformation maps a 2D point to another 2D point. T(x, y) (x, y ) If (x, y ) is on the grid, we can easily find its value, but it needs a general solution for real numbers. The (x, y ) can be anywhere on the 2D space not limited to the integers (grid numbers). If the query point (x, y ) is inside the range of the image (x: 0 to N-1, y: 0 to M 1, for a MxN image) then the operation of finding the value for query point is interpolation. If the query point is outside the coordinates of the image the operation for finding the value is called extrapolation. c. One simplest method of interpolation is called nearest neighbor. In this method you will assign the value of the closest grid point to the query point. Because of poor result and blockiness nature of this interpolation method, it is used very rarely. d. One important and widely used methods for interpolation is called billinear interpolation. In this method it is assumed that each grid block creates a 2D linear function and we can find the value for the query points in each block of the grid. Page 4
e. In 1D linear interpolation you can estimate the query value between known values. In this example we want to linearly interpolate the value at 1.25, which is between 1 and 2. V(1.25) = (1.25 1)1 + (2 1.25)3 = 0.25 + 2.25 = 2.5 f. In 2D, we calculate the liner interpolation in one direction and then for the top and bottom boundaries and then we calculate it becomes a 1D interpolation problem, as follows: v(q 1 ) = (x q x 1 )v 2 + (x 2 x q )v 1 Then, v(q 2 ) = (x q x 1 )v 4 + (x 2 x q )v 3 v(q) = (y q y 1 )v(q 2 ) + (y 2 y q )v(q 1 ) IV. Image Resizing a. Suppose we want to resize an image to twice of its current size. Since we will have more pixels in the output, we should interpolate the values between the pixels. In this work we will perform a bi-linear interpolation. To resize and image we place the four corners of the output image on the same coordinates of the four corners of the input image and then interpolate all the output pixels. b. Assume you have 4x4 image and now you want to resize it to 6x6. The four corners of the output image(6x6) is placed on the four corners of the input image (4x4) and all output pixels (black dots) are within the range of the input image pixels (circles). In the following image, you can see how the query coordinates are placed. Page 5
c. In this assignment you should write a function that sclaes the input image into s times of its input size. The function Mat Resize(Mat I, float s). You should first fill in the X and Y Mat s that contained the query point coordinates. Then you should calculate the query value by using bi-linear interpolation. Mat Resize(Mat I, float s) Complete the function Resize(Mat I, float s) in the pa4.cpp, and apply on the input image aerial.png and wite it in aerial_640.png. Submit the code and the output image in the zip file. Page 6