an implementation of sift in java code - university of otago ·  · 2009-05-09an implementation of...

21
Chapter 5 An implementation of SIFT in Java code 5.1 Introduction This chapter describes an implementation of the Scale Invariant Transform Feature (SIFT) detector and descriptor [2][1]. This implementation in Java is designed to produce results compatible to Lowe or Andrea Vedaldi’s version. This implantation tries to extract an image from a collection of keypoints. These are oriented features of the image, so they are invariant to deformation like translation, rotation and scaling. They are partially invariant to change of illumination as well. The idea is to remove the effect of such deformation using mapping activity among images which have the same elements. The SIFT descriptor is a coarse description of the edge found in the image, as they are the representation of keypoints and all their characteristics as explained above. 5.2 Scale Space Lowe suggests to use two scale-space for sift implementation, Gaussian and difference of Gaussian. The first one is the function: G(x; σ) (g σ * I )(x) where g σ is an isotropic Gaussian kernel of variance σ 2 I , x is the spatial coordinate and σ is the scale coordinate. The Gaussian scale space repre- sents the image I (x) at different levels of scale, hence the same strategy as 49

Upload: nguyenngoc

Post on 22-May-2018

251 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

Chapter 5

An implementation of SIFT in

Java code

5.1 Introduction

This chapter describes an implementation of the Scale Invariant Transform

Feature (SIFT) detector and descriptor [2] [1]. This implementation in

Java is designed to produce results compatible to Lowe or Andrea Vedaldi’s

version. This implantation tries to extract an image from a collection of

keypoints. These are oriented features of the image, so they are invariant

to deformation like translation, rotation and scaling. They are partially

invariant to change of illumination as well. The idea is to remove the effect

of such deformation using mapping activity among images which have the

same elements. The SIFT descriptor is a coarse description of the edge

found in the image, as they are the representation of keypoints and all their

characteristics as explained above.

5.2 Scale Space

Lowe suggests to use two scale-space for sift implementation, Gaussian and

difference of Gaussian. The first one is the function:

G(x; σ) , (gσ ∗ I)(x)

where gσ is an isotropic Gaussian kernel of variance σ2I, x is the spatial

coordinate and σ is the scale coordinate. The Gaussian scale space repre-

sents the image I(x) at different levels of scale, hence the same strategy as

49

Page 2: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

50 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

suggested by Lowe is used to reduce the redundancy. Each octave is further

subdivided into sublevels. For each successive octave the data is spatially

downsampled by half. The octave index ”o” and the sub-level index ”s” are

mapped to the corresponding scale σ by the formula:

σ(o, s) = σ02o+s

S , oǫ + [0, ..., O − 1], sǫ[0, ..., S − 1]

where σ0 is the base scale level, ”O” is the number of octave into the

scale space and S is the number of smoothed images into the Gaussian scale

space. Below is the process for exaction of keypoints and descriptor into the

Java code; each function will specify the value and meaning of the variable

used. We paint all the process in macro activity, as discussed in later pages.

The process starts by using the possible path to identify images that needs

to be match each other. This enhance the possibility to recognize the same

points for the same image in different frames or to recognize the image of

an object into the image where the object is present.

Figure 5.1: Scheme of our Sift execution

In this figure is schematized the execution of our code in macro step.

Page 3: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

51 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

5.3 Analysis

Analysis object is the structure to manage the matrix of the image and the

value of the single pixel. The image must be converted in Grey value. This

structure gives us the possibility to use just one matrix and will be upload

into a normal structure of Java like BufferedImage to show the result or save

as a new image. This give us the possibility to control the code complexity.

Tables 5.1,5.2 and 5.3.

Field Summarypublic float[][] data: this is the pixels matrix of image in GREY scale. The pixel value

is within range [0,1].As explained by Lowe it is important to normalizethe value in this range for SIFT process

public int height: is the height of the image matrixpublic int width: is the width of the image matrixfloat min: is the min value in the original image extracted from the filefloat max: is the max value in the original image extracted from the file

Table 5.1: Field Summary of Analysis

Costructory SummaryAnalysis():an empty object analysisAnalysis(float [][] data in, int height, int width):a new Analysis objectand copy into the image matrix data in which have height and widthdimensionsAnalysis ( int height, int width): a new object Analysis with emptyimage matrix and set its dimensions with height and width valueAnalysis (String src1, String dst2): a new Object Analysis from asource(src1) image which will be converted into Grey scale image andsaves the Grey scale image result into a dst2 file

Table 5.2: Costructory Summary of Analysis

5.4 Gaussianss

The Gaussianss object uses the object Analysis to make the Gaussian pyra-

mid. This object sets a lot of parameters. Hence Lowe’s value is followed

where:

Variable: ”sigman” (σn = 0.5) Nominal pre-smoothing. This is the

nominal smoothing level of the input image. The algorithm assumes that

the input image is actually (gσn∗ I)(x) as opposed to I(x) and adjusts the

computations according.

Page 4: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

52 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

Method Summaryvoid CopyAnalysis (float [][] data in,int height,int width): makes the copy

into an object Analysis of image matrix data in but you must specifythe height and width dimensions of data in must be specified.

BufferedImage open (String path): catches the BufferedImage value for the imagespecified into path string

BufferedImage make (BufferedImage image): makes another BufferedImage objectwhich is given back with the same dimensions of input BufferedImage

void save(BufferedImage dst image, String path):saves the BufferedImageObject into the destination specified path

BufferedImage convert(BufferedImage source image, dst image): uses the source imageto make a new Grey scale image which will be stored in dst imagereturn. The value of the pixels will be changed from the range [0..255]to [0..1] where the value will be normalized using the min and maxvalues

void setMinMax (): will find the min/max values into a matrix imagefloat [][] getData( ): gives back the matrix image ”data”Analysis DoubleSize(Analysis I): it return a new Analysis object which have

double dimensions of the original I and it uses the bilinear interpolationAnalysis HalveSize(Analysis I): it return a new Analysis object which have half

dimensions of the original Ifloat getPixel( int x, int y): it return the value of the pixel in specified

position x, yint getHeight( ): it return the height value of the matrix imageint getWidth ( ): it return the width value of the matrix imagevoid Fix( ): this function normalizes the value of the image matrix using

max and min value

Table 5.3: Method Summary of Analysis

Variable: sigman0(1.6 ∗ 21/S) Base smoothing.

The sigma value is always applied for smoothing process, thus the pre-

scale process that has been done on the image. Must be considered the

pre-scale process follows the value of ”omin” variable:

Variable: omin this variable consist of two kinds of value; negative value,

(for example ”-1”) which means that the image must be double sized before

to build the Gaussian pyramid. If the value is positive, (for example ”1”)

then the dimension of the image before starting the process must be halved.

Tables 5.4,5.5 and 5.6

5.5 DoG

Here this section discusses another scale space, as calculated from the Gaus-

sian pyramid (we make the difference of two matrix image smoothed which

are close). This scale space has the same dimensions of Gaussian one, but

Page 5: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

53 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

Field SummaryAnalysis [][] octave: this is the structure that is used to represent the scale space,

which is like the Gaussian pyramid. Each image is smoothed using aGaussian kernel where the value of sigma follows Lowe’s setting

int hightOctave: this specifies the number of octaves that we have in Gaus-sian pyramid

int widthOctave: this specifies the number of smoothed images into eachoctave

Table 5.4: Field Summary of Gaussianss

Costructory SummaryGaussianss (Analysis I, float sigman, float O, float S, float omin, floatsmin, float smax, float sigma0): this construct uses ”I” like input ma-trix image which is the source used to make the Gaussian pyramid,”sigman” is as specified above, ”O” is used to identify the numberof octaves that is needed for scale space, ”S” is the number of scalesper octave that Lowe (during his experimental stage) shows to be thebest value for the repetition of keypoints; ”omin” is as specified above;”smin” and ”smax” give the number of smoothed images per octave,”sigma0” is used to smoothen the image into the first stepGaussianss( ): empty object

Table 5.5: Costructory Summary of Gaussianss

Method SummaryAnalysis Imsmooth (Analysis I, float sigma): this is the function which is used

to smoothen the image at different levels into the octaves. ”I” is theimage at precedent level, ”sigma” is the degree of smoothing that wewant to apply, it represents the sigma value for the Gaussian kernelthat will be applied

void convolve (float [][]dst, float [][] src, int height, int width, float []filter,float W): this method is used to apply Gaussian filter to source theimage matrix without continuity at the edge. This function is appliedtwice for one kernel Gaussian dimension, hence reducing the processcomplessity

void econvolve (float [][]dst, float [][] src, int height, int width, float []filter,float W): similar as above but here we use the continuity at the edgeof the image matrix

int getKernelSize(float sigma): it gives back the dimensions of the Gaussiankernel

Table 5.6: Method Summary of Gaussianss

Page 6: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

54 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

each octave has one less matrix\sub level.

Field SummaryAnalysis [][] DoG: this is the structure that is used to represent the scale space. It

is the Difference of the Gaussian pyramidint heightDoG: this specifies the number of octaves that we have in differ-

ence of the Gaussian pyramidint widthDoG: this specifies the number of difference of Gaussian matrix

into each octave

Table 5.7: Field Summary of DoG

Costructory SummaryDoG (Gaussianss ss): It uses the Gaussian scale space to build a dif-ference of Gaussian scale space

Table 5.8: Costructory Summary of DoG

5.6 SiftLocalMax

This object is used to identify the keypoints for the image. Here check have

been applied as a guarantee of stability. We can summarize it in the follow

steps (for more details refer to chapter2):

• Local extreme detection

• Accurate keypoint localization

• Eliminating edge responses

Tables 5.9,5.10 and 5.11

5.7 KpVect

This is the object used to manage keypoints. Each keypoint has ”x” and ”y”

to identify the position, ”s” sub level value into the octave, ”o” orientation

value, ”m” magnitude value and ”oct” octave value if the keypoint is referred

to the scale space. If the keypoint refers to the final image, then this value

is empty.

Page 7: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

55 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

Field Summaryboolean [][] LoMax: we use this boolean scale space to identify the local min\max

into the difference of the Gaussian pyramid. The dimensions are: samenumber of octaves with ”S” being the number of sub-levels

int KpNum: number of keypoints identifiedint max iter: number of iteration which is used when the 3D quadratic

function is appliedKpVect [] KpV: this is the vector of Keypoints identified into the boolean scale

spaceKpVect [] KpVframes: this is the vector of Keypoints identified, into the original

image. We have two different vectors for management simplicityint l: it manages the number of elements into vector KpVKpVect [] f: it manages the number of elements into vector KpVframes

Table 5.9: Field Summary of SiftLocalMax

Costructory SummarySiftLocalMax (DoG dog, Gaussianss gss, float thresh): we use theGaussian and difference of Gaussian scale space, ”dog” and ”gss”, and”thresh” threshold value for edge responses

Table 5.10: Costructory Summary of SiftLocalMax

Method Summaryfloat [] Orientation (float src x, float src y, float src s, DoG dss, Analysis mtx):

the function which is used to calculate the keypoint identified in orien-tation and magnitude. They are used in the next DescriptorB object.The first value of vector is orientation, while the second is magnitude

void addKpVectFrames (float x, float y, float s, float o, float m): this methodis used to add an element to the KpVframes vector

void addKpVectFrames (float x, float y, float s, float o, float m, int octave):this method is used to add an element to the KpV vector

Table 5.11: Method Summary of SiftLocalMax

Field Summaryfloat x: abscissa positionfloat y: ordinate positionfloat s: sub level position into octavefloat o: orientationfloat m: magnitudefloat oct: octave number if it refers to scale space keypoint. Not defined if

it is the keypoint for the final image

Table 5.12: Field Summary of KpVect

Page 8: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

56 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

Costructory SummaryKpVect(float x,float y,float s,float o,float m, float oct): it sets the key-point valueKpVect(float x,float y,float s,float o,float m, float oct): it sets the key-point value

Table 5.13: Costructory Summary of KpVect

5.8 DescriptorB

The descriptor that we use here is exactly the same as explained by Lowe

in his research. A SIFT descriptor of keypoint (x, σ) is a local statistic of

the orientations of the gradient of the Gaussian scale space.

Tables 5.14,5.15 and 5.16

Field Summaryfloat[][] KpDesc: it is exactly the matrix of the descriptor. Each row is a

descriptor for the keypointfloat NBP: Bins for each spatial directionfloat NBO: Bins for orientation

Table 5.14: Field Summary of DescriptorB

Costructory SummaryDescriptorB (KpVect [] KpV,Gaussianss gaussian): We need to have in-formation about the keypoint vector which will be made into a descrip-tor, and the Gaussian scale-space is used to make the scale-space formagnitude and orientation which will be computed in a region aroundthe keypoint

Table 5.15: Costructory Summary of DescriptorB

Method Summaryfloat fast mod (float th): this return a value of the angle into the range

[0..2*PI]void normalize histogram (int p): normalize the value of histogram orienta-

tion about the specified descriptor

Table 5.16: Method Summary of DescriptorB

5.9 MatchB

This first implementation match as the two image, meaning that the best

candidate match for each keypoint in the first image is found by identifying

Page 9: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

57 CHAPTER 5. AN IMPLEMENTATION OF SIFT IN JAVA CODE

its nearest neighbour in the set of keypoints identified in the second image.

The nearest neighbour is defined as the keypoint with minimum Euclidean

distance. Here another kind of match function is implemented by trying to

remove false matching using a gross match activity from 2 match activities:

1. Match1: Image1 to Image2

2. Match2: Image2 to Image1

3. Match3: compare Match1 and Match2 which uses only the match

which is the identical source and destination keypoint

Tables 5.17,5.18 and 5.19

Field Summaryint[] k1: Vector of keypoints from the first image. This specifies only the

position of the keypoint into the real keypoint vectorint[] k2: Vector of keypoints from the second image. This specifies only the

position of the keypoint into the real keypoint vectorint[] score: Score value for the match among keypoints matched

Table 5.17: Field Summary of MatchB

Costructory SummaryMatchB (DescriptorB dsc1, DescriptorB dsc2): matching activity usingthe descriptor of the first and the second imageMatchB (MatchB mtc1, MatchB mtc2,KpVect [] dsc1, KpVect [] dsc2):cross activity matching using two precedent match and keypoints vec-tors

Table 5.18: Costructory Summary of MatchB

Method Summaryvoid add (int k1, int k2, int score): when we identify a new match this func-

tion is used to add a new element to the vector of matched keypoints

Table 5.19: Method Summary of MatchB

Page 10: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

Chapter 6

Java SIFT interface

6.1 Introduction

This chapter explains the interface which is used for the ”SIFT” Java code.

Here we want show what functions and results we have, when we use the

SIFT program. We don’t explain what happen into the function but we

just specify the input and output value during all the process. For more

information about the single function look the Appendix ??. The interface

starts with the object ”Application” where there is a method ”main” which

opens an object ”MyFrame”.

6.2 MyFrame

This object extends the class JFrame. Here we organize the interface and

the area of the windows. There are inside three JPanel:

• NorthPn is the north panel

• EastPn is the east panel. It is an EastPanel object which extends the

class JPanel

• WestPn is the west panel. It is an WestPanel object which extends

the class JPanel

The object NorthPn have a JMenuBar which is not yet defined. The

idea is to develop some activity in future. The object EastPn is the panel

which is used to show the results. They could be the image that we choose

58

Page 11: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

59 CHAPTER 6. JAVA SIFT INTERFACE

for the matching activity, the image where we paint the keypoints found or

where we match the two image and theirs keypoints. The object WestPn is

where we find the bottom which are linked with the Java function. They

use the input and produce the output showed in EastPn. (Look figure 6.1)

Figure 6.1: Graph of application interface

6.3 WestPn

Here we have five JButtom and one JComboBox. The first activity to do is

to set the value of threshold. There are three possibility value: low, middle

and high. If this value is not defined, when we try to active some bottom

an error message will ask us to set the threshold value. We use an object

””SIFT” where we set the value and run the function for all processes.

JComboBox is linked a ”itemListener” that we use to read the value and to

run setThresh function in ”SIFT”:

When we use the buttons in this panel others object which implement

the ”ActionListener”, are linked to listen and run the processes associated

with them.

Page 12: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

60 CHAPTER 6. JAVA SIFT INTERFACE

public class WestPanel extends JPanel{SIFT sift=new SIFT();......

public class SIFT {.̇..̇..̇.

public void setThresh(float threshold){this.thresh=threshold;

}.........

}

Table 6.1: Class WestPanel

6.3.1 selectObject and selectImage

The first two buttons ”selectObject” and ”selectImage” link to a listener

which is an object FilePreview:

public class FilePreview implements ActionListener {public void actionPerformed (ActionEvent e){Object src = e.getSource();......

Table 6.2: Class FilePreview

”FilePreview” reads the event and recognize the button then active the

right activity. The first activity here is to open a window which is used

to talk with the file system to identify the image choice. This is done

using a ”JFileChoicer” object (chooser) which is linked with another object

”JPreviewPanel” which extends JPanel. We have the possibility to have a

preview when the file selected is an image ”gif” or ”jpeg”. When we choose

our file, the object ”chooser” return the path that we use to recognize the

image.

Both the buttons active a function into object ”sift” where we start the

activity until the match step. The activity about EastPn will be discussed

into the next paragraph. Now is enough to say that we run the function to

update EastPn which will show the image or objects choices. When we select

the image which will be the object looking for, we will run sift.ReadObj

where we will make:

• the object Analysis which has the matrix of image object

Page 13: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

61 CHAPTER 6. JAVA SIFT INTERFACE

...

...

...if (src==SelObjBt){object=file.getPath();try{sift.ReadObj(object);}catch (Exception ex){JOptionPane.showMessageDialog(null,”Error in read object activ-

ity”);}EastPn.removeAll();EastPn.EastPanelUpGrade(object);EastPn.repaint();EastPn.validate();}else if (src==SelImgBt){image=file.getPath();try{sift.ReadImage(image);}catch (Exception ex){JOptionPane.showMessageDialog(null,

”Error in read image activity”);}/* UpDate of JPanel East*/EastPn.removeAll();EastPn.EastPanelUpGrade(image);EastPn.repaint();EastPn.validate();}

Table 6.3: Class WestPanel in a different point

• the Gaussian scale space

• the Difference of Gaussian scale space

• the SiftLocalMax where we identify the keypoints

• the Descriptor for each keypoint identified

For ”SelectImage” button we will make:

6.3.2 Keypoint, Orientation and Matching

For these three button there is associated the listener ”Listen” (Table 6.6)

which works recognizing the button activated\clicked and runs the function

in ”sift” specified for each one. We run some function for ”EastPn” but like

we said before, they are explained after, now we say just that they update

the east panel with the results make from the piece of code associated to

Page 14: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

62 CHAPTER 6. JAVA SIFT INTERFACE

public void ReadObj(String src) throws Exception{this.src=src;String destinazione=”LastObject”;I2= new Analysis (src,destinazione);O= (float)Math.floor(Math.log(Math.min(I2.width,

I2.height))/Math.log(2))-omin-3;gaussianIm2=new Gaussianss (I2,sigman,O,S,omin,-1,S+1,sigman0);dssIm2=new DoG(gaussianIm2);ReObjectBo=true;KP2=new SiftLocalMax(dssIm2,gaussianIm2,thresh);KD2=new DescriptorB(KP2.KpV,gaussianIm2);System.out.println(”ReadObj over”);}

Table 6.4: Read object function

public void ReadImage(String src) throws Exception{this.src2=src;String destination=”LastImage”;I= new Analysis (src2,destination);O= (float)Math.floor(Math.log(Math.min(I.width,

I.height))/Math.log(2))-omin-3;gaussianIm=new Gaussianss (I,sigman,O,S,omin,-1,S+1,sigman0);dssIm=new DoG(gaussianIm);ReImgBo=true;System.out.println(”ReadImafe over”);}

Table 6.5: Read image function

each button.

For ”Keypoint” button we will run ”sift.KP( )” (Table 6.7) function

where for the image we make:

• the SiftLocalMax where we identify the keypoints

• the Descriptor for each keypoint identified

• save the image with the keypoints identified

The first two object, KP and KD1, are explained into the documentation

of code. The local function ”this.SaveImage( )” (Table 6.8)is used to save

into a file the image selected for the matching activity and where we paint

the Keypoints identified. The destination will be always the same file:

We will use the object ”SiftLocalMax” and the vector of keypoints in-

side (KpVframes) to identify the position. To show better them, we will

paint in red color a little square with the keypoint into the center. For the

”Orientation” button we run only a function which uses the image with the

Page 15: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

63 CHAPTER 6. JAVA SIFT INTERFACE

public class Listen implements ActionListener{public void actionPerformed (ActionEvent event){Object src = event.getSource();......

if (sift.ReImgBo){sift.KP();EastPn.removeAll();EastPn.EastPanelUpGrade(sift.dst);EastPn.repaint();EastPn.validate();}else

JOptionPane.showMessageDialog(null, ”Select ImagePlease”);

}else if (src==Orientation){

if (sift.ReImgBo){EastPn.removeAll();EastPn.Orientation(sift);EastPn.repaint();EastPn.validate();}

elseJOptionPane.showMessageDialog(null, ”Select Image

Please”);}

else if(src==Match)if (sift.getKpBo()==true){

EastPn.removeAll();EastPn.Match(image,object,sift);EastPn.updateUI();EastPn.validate();}

elseJOptionPane.showMessageDialog(null, ”Select Image

and Object Please”);}

}

Table 6.6: Class Listen

...

...public void KP(){

KP=new SiftLocalMax(dssIm,gaussianIm,thresh);KD1=new DescriptorB(KP.KpV,gaussianIm);this.SaveImage();}

...

...

Table 6.7: KP function

Page 16: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

64 CHAPTER 6. JAVA SIFT INTERFACE

...

...private void SaveImage(){dst=”KpImage.jpg”;Analysis TMP=new Analysis ();

try{TMP= new Analysis(this.src,dst);immagine sorgente=TMP.open(src);

}catch (Exception ex){System.out.println(”Error in SaveImage OpenStep”);

}DS=3;for (int i=0;i¡DS;i++)

TMP=TMP.DoubleSize(TMP);ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS GRAY);ColorConvertOp op = new ColorConvertOp(cs, null);BufferedImage Image buffer= new

BufferedImage(TMP.width,TMP.height,immagine sorgente.getType());

float value;for(int y=0; y¡TMP.height; y++){

for(int x=0; x¡TMP.width; x++){Color c;value=TMP.data[y][x]*255;if (value¿255){

value=255;}

c=new Color ((int)Math.ceil(value),(int)Math.ceil(value),(int)Math.ceil(value));

Image buffer.setRGB(x, y, c.getRGB());}

}for (int i=0; i¡KP.KpVframes.length;i++){

int x src=(int)((KP.KpVframes[i].x*(Math.pow(2.0, DS))));

int y src=(int)((KP.KpVframes[i].y*(Math.pow(2.0, DS))));

Color c;for (int x=x src-1;x¡x src+2;x++){

for (int y=y src-1;y¡y src+2;y++){if (x¡0——x¿TMP.width-1——y¡0——y¿TMP.height-1)

System.out.println(”Error”);c=new Color (255,0,0);Image buffer.setRGB(x, y, c.getRGB());

}}

} try{TMP.save(Image buffer,dst);

}catch (Exception ex){System.out.println(”Error in SaveImage SaveStep”);

}}......

Table 6.8: SaveImage function

Page 17: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

65 CHAPTER 6. JAVA SIFT INTERFACE

keypoints saved with the Keypoint button, to paint in east panel the arrows

which symbolize magnitude and orientation identified for each feature. In

this case every activity happen into the EastPn which receive only the ”sift”

object with all the information that we need. (Look the paragraph about

the EastPn 6.4) For the ”Match” button we paint into the East panel both

the source (object and image) and we run identify the match among feature.

Again we choice to compute this activity into EastPn directly. (Look the

paragraph about the EastPn 6.4)

6.4 EastPn

East panel is used to show the result after some important activity. Inside

there are some function called from button in East panel. Look Table 6.9

...

...public class EastPanel extends JPanel{......

public EastPanel (String image){this.image=image;JLabel imageEast = new JLabel (new ImageIcon(this.image));

... ... this.add(imageEast);} ......

Table 6.9: class EastPn

The constructor paint into the east panel a static image. There is a

very important function which is used to organize and manage the result.

Here we have a java slider which is used to set the dimension of the image

presents into the East panel. This function is called from others functions

just to paint correctly and in the same way the results. Look Table 6.10

The other function that which is called from button in west panel is

”EastPanelUpgrade”. It receives only the string path about the image which

will be painted: Look Table 6.11

The function ”orientation”, called from orientation button in West panel,

opens the image where we always save the result about the image with the

keypoints and using the information in SiftLocalMax object read the value

of orientation and magnitude for each feature and paint a new static image

Page 18: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

66 CHAPTER 6. JAVA SIFT INTERFACE

public void Common(){GridBagLayout gridbagMain = new GridBagLayout();GridBagConstraints cMain = new GridBagConstraints();this.setLayout(gridbagMain);cMain.fill = GridBagConstraints.HORIZONTAL;

panel = new JPanel();GridBagLayout gridbag = new GridBagLayout();GridBagConstraints c = new GridBagConstraints();panel.setLayout(gridbag);c.fill = GridBagConstraints.HORIZONTAL;

JLabel label=new JLabel(”Set Image Size:”);c.gridx = 0;c.gridy = 1;gridbag.setConstraints(label, c);panel.add(label);......

cMain.gridx = 0;cMain.gridy = 1;gridbagMain.setConstraints(panel, cMain);this.add(panel);cMain.gridx = 0;cMain.gridy = 2;gridbagMain.setConstraints(imageView, cMain);this.add(imageView);

model.addChangeListener(new ReadOutSynchronizer());}

Table 6.10: Function Common

public void EastPanelUpGrade (String image){model=new DefaultBoundedRangeModel(100,0,0,100);slider=new JSlider(model);readOut=new JLabel(”100imageIcon= new ImageIcon(image);imageView=new ImageView(imageIcon,model);imageView.setPreferredSize(new Dimension (800,600));

this.Common();}

Table 6.11: Function EastPanelUpGrade

Page 19: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

67 CHAPTER 6. JAVA SIFT INTERFACE

with the arrows which symbolize them: Table 6.12

public void Orientation (SIFT sift){String image=sift.dst;String dst=”Orie”.concat(image);try{

BufferedImage imageOrie= ImageIO.read(new File(image));Graphics2D g = (Graphics2D) imageOrie.getGraphics ();

g.setColor (Color.GREEN);/*Paint line for orientation*/for (int i=0; i¡sift.KP.KpVframes.length;i++){

int im d=(int)Math.pow(2, sift.DS);int orientation=(int)(sift.KP.KpVframes[i].o*180/Math.PI);double magnitude=(int)(sift.KP.KpVframes[i].m*1500);int x dst=(int)((sift.KP.KpVframes[i].x*im d)

+(magnitude*Math.cos(orientation*2.0*Math.PI/360.0)));

int y dst=(int)((sift.KP.KpVframes[i].y*im d)+(magnitude*Math.sin(orientation*2.0*Math.PI/360.0)));int x src=(int)((sift.KP.KpVframes[i].x*im d));int y src=(int)((sift.KP.KpVframes[i].y*im d));g.drawLine(x src, y src, x dst, y dst);

}ImageIO.write (imageOrie, ”jpg”, new File (dst));}catch (Exception ex){

System.out.println(”Error in EastPanel.Orientation”);}model=new DefaultBoundedRangeModel(100,0,0,100);slider=new JSlider(model);readOut=new JLabel(”100imageIcon= new ImageIcon(dst);imageView=new ImageView(imageIcon,model);imageView.setPreferredSize(new Dimension (800,600));this.Common();}

Table 6.12: Function Orientation

The function ”Match” is where we compute the process of matching

among the selected image. This step is not develop very well and probably

it will be soon modified. Anyway here we upload the two image into a

BufferedImage object which have the dimension enough large to include

both the source that we want match. We compute the activity of matching

using the ”MatchB” object. The result will be used to paint the line among

the features which have a correspondence into the array computed for the

object MatchB. Table 6.13,6.14

Page 20: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

68 CHAPTER 6. JAVA SIFT INTERFACE

public void Match(String Image1,String Object,SIFT sift){int im d=2;Image image;Image object;

Toolkit tk = Toolkit.getDefaultToolkit();image = tk.getImage(Image1);object = tk.getImage(Object);

ImageIcon TMP=new ImageIcon();BufferedImage imageBuf;try{imageBuf=ImageIO.read(new File(Image1));imageBuf=this.resize old(imageBuf, imageBuf.getWidth()*im d ,

imageBuf.getHeight()*im d);ImageIO.write (imageBuf, ”jpg”, new File (”TMP1.jpg”));}catch (Exception ex){

System.out.println(”Error”);}

BufferedImage objectBuf;try{objectBuf=ImageIO.read(new File(Object));objectBuf=this.resize old(objectBuf, objectBuf.getWidth()*im d ,objectBuf.getHeight()*im d);ImageIO.write (objectBuf, ”jpg”, new File (”TMP2.jpg”));}catch (Exception ex){System.out.println(”Error”);}image = tk.getImage(”TMP1.jpg”);object = tk.getImage(”TMP2.jpg”);

Dimension originalSize = new Dimension();TMP.setImage(image);originalSize.height=TMP.getIconHeight();originalSize.width=TMP.getIconWidth();Dimension originalSize2 = new Dimension();TMP.setImage(object);originalSize2.height=TMP.getIconHeight();originalSize2.width=TMP.getIconWidth();

Table 6.13: Function Match

Page 21: An implementation of SIFT in Java code - University of Otago ·  · 2009-05-09AN IMPLEMENTATION OF SIFT IN JAVA CODE Method Summary ... The sigma value is always applied for smoothing

69 CHAPTER 6. JAVA SIFT INTERFACE

...

...BufferedImage matchBuf=new BufferedImage((originalSize.width)+(originalSize2.width)+30, Math.max((originalSize.height),(originalSize2.height)),BufferedImage.TYPE INT ARGB);Graphics2D g2=(Graphics2D)matchBuf.createGraphics();

g2.setBackground(Color.white);g2.clearRect(0, 0, (originalSize.width)+(originalSize2.width)+30,Math.max((originalSize.height),(originalSize2.height)));g2.drawImage(image,0,0,null);g2.drawImage(object,(originalSize.width)+30,0,null);MatchB match1=new MatchB(sift.KD1,sift.KD2);MatchB match2= new MatchB(sift.KD2,sift.KD1);MatchB match= new MatchB(match1,match2,

sift.KP.KpVframes,sift.KP2.KpVframes);

for (int c=0;c¡match.k1.length;c++){int x1,y1,x2,y2;

x1=(int)(sift.KP.KpVframes[match.k1[c]].x*im d);y1=(int)(sift.KP.KpVframes[match.k1[c]].y*im d);

x2=(int)(sift.KP2.KpVframes[match.k2[c]].x*im d);y2=(int)(sift.KP2.KpVframes[match.k2[c]].y*im d);

g2.setColor(Color.BLUE);g2.drawLine(x1, y1, (int)originalSize.getWidth()+x2+30, y2);

}

model=new DefaultBoundedRangeModel(100,0,0,100);slider=new JSlider(model);readOut=new JLabel(”100imageIcon= new ImageIcon(matchBuf);imageView=new ImageView(imageIcon,model);imageView.setPreferredSize(new Dimension (800,600));this.Common();}

Table 6.14: Function Match