-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
124 lines (106 loc) · 4.93 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BlurContrastBrightnessImage
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private BitmapImage originalImage;
private byte[] originalImageBytes;
private byte[] processedImageBytes;
private int W, H;
public MainWindow() {
InitializeComponent();
}
// open file from the finder
private void openFileButton_Click(object sender, RoutedEventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image files (*.jpg, *.jpeg)|*.jpg; *.jpeg";
if (openFileDialog.ShowDialog() == true) {
showImage(openFileDialog.FileName);
slidersGrid.IsEnabled = true;
_blobs = null;
}
}
// show image on the window
private void showImage(string filename) {
originalImage = ImageConvertor.FilenameToImage(filename);
originalImageBytes = ImageConvertor.ImageToByteArray(filename);
originalPanel.Source = originalImage;
W = (int)originalImage.PixelHeight;
H = (int)originalImage.PixelWidth;
ImageProcessing.setHeightAndLength(W, H);
}
private void Button_Click(object sender, RoutedEventArgs e) {
processedImageBytes = ImageProcessing.markEdges(originalImageBytes);
grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 4);
}
private void Button_Click_3(object sender, RoutedEventArgs e) {
processedImageBytes = ImageProcessing.getDifferenceOfGaussins(originalImageBytes);
grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 1);
}
List<double[]> _blobs = null;
private void gaussSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
if (e.NewValue == 1)
return;
if (_blobs == null) {
_blobs = ImageProcessing.getBlobCoordsUsingLaplacianKernel(originalImageBytes);
}
drawPanel.Children.Clear();
grayscalePanel.Source = originalImage;
List<double[]> Blobs = _blobs.OrderByDescending(x => x[3]).Take((int)e.NewValue).ToList();
foreach (double[] coord in Blobs) {
Ellipse el = new Ellipse();
el.Stroke = System.Windows.Media.Brushes.Red;
el.StrokeThickness = 1.3;
double r = coord[2] * originalPanel.ActualWidth;
el.Width = 2 * r;
el.Height = 2 * r;
Canvas.SetLeft(el, coord[0] * originalPanel.ActualWidth - r);
Canvas.SetTop(el, coord[1] * originalPanel.ActualHeight - r);
drawPanel.Children.Add(el);
}
drawPanel.Visibility = System.Windows.Visibility.Visible;
//if (e.NewValue <= 1)
// return;
//processedImageBytes = ImageProcessing.getDifferenceOfGaussins(originalImageBytes, (int) e.NewValue);
//grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 1);
}
private void Button_Click_1(object sender, RoutedEventArgs e) {
System.DateTime start = System.DateTime.Now;
drawPanel.Children.Clear();
grayscalePanel.Source = originalImage;
List<double[]> Blobs = ImageProcessing.getBlobCoordsUsingLaplacianKernel(originalImageBytes);
foreach (double[] coord in Blobs) {
Ellipse el = new Ellipse();
el.Stroke = System.Windows.Media.Brushes.Red;
el.StrokeThickness = 1.3;
double r = coord[2] * originalPanel.ActualWidth;
el.Width = 2 * r;
el.Height = 2 * r;
Canvas.SetLeft(el, coord[0] * originalPanel.ActualWidth - r);
Canvas.SetTop(el, coord[1] * originalPanel.ActualHeight - r);
drawPanel.Children.Add(el);
}
drawPanel.Visibility = System.Windows.Visibility.Visible;
System.DateTime finish = System.DateTime.Now;
MessageBox.Show(String.Format("It took us {0} sec to find blobs", (finish - start).TotalSeconds.ToString()));
}
}
}