8aef25154d9429de7ff2c573fcfd6e94d9b245ba
[master-thesis.git] / Parasitemia / Parasitemia / GUI / GUI.fs
1 module Parasitemia.GUI.Main
2
3 open System.IO
4 open System.Linq
5 open System.Windows
6 open System.Windows.Media
7 open System.Windows.Markup
8 open System.Windows.Shapes
9 open System.Windows.Controls
10 open System.Diagnostics
11 open Microsoft.Win32 // For the common dialogs.
12
13 open Emgu.CV.WPF
14
15 open Config
16 open Types
17
18 let run (defaultConfig: Config) =
19 let app = new Application()
20 let mainWindow = Views.MainWindow()
21 let ctrl (name: string): 'a =
22 mainWindow.Root.FindName(name) :?> 'a
23
24 // Utils.log <- (fun m -> log mainWindow m)
25
26 let colorRBCHealthy = Brushes.YellowGreen
27 let colorRBCInfected = Brushes.Red
28
29 let state = State.State()
30 let mutable currentScale = 1.
31
32 let exit: MenuItem = ctrl "menuExit"
33 let saveFile: MenuItem = ctrl "menuSave"
34 let loadFile: MenuItem = ctrl "menuOpen"
35 let newFile: MenuItem = ctrl "menuNew"
36
37 let addSourceImage: MenuItem = ctrl "menuAddSourceImage"
38 let txtPatient: TextBox = ctrl "txtPatient"
39 let txtGlobalParasitemia: TextBox = ctrl "txtGlobalParasitemia"
40
41 let butStartAnalysis: Button = ctrl "butStartAnalysis"
42
43 let stackPreviews: StackPanel = ctrl "stackPreviews"
44
45 let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage"
46 let borderCurrentImage: Border = ctrl "borderCurrentImage"
47 let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage"
48 let txtImageInformation: TextBlock = ctrl "txtImageInformation"
49
50 let scrollRBC: ScrollViewer = ctrl "scrollRBC"
51 let stackRBC: StackPanel = ctrl "stackRBC"
52
53 // Initializations.
54
55 // Utils.
56 let extractRBCPreview (img: Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>) (rbc: RBC) : Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> =
57 let rbcWidth = rbc.size.Width
58 let rbcHeight = rbc.size.Height
59 img.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point(rbc.center.X - rbcWidth / 2. |> Utils.roundInt, rbc.center.Y - rbcHeight / 2. |> Utils.roundInt),
60 System.Drawing.Size(Utils.roundInt rbcWidth, Utils.roundInt rbcHeight)))
61
62 let setRBCFrameStyle (rbc: RBC) (frame: Views.RBCFrame) =
63 frame.Opacity <- if rbc.setManually || rbc.infected then 1. else 0.
64 let color = if rbc.infected then colorRBCInfected else colorRBCHealthy
65 frame.manuallyAdded.Visibility <- if rbc.setManually then Visibility.Visible else Visibility.Hidden
66 frame.manuallyAdded.Fill <- color
67 frame.border.Stroke <- color
68
69 let RBCFrameFromExisting (rbc: RBC) (frame: Views.RBCFrame) : Views.RBCFrame =
70 frame.Visibility <- Visibility.Visible
71 frame.Height <- rbc.size.Height
72 frame.Width <- rbc.size.Width
73 frame.Tag <- rbc
74 setRBCFrameStyle rbc frame
75 frame.border.StrokeThickness <- 1.
76 frame.lblRBCNumber.Content <- rbc.num
77 frame
78
79 let highlightRBCFrame (frame: Views.RBCFrame) (highlight: bool) =
80 let rbc = frame.Tag :?> RBC
81 if highlight
82 then
83 frame.border.StrokeThickness <- 3.
84 if not rbc.infected && not rbc.setManually then frame.Opacity <- 1.
85 else
86 frame.border.StrokeThickness <- 1.
87 if not rbc.infected && not rbc.setManually then frame.Opacity <- 0.
88
89 let zoomToRBC (rbc: RBC) =
90 scrollViewCurrentImage.ScrollToHorizontalOffset(rbc.center.X * currentScale - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
91 scrollViewCurrentImage.ScrollToVerticalOffset(rbc.center.Y * currentScale - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
92
93 let parasitemiaText (nbTotal: int, nbInfected: int) : string =
94 if nbTotal = 0
95 then
96 ""
97 else
98 let percent = 100. * (float nbInfected) / (float nbTotal)
99 sprintf "%.2f %% (%d / %d)" percent nbInfected nbTotal
100
101 let updateCurrentImageInformation () =
102 match state.CurrentImage with
103 | Some srcImg ->
104 let parasitemiaStr = parasitemiaText (state.ImageParasitemia srcImg)
105 txtImageInformation.Text <- sprintf "Parasitemia: %s" parasitemiaStr
106 | _ -> ()
107
108 let updateGlobalParasitemia () =
109 txtGlobalParasitemia.Text <- parasitemiaText state.GlobalParasitemia
110
111 let rec setAsInfected (rbc: RBC) (infected: bool) =
112 rbc.SetAsInfected infected
113 canvasCurrentImage.Children
114 |> Seq.cast<Views.RBCFrame>
115 |> Seq.iter
116 (fun frame ->
117 if (frame.Tag :?> RBC) = rbc
118 then
119 setRBCFrameStyle rbc frame)
120 updateRBCFramesPreview ()
121 updateCurrentImageInformation ()
122 updateGlobalParasitemia ()
123
124 and RBCFrame (rbc: RBC) : Views.RBCFrame =
125 let f = RBCFrameFromExisting rbc (Views.RBCFrame())
126 f.menuRBCSetAsHealthy.Click.AddHandler(fun obj args -> setAsInfected (f.Tag :?> RBC) false)
127 f.menuRBCSetAsInfected.Click.AddHandler(fun obj args -> setAsInfected (f.Tag :?> RBC) true)
128 f.ContextMenuOpening.AddHandler(
129 fun obj args ->
130 if (f.Tag :?> RBC).infected
131 then
132 f.menuRBCSetAsHealthy.Visibility <- Visibility.Visible
133 f.menuRBCSetAsInfected.Visibility <- Visibility.Collapsed
134 else
135 f.menuRBCSetAsHealthy.Visibility <- Visibility.Collapsed
136 f.menuRBCSetAsInfected.Visibility <- Visibility.Visible)
137
138 f.ContextMenuClosing.AddHandler(fun obj args -> if not f.IsMouseOver then highlightRBCFrame f false )
139 f.MouseEnter.AddHandler(fun obj args -> highlightRBCFrame f true)
140 f.MouseLeave.AddHandler(fun obj args -> if not f.grid.ContextMenu.IsOpen then highlightRBCFrame f false)
141 f
142
143 and updateRBCFramesPreview () =
144 match state.CurrentImage with
145 | Some srcImg ->
146 let mutable currentPreview = 0
147 for rbc in srcImg.rbcs |> List.filter (fun rbc -> rbc.infected) do
148 let previewInfected =
149 if currentPreview < stackRBC.Children.Count
150 then
151 RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame)
152 else
153 let f = RBCFrame rbc
154 f.MouseLeftButtonUp.AddHandler(fun obj args -> zoomToRBC ((obj :?> Views.RBCFrame).Tag :?> RBC))
155 stackRBC.Children.Add(f) |> ignore
156 f
157
158 currentPreview <- currentPreview + 1
159
160 previewInfected.Height <- stackRBC.ActualHeight
161 previewInfected.Width <- stackRBC.ActualHeight * rbc.size.Width / rbc.size.Height
162 previewInfected.border.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img rbc))
163
164 stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview)
165 | _ -> ()
166
167 let updateRBCFramesCurrent () =
168 match state.CurrentImage with
169 | Some srcImg ->
170 let mutable currentCanvas = 0
171 for rbc in srcImg.rbcs do
172 let frame =
173 if currentCanvas < canvasCurrentImage.Children.Count
174 then
175 RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame)
176 else
177 let f = RBCFrame rbc
178 canvasCurrentImage.Children.Add(f) |> ignore
179 f
180
181 currentCanvas <- currentCanvas + 1
182
183 Canvas.SetLeft(frame, rbc.center.X - rbc.size.Width / 2.)
184 Canvas.SetTop(frame, rbc.center.Y - rbc.size.Height / 2.)
185
186 for i in currentCanvas .. canvasCurrentImage.Children.Count - 1 do
187 canvasCurrentImage.Children.[i].Visibility <- Visibility.Hidden
188 | _ -> ()
189
190 // Operations.
191 let synchronizeState () =
192 state.PatientID <- txtPatient.Text
193
194 let setCurrentImage (srcImg: SourceImage) =
195 state.CurrentImage <- Some srcImg
196
197 // Highlight the preview.
198 stackPreviews.Children
199 |> Seq.cast<Views.ImageSourcePreview>
200 |> Seq.iter (fun preview -> preview.border.BorderThickness <- Thickness(if preview.lblImageNumber.Content = box srcImg.num then 3. else 0.))
201
202 canvasCurrentImage.Height <- float srcImg.img.Height
203 canvasCurrentImage.Width <- float srcImg.img.Width
204 canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img))
205
206 updateRBCFramesCurrent ()
207 updateRBCFramesPreview ()
208 updateCurrentImageInformation ()
209
210
211 let addPreview (srcImg: SourceImage) =
212 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
213 imgCtrl.lblImageNumber.Content <- srcImg.num
214 let width = 200
215 let height = srcImg.img.Height * width / srcImg.img.Width
216 imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
217 stackPreviews.Children.Add(imgCtrl) |> ignore
218 imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args -> setCurrentImage (state.SourceImages |> Seq.find (fun i -> box i.num = (obj :?> Views.ImageSourcePreview).lblImageNumber.Content)))
219
220 let updatePreviews () =
221 stackPreviews.Children.Clear ()
222 if state.SourceImages.Count() > 0
223 then
224 for srcImg in state.SourceImages do
225 addPreview srcImg
226 match state.CurrentImage with
227 | Some srcImg -> setCurrentImage srcImg
228 | _ -> ()
229
230 let updateGUI () =
231 txtPatient.Text <- state.PatientID
232 updatePreviews ()
233 updateGlobalParasitemia ()
234
235 exit.Click.AddHandler(fun obj args -> mainWindow.Root.Close())
236 saveFile.Click.AddHandler(fun obj args ->
237 synchronizeState ()
238 if state.FilePath = ""
239 then
240 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension, Filter = PiaZ.filter);
241 let res = dialog.ShowDialog()
242 if res.HasValue && res.Value
243 then
244 state.FilePath <- dialog.FileName
245 state.Save()
246 else
247 state.Save())
248
249 loadFile.Click.AddHandler(fun obj args ->
250 // TODO: if current state not saved and not empty, ask to save it.
251 let dialog = OpenFileDialog(Filter = PiaZ.filter)
252 let res = dialog.ShowDialog()
253 if res.HasValue && res.Value
254 then
255 state.FilePath <- dialog.FileName
256 state.Load()
257 updateGUI ())
258
259 newFile.Click.AddHandler(fun obj args ->
260 // TODO: if current state not saved and not empty, ask to save it.
261 state.Reset()
262 updateGUI())
263
264 addSourceImage.Click.AddHandler(fun obj args ->
265 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff")
266 let res = dialog.ShowDialog()
267 if res.HasValue && res.Value
268 then
269 let srcImg = state.AddSourceImage(dialog.FileName)
270 addPreview srcImg
271 updateGlobalParasitemia ()
272 if state.SourceImages.Count() = 1
273 then
274 setCurrentImage srcImg)
275
276 butStartAnalysis.Click.AddHandler(fun obj args ->
277 let results = ImageAnalysis.doMultipleAnalysis (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList) defaultConfig
278 for id, cells in results do
279 state.SetResult (int id) cells
280 updateGlobalParasitemia ()
281
282 // Refresh current image.
283 match state.CurrentImage with
284 | Some srcImg -> setCurrentImage srcImg
285 | _ -> ())
286
287 // Zoom on the current image.
288 let adjustCurrentImageBorders (deltaX: float) (deltaY: float) =
289 borderCurrentImage.BorderThickness <-
290 Thickness(
291 (scrollViewCurrentImage.ViewportWidth + deltaX) / 2.,
292 (scrollViewCurrentImage.ViewportHeight + deltaY) / 2.,
293 (scrollViewCurrentImage.ViewportWidth + deltaX) / 2.,
294 (scrollViewCurrentImage.ViewportHeight + deltaY) / 2.)
295
296 canvasCurrentImage.SizeChanged.AddHandler(fun obj args ->
297 let deltaX = args.NewSize.Width - args.PreviousSize.Width
298 let deltaY = args.NewSize.Height - args.PreviousSize.Height
299 if deltaX > 0.5 || deltaY > 0.5
300 then
301 adjustCurrentImageBorders 0.0 0.0
302 // Center the view at the center of the image initialy.
303 scrollViewCurrentImage.UpdateLayout()
304 scrollViewCurrentImage.ScrollToHorizontalOffset(borderCurrentImage.ActualWidth / 2. - scrollViewCurrentImage.ViewportWidth / 2.)
305 scrollViewCurrentImage.ScrollToVerticalOffset(borderCurrentImage.ActualHeight / 2. - scrollViewCurrentImage.ViewportHeight / 2.))
306
307 scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args ->
308 let deltaX = args.NewSize.Width - args.PreviousSize.Width
309 let deltaY = args.NewSize.Height - args.PreviousSize.Height
310 adjustCurrentImageBorders deltaX deltaY
311 scrollViewCurrentImage.ScrollToHorizontalOffset(scrollViewCurrentImage.HorizontalOffset + deltaX / 8.)
312 scrollViewCurrentImage.ScrollToVerticalOffset(scrollViewCurrentImage.VerticalOffset + deltaY / 8.))
313
314 let mutable maxScale = 4.
315 let mutable minScale = 0.25
316 let currentImageScaleTransform = ScaleTransform()
317 canvasCurrentImage.LayoutTransform <- currentImageScaleTransform
318 borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args ->
319 let scaleFactor = if args.Delta > 0 then 2.0 else 0.5
320 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale
321 then
322 let previousScale = currentScale
323 currentScale <-
324 let newScale = currentScale * scaleFactor
325 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
326 let realScaleFactor = currentScale / previousScale
327
328 let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left
329 let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top
330
331 //canvasCurrentImage.Margin <- Thickness(canvasCurrentImage.Margin.Top * realScaleFactor)
332 currentImageScaleTransform.ScaleX <- currentScale
333 currentImageScaleTransform.ScaleY <- currentScale
334
335 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
336 scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
337 args.Handled <- true)
338
339 // Pan on the current image.
340 let mutable scrollStartPosition = Point(0., 0.)
341 let mutable scrollStartOffsetX = 0.
342 let mutable scrollStartOffsetY = 0.
343 borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args ->
344 scrollStartPosition <- args.GetPosition(scrollViewCurrentImage)
345 scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset
346 scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset
347 borderCurrentImage.Cursor <- Input.Cursors.ScrollAll
348 borderCurrentImage.CaptureMouse() |> ignore
349 args.Handled <- true)
350
351 borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args ->
352 if borderCurrentImage.IsMouseCaptured
353 then
354 let position = args.GetPosition(scrollViewCurrentImage)
355 let deltaX = scrollStartPosition.X - position.X
356 let deltaY = scrollStartPosition.Y - position.Y
357 scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
358 scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
359 args.Handled <- true)
360
361 borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args ->
362 if borderCurrentImage.IsMouseCaptured
363 then
364 borderCurrentImage.Cursor <- Input.Cursors.Arrow
365 borderCurrentImage.ReleaseMouseCapture()
366 args.Handled <- true)
367
368 // display mainWindow img
369 mainWindow.Root.Show()
370 app.Run()