Add a DPI calculator to help to find the correct image resolution.
[master-thesis.git] / Parasitemia / ParasitemiaUI / Analysis.fs
1 module ParasitemiaUI.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 ParasitemiaCore.UnitsOfMeasure
17 open ParasitemiaCore.Config
18
19 open Types
20
21 let showWindow (parent: Window) (state: State.State) : bool =
22 let win = Views.AnalysisWindow()
23 win.Root.Owner <- parent
24 win.Root.Left <- parent.Left + parent.ActualWidth / 2. - win.Root.Width / 2.
25 win.Root.Top <- parent.Top + parent.ActualHeight / 2. - win.Root.Height / 2.
26
27 let logListener =
28 { new Logger.IListener with
29 member this.NewEntry severity mess =
30 win.Root.Dispatcher.Invoke(fun () ->
31 win.textLog.Inlines.Add(Documents.Run(mess))
32 win.textLog.Inlines.Add(Documents.LineBreak())
33 win.scrollLog.ScrollToBottom()) }
34
35 Logger.Log.AddListener(logListener)
36
37 let minPPI = 1.
38 let maxPPI = 10e6
39 let parseAndValidatePPI (input: string) : float option =
40 match Double.TryParse(input) with
41 | true, value when value >= minPPI && value <= maxPPI -> Some value
42 | _ -> None
43
44 let monitor = Object()
45 let mutable atLeastOneAnalysisPerformed = false
46 let mutable analysisPerformed = false
47 let mutable analysisCancelled = false
48
49 let updateSourceImages () =
50 win.stackSourceImagesSelection.Children.Clear()
51 let width = int win.stackSourceImagesSelection.ActualWidth
52 for srcImg in state.SourceImages do
53 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg, Margin = Thickness(3.))
54 imageSourceSelection.Tag <- srcImg
55
56 imageSourceSelection.txtImageNumber.Text <- srcImg.num.ToString()
57 let height = srcImg.img.Height * width / srcImg.img.Width
58 imageSourceSelection.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
59 imageSourceSelection.chkSelection.IsChecked <- Nullable<bool>(srcImg.dateLastAnalysis.Ticks = 0L)
60 imageSourceSelection.lblDateLastAnalysis.Content <- if srcImg.dateLastAnalysis.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis.ToString()
61
62 imageSourceSelection.txtResolution.Text <- if srcImg.dateLastAnalysis.Ticks = 0L then "" else srcImg.config.Parameters.resolution.ToString()
63 imageSourceSelection.menuZoom50X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "230000")
64 imageSourceSelection.menuZoom100X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "460000")
65
66 imageSourceSelection.butDPICalculator.Click.AddHandler(fun obj args ->
67 match DPICalculator.showWindow win.Root with
68 | Some resolution -> imageSourceSelection.txtResolution.Text <- resolution.ToString()
69 | None -> ())
70
71 imageSourceSelection.txtResolution.PreviewTextInput.AddHandler(fun obj args ->
72 let text = imageSourceSelection.txtResolution.Text + args.Text
73 args.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
74
75 imageSourceSelection.imagePreview.MouseLeftButtonDown.AddHandler(fun obj args ->
76 let checkbox = imageSourceSelection.chkSelection
77 checkbox.IsChecked <- Nullable<bool>(not (checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
78
79 win.stackSourceImagesSelection.Children.Add(imageSourceSelection) |> ignore
80
81 // Get the new parameters for each image. If an error occurs then 'None' is returned and a message box is displayed.
82 // The boolean is 'true' if the image is selected (checked).
83 let getInputImagesParameters () : (SourceImage * bool * Parameters) list option =
84 let sourceImagesControls = win.stackSourceImagesSelection.Children |> Seq.cast<Views.ImageSourceSelection>
85 let parameters = seq {
86 for srcImgCtrl in sourceImagesControls do
87 let srcImg = srcImgCtrl.Tag :?> SourceImage
88 let isChecked = srcImgCtrl.chkSelection.IsChecked
89 match parseAndValidatePPI srcImgCtrl.txtResolution.Text with
90 | Some resolution ->
91 yield Some (srcImg, isChecked.HasValue && isChecked.Value, { srcImg.config.Parameters with resolution = resolution * 1.<ppi> })
92 | None ->
93 MessageBox.Show(sprintf "No resolution defined for the image number %d" srcImg.num, "No resolution defined", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
94 yield None } |> Seq.takeWhile (fun e -> e.IsSome) |> Seq.map (fun e -> e.Value) |> List.ofSeq
95 if parameters.Count() <> sourceImagesControls.Count()
96 then None
97 else Some parameters
98
99 win.butClose.Click.AddHandler(fun obj args -> win.Root.Close())
100
101 win.butStart.Click.AddHandler(fun obj args ->
102 match getInputImagesParameters () with
103 | Some imagesParameters ->
104 let imagesToProcess = [
105 for srcImg, selected, parameters in imagesParameters do
106 srcImg.config.Parameters <- parameters // Save parameters.
107 if selected
108 then yield srcImg.num.ToString(), srcImg.config, srcImg.img ]
109
110 if imagesToProcess.IsEmpty
111 then
112 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
113 else
114 win.stackSourceImagesSelection.IsEnabled <- false
115 analysisPerformed <- false
116 win.butStart.IsEnabled <- false
117 win.butClose.Content <- "Abort"
118
119 async {
120 let maybeResults =
121 ParasitemiaCore.Analysis.doMultipleAnalysis
122 imagesToProcess
123 (Some (fun progress -> win.Root.Dispatcher.Invoke(fun () -> win.progress.Value <- float progress); not analysisCancelled))
124
125 lock monitor (
126 fun() ->
127 match maybeResults with
128 | Some results ->
129 for id, cells in results do
130 state.SetResult (int id) cells
131
132 win.Root.Dispatcher.Invoke(fun () ->
133 win.stackSourceImagesSelection.IsEnabled <- true
134 win.butStart.IsEnabled <- true
135 win.butClose.Content <- "Close"
136 updateSourceImages ())
137
138 Logger.Log.User("All analyses terminated successfully")
139 atLeastOneAnalysisPerformed <- true
140 analysisPerformed <- true
141 | None -> ())
142 } |> Async.Start
143 | _ -> ())
144
145 win.Root.Loaded.AddHandler(fun obj args -> updateSourceImages ())
146
147 win.Root.ShowDialog() |> ignore
148
149 Logger.Log.RmListener(logListener)
150
151 lock monitor (fun () ->
152 if not analysisPerformed
153 then
154 analysisCancelled <- true
155 atLeastOneAnalysisPerformed)