Professional Documents
Culture Documents
08 45 49
08 45 49
Prepared by :
1)Snehal Bhayani (201211008)
2)Sagar Ahir (201211045)
3)Ankur Undhad(201211049)
Introduction
Computation of large chunks of data demands machine tools for efficient and at-leisure
implementation of the specific algorithms. And as is the case here, we are to process images of
approximately 512 * 512 resolution. Reading the images into matrices makes it an array of
numbers large enough to make it impossible to process them without a software programming
package.
Hence using various tools provided by higher level programming languages, we perform
a many operation on these images(or the matrices so obtained from them). MATLAB is used to
primarily process the given images. We use it to convert the image into a gray scale image and
then form a matrix out of the same. As an aside, we crop the image using MATLAB as well.
Apart from that, all of the other computations are done using JAVA programming
language. The IDE used for JAVA is Eclipse software. An open source IDE for a full-fledged
JAVA program development, we employ various high level API for reading, writing, and
processing substantially voluminous matrices of data. With layers of abstraction we define
objects of complex number structures and along with them operations of complex number
system. Modularity and structural technique enable us to define a systematic approach to tackling
the computational problem.
Norms of the matrices, DFT and IDFT, as well as plotting the results of the magnitude and angle
plot of DFT and IDFT as photographs is implemented using JAVA language.
Some errors abound due to the limitations of computer in representation of real numbers.
To cite a few, representation of the irrational number PI, taking Nth roots of real numbers that are
not Nth powers of some other rational number, etc. These errors being quite minute, can be
neglected when the size of the image is comparable in terms of it's resolution and pixel array.
Indeed as the comparison between the IDFT image and the DFT image show, we have been able
to recreate the original image with quite negligible errors.
Some of the concepts of representation of color models and the floating point
implementation in JAVA have been looked into along-with the computation exercise.
Project discussion
Question 1
Facial photographs of all of the three participants are taken and named accordingly.
According to first question, we use MATLAB to convert the given image from RGB format to
Gray-Scale format.
In RGB format each pixel is specified by three values of red ,green and blue wherein 8
bit and 16 bit storage methods which range from 0 to255 and 0 to 655365 respectively are
employed.
IRGB = ( IR + IG + IB )
---------------------------- (1)
For Gray-scale, the most commonly used storage method is 8 bit storage in which there
are 256 gray levels or in other words each pixel is represented by the intensity value ranging
from 0-255.
For this we use IMREAD( ) command in MATLAB to read the image from the path
given. Once the image is copied into the working directory of MATLAB, we can
Then by using command IMSHOW( ); we display both:
1) RGB Image and (2) Gray scale image.
The program for the same can be found in Appendix A.
Matrix NORMs
A matrix norm || . || is function from C m x n to R with three properties :
1) ||A|| 0 for all A C m x n and ||A|| = 0 if and only if A=0.
2) || A+B || ||A|| + ||B|| for all A C m x n .
3) || A || = || ||A|| for all C, A C m x n .
-------------------------------------------------(2)
Infinity norm : (maximum of row sum):
-------------------------------------------------(3)
One norm : ( maximum column sum ) :
--------------------------------------------------(4)
--------------------------(5)
As we have a product of three matrices, we will need to tweak a bit in our formula to calculate
the matrix product NORM upper bound.
-----------------(6)
Again once DFT is calculated here, we can move up to calculating the IDFT. For that the upper
bound in relative error is given as :
-------------------(7)
Here we present the general derived formulae whose detailing and application to our DFT
problem is given in appendix C.
We use these formulae for calculating the values of Error NORMs in DFT and IDFT calculation.
Here as the formulae are for infinity NORM we can get the same for One NORM as well.
Where 0 m ,n N-1.
---------------(8)
same, as required in given exercises, we create custom JAVA Objects Complex. Also
are defined all of the operations that are legitimate on mathematical complex numbers as
JAVA functions. Thus at all places while processing matrices with each element as
Complex object, we employ the custom defined operations on them instead of the
orthodox plain mathematical operations.
And further employing this modularity and a top-down approach, number of JAVA
functions for generating Transform Matrices, Multiplying two Complex matrices,
printing the same on IO console, etc. are clearly defined and called from the main method
of JAVA.
JAVA program
Divided structurally and logically we have two .java files. One is the Complex.java for
modeling the mathematical complex numbers and their operations. The other is
MultiplyMatrices.java with the actual flow for processing the images.
Listing the operations performed by our program in order of execution:
1. Reading the specified image from the given path into JAVA defined Image Object.
2. Reading the pixel values from the image into a given 2D array. Again assuming its a
gray scale image (for in exercise 1 we already converted it into Gray-scale using
MATLLAB) the RGB model is split to obtain the value of brightness as an integer
between 0 and 255. The arrays so formed are represented by variable <B>.
3.
We calculate the NORMs Infinity and One using a simple looping structure for the
given array <B>.
4. The
transformation
array
<transformationMat>
and
its
conjugate
<invTransformationMat> are generated with quite a degree of accuracy. Certain
special cases are considered as specified in the program to reduce the errors due to
floating point representation.
5.
Question 2
Here we calculate the NORMs Infinity and One for the matrices that have been represented.
Infinity Norm of Image snehalgray.jpg = 96727.0
One Norm of Image snehalgray.jpg = 129602.0
Infinity Norm of Image sagargray.gif = 87533.0
One Norm of Image sagargray.gif = 88349.0
Infinity Norm of Image ankurgray.jpg = 129418.0
One Norm of Image ankurgray.jpg = 129418.0
All of these images and their original versions are attached in appendix D.
Question 3
Below mentioned operations are performed on all of the three matrices representing the three
images snehalgray.jpg , sagargray.gif and ankurgray.jpg. These operations are done for
each image one at a time using the JAVA program.
(a) In theory 2.4 we have shown DFT and IDFT as matrix multiplications.
(b) We calculate DFT using JAVA (program given in appendix B). The operation uses a plain
nested looping structure simply multiplying matrices element by element.
(c) Similarly we calculate the IDFT reusing the matrix multiplication logic.
Time span to complete
DFT
IDFT
snehalgray.jpg
24 seconds
23 seconds
sagargray.gif
23 seconds
22 seconds
ankurgray.jpg
25 seconds
23 seconds
Question 4
As mentioned in the program steps, for each one of the three arrays representing the three
images, we print the images of their processed arrays. Special API used for the same in java, the
concepts of BufferedImage and ImageIO are employed for the same.
Creating an empty image, we fill its pixels with the values from the specified array and then
save the same to hard-disk.
The Image representing magnitude(IDFT) is the same as that of the original gray image.
Upon visual inspection its hard to point out the errors, but there is some error. The various
NORMs of many arrays as calculated are tabulated here:
Given Data
Transformation M atrix
Inverse-transformation
matrix
DFT
IDFT
snehalgray.jpg
96727.0
22.627416997969664
22.627416997969664
176877.86736597194
96726.99999999945
sagargray.gif
87533.0
22.627416997969664
22.627416997969664
133546.99128534563
87533.00000000004
ankurgray.jpg
129418.0
22.627416997969664
22.627416997969664
142651.7869675418
129417.99999999999
NORM S**
Image (All are
512*512
dimensioned)
Given Data
Transformation M atrix
Inverse-transformation
matrix
DFT
IDFT
snehalgray.jpg
129602.0
22.627416997969664
22.627416997969664
189419.03909827032
129601.99999999907
sagargray.gif
88349.0
22.627416997969664
22.627416997969664
133546.99128534563
88348.99999999999
ankurgray.jpg
129418.0
22.627416997969664
22.627416997969664
169874.22738548837
129417.99999999999
NORM S**
Image (All are
512*512
dimensioned)
NORM -wise
upper
bounds**
Upper bound in
DFT(Infinity NORM )
Upper bound in
DFT(One NORM )
Upper bound in
IDFT(Infinity NORM )
Upper bound in
IDFT(One NORM )
snehalgray.jpg
1686.5501185722708
2505.316288474069
7775126.98042321
1.154797288695631E7
sagargray.gif
2018.8433278918765
2545.0504779533635
9306414.801078651
1.1731177348562002E7
ankurgray.jpg
2791.418087105529
2764.2717157700945
1.2866431315967677E7
1.2741259947083991E7
Table 4
(Relative error upper bounds with respect to perturbed matrices)
Conclusion
Some points to be noticed in terms of results of Computational exercises:
(a) Owing to the vastness of the matrices being handled and the way of employing the
algorithm, a substantial amount of time was consumed in generating the DFT and IDFT
of the given images.
The approximate time in completing all of the Matrix operations turned out to be
a little over 1 minute. This comes off due to the number of real multiplications and real
additions occurring. Around (1073741824) multiplications and (1073217536) real
additions.
And the data-type being used to represent each element is a custom JAVA object.
Thus the overhead of processing each memory occupying object for an individual
element takes up substantial processor time.
(b) The matrix operations have been done using the simplest algorithms and hence errors due
to representation of special numbers as well as real operations of exponentiation, arise.
And the magnitude of the errors are seen to be proportional to the dimension of the
matrices involved. To get an idea of the relative error in calculation of DFT and IDFT we
use the matrix 1 and infinity NORMs.
Assuming that the errors are only due to representation of matrix elements. And
errors due to truncation of matrix multiplication are taken to be as errors in representation
of numbers. So that our task of specifying the NORM-wise upper bound due to matrix
multiplication becomes simpler and clearer.
So we find the upper bound of NORM-wise relative error for DFT and IDFT. The
upper bounds for both infinity and One NORM are nearly the same for DFT and IDFT.
The values are quite large for IDFT of the order of 10 ^ 5 . Refer to tables 2,3 and 4 for
comparisons of various NORMS of matrices and their NORM-wise error upper bounds.
The reason found is the large condition number due to the large values of NORMS of
individual matrices and the NORMs of intermediate matrices are much larger than the
NORMs of data and final product. And also its not helped by the fact that error in
generating the Transformation matrix is unnecessarily approximated to a less tight bound
as we cant find || - W || and thus we approximate to its upper limit as |||| + ||W||
Appendices
Appendix A
Program1 : A program for reading the image in MATLAB and converting it into a Gray-scale
image. Herewith we as well convert the 3 D array into a 2D one by converting the third
dimensions value to 1. And then save the same gray-scale image in D:// drive for being read by a
java code at a later stage.
Here as an example we read one image. The same can be used for other two images simply by
replacing the path of the image from the function imread().
Appendix B
Program1 : MultiplyMatrices.java
/*A JAVA program to perform various operations needed to compute matrices and thereby perform IMAGE processing.
* The API used in here are primarily that of JAVA level packages for working on image processing.
* The list of operations that are performed w ith this JAVA program are specified in THEORY titled "JAVA Program" in text.
* And this code is written using Eclipse Java EE IDE for Web Developers.( Indigo Release )
* It references another Complex.java code. That is also listed below.
* The version of JVM used is JRE1.6
* Images converted to gray-scale are read from path D:\<Name OF the image>
* And Printed Back to path E:\<Name Of the Image>
* It is for the Photograph of one person And iteratively we can do the same for the other two photographs.
*/
import
import
import
import
import
import
import
import
import
import
java.awt.Image;
java.awt.image.BufferedImage;
java.awt.image.Col orModel;
java.awt.image.Pi xelGrabber;
java.io.BufferedInputStream;
java.io.File;
java.io.FileInputStream;
java.io.InputStream;
java.util.Calendar;
javax.imageio.ImageIO;
/*Back transformation of the 2d resulting matrix for DFT and IDFT transformation into a 1D array.
* Thus we can then print the images resulting from the pixel values onto hard disk into the form of an image.
* ALso to note is a critical point that so far we have processed the matrices with a byte of representation for each pixel.
* To print it back in the form of RGB format, we convert back the single value into a number representing the RGB values.
* With the equation as RGBValue = (intensity)*256^2 + (intensity)*256 + (intensity) .
*/
// For Printing the magnitude of the DFT matrix
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
int temp = (int)(Complex.mag(DFT[i][j]));
DFTData[i*size+j] = temp *256 * 256 + temp *256 + temp;
}
}
// Create a custom JAVA object of type <BufferedImage> with needed dimensions (that equal those of the original image matrix).
BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
// Set the individual pixel value to be equal to that of the one contained in the created DFTData array.
image.setRGB(0, 0, size, size, DFTData, 0, size);
// Save the image contained in JAVA object onto a hard disk and save it with the specified name.
File imageFile = new File("E://capturedImageDFTMagnitude.jpg");
ImageIO.write(image, "jpg", imageFile);
System.out.println(" DFT Magnitude Image created and saved ");
// For Printing the angle of the DFT matrix
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
int temp = (int)(Complex.arg(DFT[i][j]));
DFTData[i*size+j] = temp *256 * 256 + temp *256 + temp;
}
}
// Create a custom JAVA object of type <BufferedImage> with needed dimensions (that equal those of the original image matrix) .
image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
// Set the individual pixel value to be equal to that of the one contained in the created DFTData array.
image.setRGB(0, 0, size, size, DFTData, 0, size);
// Save the image contained in JAVA object onto a hard disk and save it with the specified name.
imageFile = new File("E://capturedImageDFTAngle.jpg");
ImageIO.write(image, "jpg", imageFile);
System.out.println(" DFT Angle Image created and saved ");
} catch(Exception e)
{
e.printStackTrace();
}
}
// Norms Zero and Infinity are calculated in this function. And in an array of size-2, both the NORMS are returned.
// Here position 0 has Infinity NORM
// And position 1 has One NORM
public static double[] findNORMs(Complex[][] data)
{
int size = data[0].length;
double norms[] = new double[2];
double max=0;
for(int i=0;i<size;i++)
{
double sum =0;
for(int j=0;j<size;j++)
{
sum = sum + Complex.mag(data[i][j]);
}
if(max<sum)
max = sum;
}
norms[0]=max;
//
System.out.println(" The Infinity Norm of the given matrix is ::: "+max);
for(int i=0;i<size;i++)
{
double sum =0;
for(int j=0;j<size;j++)
{
sum = sum + Complex.mag(data[j][i]);
}
if(max<sum)
max = sum;
}
norms[1]=max;
System.out.println(" The Zero Norm of the given matrix is ::: "+max);
return norms;
//
}
/*
*
*
*
*
*
*
Here To generate the W matrix for DFT transformation, we will set some predefined rules. As per the notations of mathematic al symbolism,
w^N = 1
w^(N/2) = -1
w^(N/4) = 0 + j1
*/
Program2 : Complex.java
/*A Custom object "Complex" is defined along with it's attributes and functions that can operate on those attributes.
* Here mathematically a complex number will be denoted as c = a + jb
* where <a> and <b> will be the attributes of our custom object "Complex" that has it's structure mapped to mathematical complex number.
* and all of the operations of the complex number can have with itself or with another complex number are also define d on our JAVA representation
* of the complex number.
*
*/
public class Complex {
double r=0; // the real part
double i=0; // the imaginary part
/*
* A constructor to set its real and imaginary parts while declaring an instance of the complex number.
*/
public Complex(double r, double i)
{
this.r =r;
this.i=i;
}
/* An empty constructor
*/
public Complex() {
}
/*Addition of two complex numbers
*/
public static Complex add(Complex c1 , Complex c2)
{
Complex c = new Complex();
c.r = c1.r + c2.r;
c.i = c1.i + c2.i;
return c;
}
/* Multiplication of two complex numbers
*/
public static Complex mul(Complex c1 , Complex c2)
{
Complex c = new Complex();
c.r = c1.r * c2.r - c1.i *c2.i;
c.i= c1.r * c2.i + c1.i*c2.r;
return c;
}
/* Inverting of a complex number
*/
public static Complex inv(Complex c1)
{
Complex c = new Complex();
double den = (c1.i * c1.i + c1.r * c1.r);
c.r = c1.r/den;
c.i = -c1.i/(den);
return c;
}
/* Multiply ing a complex number with a real number.
*/
public static Complex scale(Complex c1,double alpha)
{
Complex c = new Complex();
c.r = c1.r * alpha;
c.i = c1.i * alpha;
return c;
}
/*Power of a complex number to a real number.
*/
public static Complex powC(Complex c1, double x)
{
Complex c = new Complex();
double mag = Math.sqrt(c1.r * c1.r + c1.i *c1.i);
double angle = Math.atan2(c1.i,c1.r);
c.r= Math.pow(mag, x) * Math.cos(angle * x);
c.i= Math.pow(mag, x) * Math.sin(angle * x);
return c;
}
/*Magnitude of a complex number
*/
public static double mag(Complex c1)
{
Complex c = new Complex();
double mag = Math.sqrt(c1.r * c1.r + c1.i *c1.i);
return mag;
}
Appendix C
Here we have calculated the NORM-wise error in matrix multiplication as applied to calculation
of DFT Transforms and IDFT Transforms. The DFT matrix being a product of 3 matrices and
also
Appendix D
Picture 1
Captured Image of < snehalgray.jpg>
Picture 2
Captured Image of Magnitude< DFT for< snehalgray.jpg>>
Picture 3
Captured Image of Angle< DFT for< snehalgray.jpg>>
Picture 4
Captured Image of Magnitude< IDFT for <DFT for< snehalgray.jpg>>>
Picture 5
Captured Image of < sagargray.gif >
Picture 6
Captured Image of Magnitude< DFT for< sagargray.gif >>
Picture 7
Captured Image of Angle< DFT for<sagargray.gif>>
Picture 8
Captured Image of Magnitude< IDFT for <DFT for< sagargray.gif>>>
Picture 9
Captured Image of < ankurgray.jpg>
Picture 10
Captured Image of Magnitude< DFT for< ankurgray.jpg>>
Picture 11
Captured Image of Angle< DFT for< ankurgray.jpg>>
Picture 12
Captured Image of Magnitude<IDFT for < DFT for< ankurgray.jpg>>>
Further Reading
[1] Color Model in Java :
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/ColorModel.html
[2] Luminance in Color Model:
http://en.wikipedia.org/wiki/Luminance_(relative)
[3] Java.awt.Image :
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Image.html
[4] IEEE floating point representation
http://en.wikipedia.org/wiki/IEEE_floating_point