Wednesday, July 17, 2013

Fourier Transform Model

We’ve done a lot on image processing; we’ve learned a lot of ways to determine the properties and information about images. Here we would be discussing another way- getting the Fourier domain of the image. Fourier transform is a process of converting signals from a dimension X to dimension of 1/X. For images, this converts the spatial domain of images to the frequency domain. A fast way of implementing FT is through the Fast Fourier Transform algorithm. [1,2] For our fourth activity, the concept of Fourier transform was used for image processing.


Familiarizing with discrete Fourier transform

Fourier transform can be performed in images using Scilab. First, we were asked to get the FFT of different synthetic images with dimension of 128 x 128 pixels. FFT for 2D signals makes use of the function fft2(). The dimension was chosen as such because fft2() was more efficient for images with sizes in power of two. [2] We have learned to use Scilab in make synthetic image of circle. Circles of different radius were produced and FFT would be applied to each. 

Synthetic images of circles with different radius sizes.

Basically, applying FFT in images using Scilab is as follows. Since synthetic images produced from Scilab was purely single channel, no need to read the image in grayscale. Some alterations on the given sample code was made like using the double() function to convert the image matrix values into floats before applying the fft2(). The output of the the fft2() are complex number array so abs() function was used  to compute the magnitude of the complex number. Function mat2gray() was also used to represent the matrix as grayscale image. Finally, image was show using either the functions imshow() or imwrite()

The FFT of the circles resulted like a diffraction pattern when light was made pass through slits: as the radius of the circle was decreased, the pattern became more prominent (fringes became bigger). The output of the FFT below was observed on the corner. Thus the FFT must be shifted to move the information to the center of the image. 

FFT of circles with different radius.

Function fftshift() was used and this results to the output below. 

Shifted FFT of circles of different radius.

A sample Scilab code employing FFT on image is illustrated below


FFT was also applied to letter A with font style Arial and font size of 50 pt. The inverse Fourier transform is the conversion of images from the frequency domain to the spatial domain. This can be done just by using the forward FFT however the image produced is inverted. [2] Applying inverse FFT to the FFT of circle, the output observed just looks its original image. When it was applied to the FFT of letter 'A' image, it resulted to an inverted 'A'. 

From left to right, synthetic image of letter A, FFT of 'A' image, shifted FFT of 'A' image, inverse FFT of the FFT of 'A'. 


Convolution: Simulation of an imaging device

Convolution is a linear operation that "smears" one function to another. The output function will look like the both the two input functions. This can also be applied to images. A synthetic image of the VIP text shown below was convolved to circles of different radius. 

Synthetic image of the 'VIP' text.

The resulting images were as follows. For this case, the circles were treated as apertures. For small apertures, the image output is blurry. As the aperture gets bigger, the image became clearer. 

Convolution outputs using circles of different radius (refer to first figure for corresponding circle sizes)


Correlation: Template matching

Another linear operation is the correlation. Correlation measures the similarity between the two functions. If there is high resemblance between the two functions, the correlation value is also high. This was done for the first two images below. Correlation was done in Scilab using the following steps. First, getting both the FFT of the letter A image and the complex conjugate of the FFT of the text image. Then multiplying them element by element. Dot product was only applied here. Function conj() was used to compute the complex conjugate. The resulting image is the last image below, which observed to have the brightest spot on the positions of the letter A in the text. This is just what expected since you want to see the correlation between the two images, that is having the letter A. 

From left to right, image of the text, image of letter 'A' and the correlation output.


Edge detection using convolution integral

Edge detection can be done using convolution. The edge to be detected was that from the text VIP while the detector was different 3x3 matrix pattern. For example, as how it should look like in the Scilab code,

pattern = [-1 -1 -1; 2 2 2; -1 -1 -1] 

is the pattern for horizontal edge detector. The edge patterns and the corresponding detected edges were shown below. Explaining the result simply, the detected  edge depended on the pattern used as aperture. Horizontal edges appeared brighter when horizontal edge pattern was used, etc. The whole edge was detected when the spot pattern was used. 

Edge detection from left to right: horizontal, vertical, diagonal down, diagonal up, and spot size.

More code for this activity. I am really slow when it comes to coding so I am very grateful that this activity seemed like a class activity on the day this was sent to us. I thank Anjali Tarun, Alix Santos and Joshua Beringuela for the useful exchange of ideas especially the needed alteration for the code to work :) As usual I thank my blog/code buddy, Nestor Bareza. 

A grade of 10/10 for me in this activity because I am able to do all the tasks..
__________
References
[1] R. Fisher, et al. Fourier transform. Retrieved from http://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm
[2] Soriano, M. "Fourier Transform Model of Image Formation." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.

Thursday, July 4, 2013

Histogram Manipulation

It's pretty hard to take pictures at night, images will most likely be dark. I think many will agree with me.. the fun starts at night (just to be clear.. I'm not a party person but I do believe that people had their most fun happenings at night)! But what if your only-once-in-a-lifetime captured moment turnout dark? Worry not! :) Scilab, or for the simplest way possible, advanced image processing software like GIMP has a solution for that :)

Every images, when in grayscale, has its histogram. It is the Probability Distribution Function (PDF) of the image that lets you identify the tonal distribution of an image. Even those dark-looking images still contain information, they were just in the darker range. If the PDF is modified, enhancement on the image can be done.. dark images could look brighter, etc. For our sixth activity, we are asked to enhance pictures through histogram manipulation. This was done by "backprojecting" the grayscale values into the desired Cummulative Distribution Function (CDF) using the CDF of the PDF of the original image.[1,2] This will be further discussed later on. Let us consider this picture. 


Using Scilab, the image was converted to grayscale using the function rgb2gray() and the resulting image is seen below. 


Then, histogram of the grayscaled image was obtained using imhist() function. Scilab Help would always make our life easier. The variable hist that I used in the code (seen far below) represent the distribution itself and the cells are the bins. Unless specified, the bins of the histogram were set in default, which is 256 given the type of our image. So we can plot hist vs. cells or just plot(hist). But to produce a normalized plot, the histogram must be divided by the total number of pixels of the image. The normalized histogram is shown in the figure below


CDF of the histogram was obtained using the function cumsum(). Again normalizing, CDF resulted to the following plot


As you can see, the pixels were concentrated on small values. We want the values to be equally distributed implying that there would be the same amount of dark and light colors. For the desired equally distributed histogram, the CDF is linear as shown in the figure below. Human eye, on the other hand, does not have a linear response. So we will try other CDF - one that represents a sigmoid function. Backprojecting was done by finding the corresponding y-values of the original CDF to the desired CDF then replacing the pixels value with the x-values from the desired CDF. [1]

The desired CDFs showing a linear function (left) and sigmoid function (right). 

I understand well the concept of backprojecting. However, I really have a problem on incorporating it into a code. Ma'am Soriano suggested to use interp() function but it's really hard for me to figure out the correct syntax. For that, I'm really thankful to Nestor Bareza (for like always willingly helping me :)) I also thank Joshua Beringuela for the helpful discussion about the find() function. 

Now the enhanced images are as follows:

Enhanced image using a linear CDF. 

Enhanced image using a sigmoid CDF. 

Comparing with the grayscale of the original picture, the brightening up of the photo was evident. It was observed that for the enchanced image using linear CDF was lighter and revealed more details than that of the sigmoid CDF. Also, to check, the PDF and CDF of the enhanced images are shown below. The CDF of the enhanced image somewhat follows its desired CDF.

PDF of the enhanced images using a linear CDF (left) and sigmoid CDF (right). 

CDF of the enhanced images using a linear CDF (left) and sigmoid CDF (right).

The complete code:



Now using GIMP to modify the histogram..

So I guess the class' favorite subject in this activity is our very handsome classmate, Nestor :P So I thought of doing the same :)

Histogram manipulation can also be done using GIMP. From colors menu, just click the curves. This is the original image with the linear curve..


Adjusting the  curve...


Oops! wrong move. undo undo. Our lovely Tor would not be pleased. hehe


Okay, better :) So yah! You can't really resist this beauty and hotness. XD

If the concavity of the curve shifts upward, the image becomes darker. What you want is the downward concavity in order to make the picture appears brighter and reveal more details. Trying a different curve, image will look something like..


It can also be performed using other freeware image processing software.. so I have Paint.NET installed in my laptop, better try it out! :) The picture of the Siene River with Eiffel Tower in the view is obtained from [3]. Exploring the menus and tools, you would find in Adjustment menu the Curves and just like in the GIMP it can be adjusted.




So yes, it worked just like the GIMP. And again playing with the curve..


Tadah! A pretty nice color combination :)

I'm giving myself 11/10 for successfully enhancing the pictures through their histogram and employing this on other freeware software, Paint.NET :)
__________
References
[1] Soriano, M. "Enhancement by Histogram Manipulation." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.
[2] Image Histogram. Retrieved from http://en.wikipedia.org/wiki/Image_histogram
[3] http://shedexpedition.com/wp-content/uploads/2013/05/France-Eiffel-Tower-and-the-Seine-River-at-Night.jpg