7d72e729cfb2e96978493ab643c717f17b87e098
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Analysis.fs
1 module Parasitemia.GUI.Analysis
2
3 open System
4 open System.IO
5 open System.Linq
6 open System.Windows
7 open System.Windows.Media
8 open System.Windows.Markup
9 open System.Windows.Shapes
10 open System.Windows.Controls
11 open System.Diagnostics
12 open Microsoft.Win32 // For the common dialogs.
13
14 open Emgu.CV.WPF
15
16 open UnitsOfMeasure
17 open Config
18 open Types
19
20 let showWindow (parent: Window) (state: State.State) : bool =
21 let window = Views.AnalysisWindow()
22 window.Root.Owner <- parent
23 window.Root.Left <- parent.Left + parent.ActualWidth / 2. - window.Root.Width / 2.
24 window.Root.Top <- parent.Top + parent.ActualHeight / 2. - window.Root.Height / 2.
25
26 let ctrl (name: string): 'a = window.Root.FindName(name) :?> 'a
27
28 let butClose: Button = ctrl "butClose"
29 let butStart: Button = ctrl "butStart"
30
31 let stackImagesSourceSelection: StackPanel = ctrl "stackImagesSourceSelection"
32 let progressBar: ProgressBar = ctrl "progress"
33 let textLog: TextBlock = ctrl "textLog"
34 let scrollLog: ScrollViewer = ctrl "scrollLog"
35
36 Utils.log <- (fun mess -> window.Root.Dispatcher.Invoke(fun () ->
37 textLog.Inlines.Add(Documents.Run(mess))
38 textLog.Inlines.Add(Documents.LineBreak())
39 scrollLog.ScrollToBottom()))
40
41 let minPPI = 1.
42 let maxPPI = 10e6
43 let parseAndValidatePPI (input: string) : float option =
44 let res = ref 0.
45 if Double.TryParse(input, res) && !res >= minPPI && !res <= maxPPI
46 then Some !res
47 else None
48
49 let monitor = Object()
50 let mutable atLeastOneAnalysisPerformed = false
51 let mutable analysisPerformed = false
52 let mutable analysisCancelled = false
53
54 let updateSourceImages () =
55 stackImagesSourceSelection.Children.Clear()
56 let width = int stackImagesSourceSelection.ActualWidth
57 for srcImg in state.SourceImages do
58 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg, Margin = Thickness(3.))
59
60 let updateResolution () =
61 match parseAndValidatePPI imageSourceSelection.txtResolution.Text with
62 | Some resolution -> srcImg.config.Parameters <- { srcImg.config.Parameters with resolution = resolution * 1.<ppi> }
63 | None -> ()
64
65 imageSourceSelection.txtImageNumber.Text <- srcImg.num.ToString()
66 let height = srcImg.img.Height * width / srcImg.img.Width
67 imageSourceSelection.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
68 imageSourceSelection.chkSelection.IsChecked <- Nullable<bool>(srcImg.dateLastAnalysis.Ticks = 0L)
69 imageSourceSelection.lblDateLastAnalysis.Content <- if srcImg.dateLastAnalysis.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis.ToString()
70
71 imageSourceSelection.txtResolution.Text <- srcImg.config.Parameters.resolution.ToString()
72 imageSourceSelection.menuZoom50X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "230000"; updateResolution ())
73 imageSourceSelection.menuZoom100X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "460000"; updateResolution ())
74
75 imageSourceSelection.txtResolution.PreviewTextInput.AddHandler(fun obj args ->
76 let text = imageSourceSelection.txtResolution.Text + args.Text
77 args.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
78
79 imageSourceSelection.imagePreview.MouseLeftButtonDown.AddHandler(fun obj args ->
80 let checkbox = imageSourceSelection.chkSelection
81 checkbox.IsChecked <- Nullable<bool>(not (checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
82
83 imageSourceSelection.txtResolution.LostFocus.AddHandler(fun obj args -> updateResolution ())
84
85 stackImagesSourceSelection.Children.Add(imageSourceSelection) |> ignore
86
87 butClose.Click.AddHandler(fun obj args -> window.Root.Close())
88
89 butStart.Click.AddHandler(fun obj args ->
90 let imagesToProcess = [
91 for imageSelection in stackImagesSourceSelection.Children |> Seq.cast<Views.ImageSourceSelection> do
92 let chk = imageSelection.chkSelection.IsChecked
93 if chk.HasValue && chk.Value
94 then
95 let srcImg = imageSelection.Tag :?> SourceImage
96 yield srcImg.num.ToString(), srcImg.config, srcImg.img ]
97
98 if imagesToProcess.IsEmpty
99 then
100 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
101 else
102 analysisPerformed <- false
103 butStart.IsEnabled <- false
104 butClose.Content <- "Abort"
105 async {
106 let results =
107 ImageAnalysis.doMultipleAnalysis
108 imagesToProcess
109 (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
110
111 lock monitor (
112 fun() ->
113 if not analysisCancelled
114 then
115 for id, cells in results do
116 state.SetResult (int id) cells
117
118 window.Root.Dispatcher.Invoke(fun () ->
119 butStart.IsEnabled <- true
120 butClose.Content <- "Close"
121 updateSourceImages ())
122
123 Utils.log "All analyses terminated successfully"
124 atLeastOneAnalysisPerformed <- true
125 analysisPerformed <- true)
126 } |> Async.Start)
127
128 window.Root.Loaded.AddHandler(fun obj args -> updateSourceImages ())
129
130 window.Root.ShowDialog() |> ignore
131
132 lock monitor (fun () ->
133 if not analysisPerformed
134 then
135 analysisCancelled <- true
136 atLeastOneAnalysisPerformed)