Listing Program 1. MainWindow.cs
using System; using System.Windows;
namespace SkripsiTepe {
public partial class MainWindow : Window {
public MainWindow {
InitializeComponent; }
void btnstart_Clickobject sender, System.Windows.RoutedEventArgs e {
MainSearchWindow win = new MainSearchWindow; win.Show;
} void btnhelp_Clickobject sender, RoutedEventArgs e
{ HelpWindow win2 = new HelpWindow;
win2.Show; }
} }
2. MainSearchWindow.cs using SkripsiTepe.DTOs;
using SkripsiTepe.Helper; using SkripsiTepe.Indexers;
using SkripsiTepe.Query; using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Diagnostics; using System.IO;
using System.Windows; using System.Windows.Controls;
using System.Windows.Media.Imaging; namespace SkripsiTepe
{ public partial class MainSearchWindow : Window
Universitas Sumatera Utara
{ private const string host = localhost;
public bool ExecInParallel { get; set; } public string IndexDirectory { get; set; }
public string QueryImageFullPath { get; set; } public emAlgo SelectedAlgo { get; set; }
private BackgroundWorker IndexBgWorker; private BackgroundWorker QueryBgWorker;
private Stopwatch _stopWatch; public MainSearchWindow
{ InitializeComponent;
} private void frmMainSearch_Loadedobject sender, RoutedEventArgs e
{ InitIndexDirectory;
cmbAlgo.SelectedIndex = Properties.Settings.Default.SelectedAlgo; SelectedAlgo = GetSelectedAlgo;
LoadSurfSettings; SetBackgroundWorker;
} private void LoadSurfSettings
{
cmbSurfThreshold.SelectedIndex = Properties.Settings.Default.SurfThresholdSelectedIndex;
cmbSurfApproach.SelectedIndex = Properties.Settings.Default.SurfAlgo; }
private void InitIndexDirectory {
string indexDirecroty = Properties.Settings.Default.IndexFolder; if string.IsNullOrEmptyindexDirecroty
{ indexDirecroty = DirectoryHelper.PictureObserverDirectory; helper
Properties.Settings.Default.IndexFolder = indexDirecroty; }
SetIndexDirectoryindexDirecroty; }
002 private void SetIndexDirectorystring folder
{ lblDirectoryPath.Text = folder;
IndexDirectory = folder;
Universitas Sumatera Utara
Properties.Settings.Default.IndexFolder = folder; Properties.Settings.Default.Save;
} asynctask setting
private void SetBackgroundWorker {
IndexBgWorker = new BackgroundWorker; IndexBgWorker.DoWork += new DoWorkEventHandlerIndexBgWorker_DoWork;
IndexBgWorker.ProgressChanged += new ProgressChangedEventHandler IndexBgWorker_ProgressChanged;
IndexBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler IndexBgWorker_RunWorkerCompleted;
IndexBgWorker.WorkerReportsProgress = true; IndexBgWorker.WorkerSupportsCancellation = true;
QueryBgWorker = new BackgroundWorker; QueryBgWorker.DoWork += new DoWorkEventHandlerQueryBgWorker_DoWork;
QueryBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler QueryBgWorker_RunWorkerCompleted;
QueryBgWorker.WorkerReportsProgress = true; }
private void SaveSurfSettingValues {
this.Dispatcher.Invoke = {
Properties.Settings.Default.SurfThresholdSelectedIndex = cmbSurfThreshold.SelectedIndex;
Properties.Settings.Default.SurfAlgo = cmbSurfApproach.SelectedIndex; Properties.Settings.Default.Save;
}; }
private emAlgo GetSelectedAlgo {
emAlgo selectAlgo = emAlgo.Undetermined; if cmbAlgo.SelectedIndex -1
selectAlgo = emAlgocmbAlgo.SelectedIndex + 1; return selectAlgo;
} public FileInfo[] getFilesstring SourceFolder, string Filter,
System.IO.SearchOption searchOption {
ListFileInfo files = new ListFileInfo;
Universitas Sumatera Utara
DirectoryInfo di = new DirectoryInfoSourceFolder; Create an array of filter string
string[] MultipleFilters = Filter.Split|; for each filter find mathing file names
foreach string FileFilter in MultipleFilters {
add found file names to array list files.AddRangedi.GetFilesFileFilter, searchOption;
} returns string array of relevant file names
return files.ToArray; }
private SurfSettings GetSurfSetting {
SurfSettings surfSetting = new SurfSettings; this.Dispatcher.Invoke =
{ surfSetting.HessianThresh = Convert.ToDoublecmbSurfThreshold.SelectedItem as
ListBoxItem.Content; surfSetting.Algorithm = SurfAlgocmbSurfApproach.SelectedIndex;
}; return surfSetting;
} private void cmbAlgo_SelectionChangedobject sender,
System.Windows.Controls.SelectionChangedEventArgs e {
Properties.Settings.Default.SelectedAlgo = cmbAlgo.SelectedIndex; Properties.Settings.Default.Save;
SelectedAlgo = GetSelectedAlgo; if SelectedAlgo == emAlgo.AccordSurf
{ var selectedIndex = cmbSurfApproach.SelectedIndex;
cmbSurfApproach.Items[1] as ListBoxItem.Content = kd-tree; cmbSurfApproach.SelectedIndex = -1;
cmbSurfApproach.SelectedIndex = selectedIndex; }
} region Indexing Section
001 - Pilih Folder private void btnSelectFolder_Clickobject sender, RoutedEventArgs e
{
Universitas Sumatera Utara
string _folderName = IndexDirectory; var dlg1 = new FolderBrowserDialogEx helper
{ Description = Select a folder for the extracted files:,
ShowNewFolderButton = false, ShowEditBox = true,
SelectedPath = _folderName, ShowFullPathInEditBox = false,
}; dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;
System.Windows.Forms.DialogResult result = dlg1.ShowDialog; if result == System.Windows.Forms.DialogResult.OK
{ SetIndexDirectorydlg1.SelectedPath;
} }
003 private void btnIndex_Clickobject sender, RoutedEventArgs e
{ btnIndex.IsEnabled = false;
if SelectedAlgo == emAlgo.AccordSurf {
Save Settings SaveSurfSettingValues;
Pass Surf Settings SurfSettings surfSetting = GetSurfSetting;
IndexBgWorker.RunWorkerAsyncsurfSetting; }
else {
IndexBgWorker.RunWorkerAsync; }
} 004
private void IndexBgWorker_DoWorkobject sender, DoWorkEventArgs e {
try {
WriteIndexStatusIndexing...; SurfSettings surfSetting;
switch SelectedAlgo
Universitas Sumatera Utara
{ case emAlgo.Undetermined:
MessageBox.ShowPlease select Algorithm for indexing; return;
case emAlgo.AccordSurf: surfSetting = e.Argument as SurfSettings;
if surfSetting == null {
MessageBox.ShowSurfSetting not supplied; return;
}; AccordSurfAlgoIndexingsurfSetting;
break; default:
MessageBox.ShowPilih Algoritma; return;
} }
catch Exception ex {
Debug.WriteLineex.ToString; throw;
} }
004a private void IndexBgWorker_ProgressChangedobject sender, ProgressChangedEventArgs
e {
pbIndex.Value = e.ProgressPercentage + 1; }
004b private void IndexBgWorker_RunWorkerCompletedobject sender,
RunWorkerCompletedEventArgs e {
if e.Error = null MessageBox.Showe.Error.ToString;
btnIndex.IsEnabled = true; }
private void WriteIndexStatusstring msg {
var txtdispatcher = lblIndexStatus.Dispatcher; if txtdispatcher.CheckAccess
{ txtdispatcher.InvokeAction = lblIndexStatus.Text = msg;
}
Universitas Sumatera Utara
else {
lblIndexStatus.Text = msg; }
}
005 private void AccordSurfAlgoIndexingSurfSettings surfSetting
{ var imageFiles = getFilesIndexDirectory, .gif|.jpg|.png|.bmp|.jpeg,
SearchOption.TopDirectoryOnly; int totalFileCount = imageFiles.Length;
Dispatcher.InvokeActiondelegate {
pbIndex.Minimum = 0; pbIndex.Maximum = totalFileCount; lblIndexStatus.Text = Indexing..;
}; if surfSetting.Algorithm == SurfAlgo.Linear
{ SurfIndexer4 surfIndexer = new SurfIndexer4;
if ExecInParallel surfIndexer.IndexFilesAsyncimageFiles, IndexBgWorker, WriteIndexStatus,
surfSetting; else
surfIndexer.IndexFilesimageFiles, IndexBgWorker, WriteIndexStatus, surfSetting; }
} region Context Menu
private void brdImage_ContextMenuOpeningobject sender, ContextMenuEventArgs e {
FrameworkElement fe = e.Source as FrameworkElement; ContextMenu cm = fe.ContextMenu;
while cm == null {
fe = FrameworkElementfe.Parent; if fe == null break;
cm = fe.ContextMenu; }
MenuItem openSurfDetails = null; foreach MenuItem mi in cm.Items
{ if mi.Name == mnuOpenSurfDetail
{
Universitas Sumatera Utara
openSurfDetails = mi; }
} if SelectedAlgo == emAlgo.AccordSurf
{ if openSurfDetails = null openSurfDetails.Visibility =
System.Windows.Visibility.Visible; }
else {
if openSurfDetails = null openSurfDetails.Visibility = System.Windows.Visibility.Collapsed;
} }
private void mnuOpenSurfDetail_Clickobject sender, RoutedEventArgs e {
string selectedObserverImage = string.Empty; string selectedModelImage = string.Empty;
MenuItem mnu = sender as MenuItem; Border selectedBorder = null;
if mnu = null {
selectedBorder = ContextMenumnu.Parent.PlacementTarget as Border; selectedObserverImage = stringselectedBorder.Tag;
} selectedModelImage = QueryImageFullPath;
bool isObserverImagePathFound = string.IsNullOrWhiteSpaceselectedObserverImage; isObserverImagePathFound = isObserverImagePathFound
File.ExistsselectedObserverImage; bool isModelImagePathFound = string.IsNullOrWhiteSpaceselectedModelImage;
isModelImagePathFound = isObserverImagePathFound File.ExistsselectedModelImage;
if isObserverImagePathFound isModelImagePathFound {
SurfSettings surfSetting = GetSurfSetting; if SelectedAlgo == emAlgo.AccordSurf
{ pindah windows AccordSurfWindow win = new AccordSurfWindowselectedModelImage,
selectedObserverImage, surfSetting; win.Show;
}
Universitas Sumatera Utara
} }
private void mnuOpenPic_Clickobject sender, RoutedEventArgs e {
string selectedObserverImage = string.Empty; string selectedModelImage = string.Empty;
MenuItem mnu = sender as MenuItem; Border selectedBorder = null;
if mnu = null {
selectedBorder = ContextMenumnu.Parent.PlacementTarget as Border; selectedObserverImage = stringselectedBorder.Tag;
} selectedModelImage = QueryImageFullPath;
bool isObserverImagePathFound = string.IsNullOrWhiteSpaceselectedObserverImage; isObserverImagePathFound = isObserverImagePathFound
File.ExistsselectedObserverImage; if isObserverImagePathFound
{ System.Diagnostics.Process.StartselectedObserverImage;
} }
endregion 006a
region Pilih gambar dan cari private void btnSelectQueryImage_Clickobject sender, RoutedEventArgs e
{ string queryImagePath = SelectQueryImage;
QueryImagequeryImagePath; }
006b ulangipencarian
private void btnQueryImage_Clickobject sender, RoutedEventArgs e {
QueryImageQueryImageFullPath; }
007 private string SelectQueryImage
{ string queryImageFilePath = string.Empty;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog;
Universitas Sumatera Utara
if string.IsNullOrWhiteSpaceProperties.Settings.Default.QueryFolder {
dlg.InitialDirectory = IndexDirectory; }
else {
dlg.InitialDirectory = Properties.Settings.Default.QueryFolder; }
Set filter for file extension and default file extension dlg.DefaultExt = .jpg;
dlg.Filter = JPG Files .jpg|.jpg|JPEG Files .jpeg|.jpeg|PNG Files .png|.png|JPG Files .jpg|.jpg|GIF Files .gif|.gif;
Display OpenFileDialog by calling ShowDialog method Nullablebool result = dlg.ShowDialog;
Get the selected file name and display in a TextBox if result == true
{ Open document
queryImageFilePath = dlg.FileName; txtQuery.Text = queryImageFilePath;
QueryImageFullPath = queryImageFilePath; imqQuery.Source = new BitmapImagenew UriqueryImageFilePath;
Properties.Settings.Default.QueryFolder = Path.GetDirectoryNamequeryImageFilePath;
Properties.Settings.Default.Save; }
return queryImageFilePath; }
009 private void QueryBgWorker_DoWorkobject sender, DoWorkEventArgs e
{ this.Dispatcher.Invoke =
{ btnSelectQueryImage.IsEnabled = false;
btnQueryImage.IsEnabled = false; };
switch SelectedAlgo {
Universitas Sumatera Utara
case emAlgo.AccordSurf: QueryAccordSurfAlgoQueryImageFullPath;
break; default:
MessageBox.ShowPilih Algoritma; return;
} this.Dispatcher.Invoke =
{ btnSelectQueryImage.IsEnabled = true;
btnQueryImage.IsEnabled = true; };
} private void QueryBgWorker_RunWorkerCompletedobject sender,
RunWorkerCompletedEventArgs e {
if e.Error = null {
MessageBox.Showe.Error.ToString; WriteQueryStatusError in querying Couldnt query;
} btnQueryImage.IsEnabled = true;
btnSelectQueryImage.IsEnabled = true; }
008 private void QueryImagestring queryImagePath
{ try
{ ImageList.ItemsSource = null;
if string.IsNullOrEmptyqueryImagePath {
QueryImageFullPath = queryImagePath; switch SelectedAlgo
{ case emAlgo.Undetermined:
MessageBox.ShowPilih Algoritma; return;
Universitas Sumatera Utara
case emAlgo.AccordSurf: QueryBgWorker.RunWorkerAsync;
break; default:
MessageBox.ShowAlgoritma Error; return;
} }
} catch Exception ex
{ MessageBox.Showex.ToString, Error, MessageBoxButton.OK,
MessageBoxImage.Error; }
} 010
private void QueryAccordSurfAlgostring queryImagePath {
WriteQueryStatusRetrieving...; SaveSurfSettingValues;
DTOs ListImageRecord searchImage = new ListImageRecord;
SurfSettings surfSetting = GetSurfSetting; string msg=;
if surfSetting.Algorithm == SurfAlgo.Linear {
Query SurfQuery4 surfQuery = new SurfQuery4;
_stopWatch = Stopwatch.StartNew; searchImage = surfQuery.QueryImagequeryImagePath, out msg, surfSetting;
_stopWatch.Stop; }
msg += string.Format {0} ms, _stopWatch.ElapsedMilliseconds; WriteQueryStatusmsg;
this.Dispatcher.Invoke = {
ImageList.ItemsSource = searchImage; lblTotalCount.Text = searchImage.Count.ToString;
}; }
Universitas Sumatera Utara
private void WriteQueryStatusstring msg {
var txtdispatcher = lblQueryStatus.Dispatcher; if txtdispatcher.CheckAccess
{ txtdispatcher.InvokeAction = lblQueryStatus.Text = msg;
} else
{ lblQueryStatus.Text = msg;
} }
endregion private int GetNumberOfImageFilesInIndexDirectory
{ FileInfo[] imageFiles = getFilesIndexDirectory, .gif|.jpg|.png|.bmp|.jpeg,
SearchOption.TopDirectoryOnly; int totalFileCount = imageFiles.Length;
return totalFileCount; }
endregion }
} 3. SurfDetailWindow.cs
using System.Windows; using System.Windows.Documents;
using SkripsiTepe.DTOs; using System.Windows.Media.Imaging;
using System; using System.Collections.Generic;
using System.ComponentModel; using System.Data;
using System.Drawing; using System.Text;
using System.Diagnostics; using System.IO;
using Gataulah; namespace SkripsiTepe
{ summary
Interaction logic for AccordSurf.xaml summary
public partial class AccordSurfWindow : Window {
Universitas Sumatera Utara
Bitmap returnBitmap; ListIPoint ipts1 = new ListIPoint;
ListIPoint ipts2 = new ListIPoint; public string ModelImagePath { get; set; }
public string ObserverImagePath { get; set; } public SurfSettings SurfSetting { get; set; }
public AccordSurfWindow {
InitializeComponent; }
public AccordSurfWindowstring modelImagePath, string observerImagePath, SurfSettings setting : this
{ this.ModelImagePath = modelImagePath;
this.ObserverImagePath = observerImagePath; this.SurfSetting = setting;
} private void AccordSurfCompareWin_Loadedobject sender, RoutedEventArgs e
{ ShowImage;
} protected void ShowImage
{ bool isModelImageMissing = string.IsNullOrEmptyModelImagePath;
bool isObserverImageMissing = string.IsNullOrEmptyObserverImagePath; if isModelImageMissing || isObserverImageMissing
return; Bitmap compareImagebmp = CompareAndDrawImagenew BitmapModelImagePath,
new BitmapObserverImagePath, SurfSetting;
if compareImagebmp = null {
MemoryStream ms = new MemoryStream; compareImagebmp.Savems, System.Drawing.Imaging.ImageFormat.Png;
ms.Position = 0; BitmapImage bi = new BitmapImage;
bi.BeginInit; bi.StreamSource = ms;
bi.EndInit; imageAccordCompare.Source = bi;
Universitas Sumatera Utara
} else
{ this.Close;
} }
private Bitmap CompareAndDrawImageBitmap modelImage, Bitmap observedImage, SurfSettings setting
{ Stopwatch watch1 = new Stopwatch;
Stopwatch watch2 = new Stopwatch; watch2.Start;
watch1.Reset; watch1.Start; double hessianThreshold = setting.HessianThresh.HasValue ?
setting.HessianThresh.Value : 500; float hessianThreshold2 = floathessianThreshold 1000000;
IntegralImage iimg1 = IntegralImage.FromImagemodelImage; ipts1 = FastHessian.getIpointshessianThreshold2, 5, 2, iimg1;
SurfDescriptor.DecribeInterestPointsipts1, false, false, iimg1; IntegralImage iimg2 = IntegralImage.FromImageobservedImage;
ipts2 = FastHessian.getIpointshessianThreshold2, 5, 2, iimg2; SurfDescriptor.DecribeInterestPointsipts2, false, false, iimg2;
watch1.Stop; Debug.WriteLineSurf Detection tooked {0} ms, watch1.ElapsedMilliseconds;
ListIPoint[] matches = Utils.getMatchesipts1, ipts2; if ipts1.Count 4 || ipts2.Count 4
{ MessageBox.ShowError, Interest Point tidak cukup;
return null; }
PaintSURFmodelImage,observedImage, matches; return returnBitmap;
Universitas Sumatera Utara
} private void PaintSURFBitmap img1, Bitmap img2, ListIPoint[] matches
{ Bitmap bmp = new Bitmapimg1.Width + img2.Width, Math.Maximg1.Height,
img2.Height; Graphics g = Graphics.FromImagebmp;
g.DrawImageimg1, 0, 0,img1.Width,img1.Height; g.DrawImageimg2, img1.Width, 0,img2.Width,img2.Height;
Pen redPen = new PenColor.Red; Pen bluePen = new PenColor.Blue;
Pen matchPen = new PenColor.Gold; Pen myPen;
int x = 0; for int i = 0; i matches.Length;i++
{ foreach IPoint ip in matches[i]
{ int S = 2 Convert.ToInt322.5f ip.scale;
int R = Convert.ToInt32S 2f; System.Drawing.Point pt = new System.Drawing.PointConvert.ToInt32ip.x,
Convert.ToInt32ip.y; System.Drawing.Point ptR = new System.Drawing.PointConvert.ToInt32R
Math.Cosip.orientation, Convert.ToInt32R Math.Sinip.orientation;
myPen = ip.laplacian 0 ? bluePen : redPen; g.DrawEllipsemyPen, x+pt.X - R, pt.Y - R, S, S;
g.DrawLinenew PenColor.FromArgb0, 255, 0, new System.Drawing.Pointx + pt.X, pt.Y, new System.Drawing.Pointx + pt.X + ptR.X, pt.Y + ptR.Y;
} x += img1.Width;
} for int i = 0; i matches[0].Count; i++
{ System.Drawing.Point pt1 = new
System.Drawing.PointConvert.ToInt32matches[0][i].x, Convert.ToInt32matches[0][i].y; System.Drawing.Point pt2 = new System.Drawing.Pointimg1.Width +
Convert.ToInt32matches[1][i].x, Convert.ToInt32matches[1][i].y; g.DrawLinematchPen, pt1, pt2;
}
Universitas Sumatera Utara
g.Dispose; returnBitmap = bmp;
} }
} 4. IntegralImage.cs
using System; using System.Collections.Generic;
using System.Linq; using System.Text;
using System.Drawing; namespace Gataulah
{
[Serializable] public class IntegralImage
{ const float cR = .2989f;
const float cG = .5870f; const float cB = .1140f;
internal float[,] matriks; public int lebar, tinggi;
public float this[int y, int x] {
get { return matriks[y, x]; } set { matriks[y, x] = value; }
} private IntegralImageint lebar, int tinggi
{ this.lebar = lebar;
this.tinggi = tinggi; this.matriks = new float[tinggi, lebar];
} 01
public static IntegralImage FromImageBitPeta image {
IntegralImage pic = new IntegralImageimage.lebar, image.tinggi;
Universitas Sumatera Utara
float barissum = 0; for int x = 0; x image.lebar; x++
{ Color c = image.GetPixelx, 0;
barissum += cR c.R + cG c.G + cB c.B 255f; pic[0, x] = barissum;
} for int y = 1; y image.tinggi; y++
{ barissum = 0;
for int x = 0; x image.lebar; x++ {
Color c = image.GetPixelx, y; barissum += cR c.R + cG c.G + cB c.B 255f;
pic[y, x] = barissum + pic[y - 1, x]; }
} return pic;
} public float BoxIntegralint baris, int col, int bariss, int cols
{ int r1 = Math.Minbaris, tinggi - 1;
int c1 = Math.Mincol, lebar - 1; int r2 = Math.Minbaris + bariss, tinggi - 1;
int c2 = Math.Mincol + cols, lebar - 1; float A = 0, B = 0, C = 0, D = 0;
if r1 = 0 c1 = 0 A = matriks[r1, c1]; if r1 = 0 c2 = 0 B = matriks[r1, c2];
if r2 = 0 c1 = 0 C = matriks[r2, c1]; if r2 = 0 c2 = 0 D = matriks[r2, c2];
return Math.Max0, A - B - C + D; }
public float HaarXint baris, int kolom, int size {
return BoxIntegralbaris - size 2, kolom, size, size 2 - 1 BoxIntegralbaris - size 2, kolom - size 2, size, size 2;
Universitas Sumatera Utara
} public float HaarYint baris, int kolom, int size
{ return BoxIntegralbaris, kolom - size 2, size 2, size
- 1 BoxIntegralbaris - size 2, kolom - size 2, size 2, size; }
} }
5. FastHessian.cs using System;