2132363b1a7cda6378803185d4e0a11445084c1b
1
module Parasitemia.GUI.Main
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.
19 let run (defaultConfig
: Config) =
20 let app = new Application()
21 let mainWindow = Views.MainWindow()
22 let ctrl (name
: string): 'a =
23 mainWindow.Root.FindName(name) :?> 'a
25 let colorRBCHealthy = Brushes.YellowGreen
26 let colorRBCInfected = Brushes.Red
28 let state = State.State()
29 let mutable currentScale = 1.
30 let mutable displayHealthy = false
32 let menuExit: MenuItem = ctrl "menuExit"
33 let menuSaveFile: MenuItem = ctrl "menuSave"
34 let menuLoadFile: MenuItem = ctrl "menuOpen"
35 let menuNewFile: MenuItem = ctrl "menuNew"
36 let menuAddSourceImage: MenuItem = ctrl "menuAddSourceImage"
37 let menuStartAnalysis: MenuItem = ctrl "menuStartAnalysis"
38 let menuHightlightRBC: MenuItem = ctrl "menuHightlightRBC"
40 let txtPatient: TextBox = ctrl "txtPatient"
41 let txtGlobalParasitemia: TextBox = ctrl "txtGlobalParasitemia"
43 let stackPreviews: StackPanel = ctrl "stackPreviews"
45 let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage"
46 let borderCurrentImage: Border = ctrl "borderCurrentImage"
47 let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage"
48 let txtImageInformation: TextBlock = ctrl "txtImageInformation"
50 let scrollRBC: ScrollViewer = ctrl "scrollRBC"
51 let stackRBC: StackPanel = ctrl "stackRBC"
54 menuHightlightRBC.IsChecked <- displayHealthy
57 let extractRBCPreview (img
: Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte
>) (rbc
: RBC) : Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte
> =
58 let rbcWidth = rbc
.size
.Width
59 let rbcHeight = rbc
.size
.Height
60 let x = rbc
.center
.X - rbcWidth / 2. |> Utils.roundInt
61 let y = rbc
.center
.Y - rbcHeight / 2. |> Utils.roundInt
62 let w = Utils.roundInt
rbcWidth
63 let h = Utils.roundInt
rbcHeight
64 //Utils.dprintfn "w: %A, h: %A, cx: %A, cy: %A, img.w: %A, img.h: %A" w h x y img.Width img.Height
65 img
.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point((if x < 0 then 0 else x), (if y < 0 then 0 else y)),
66 System.Drawing.Size((if x + w >= img
.Width then img
.Width - x else w),
67 (if y + h >= img
.Height then img
.Height - y else h))))
69 let setRBCFrameStyle (rbc
: RBC) (frame
: Views.RBCFrame) =
70 frame
.Opacity <- if displayHealthy || rbc.setManually
|| rbc.infected
then 1. else 0.
71 let color = if rbc.infected
then colorRBCInfected else colorRBCHealthy
72 frame
.manuallyAdded
.Visibility <- if rbc.setManually
then Visibility.Visible else Visibility.Hidden
73 frame
.manuallyAdded
.Fill <- color
74 frame
.border
.Stroke <- color
76 let RBCFrameFromExisting (rbc: RBC) (frame
: Views.RBCFrame) : Views.RBCFrame =
77 frame
.Visibility <- Visibility.Visible
78 frame
.Height <- rbc.size
.Height
79 frame
.Width <- rbc.size
.Width
81 setRBCFrameStyle rbc frame
82 frame
.border
.StrokeThickness <- 1.
83 frame
.txtRBCNumber
.Text <- rbc.num
.ToString()
86 let highlightRBCFrame (frame
: Views.RBCFrame) (highlight
: bool) =
87 let rbc = frame
.Tag :?> RBC
90 frame
.border
.StrokeThickness <- 3.
91 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 1.
93 frame
.border
.StrokeThickness <- 1.
94 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 0.
96 let zoomToRBC (rbc: RBC) =
97 scrollViewCurrentImage.ScrollToHorizontalOffset(rbc.center
.X * currentScale - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
98 scrollViewCurrentImage.ScrollToVerticalOffset(rbc.center
.Y * currentScale - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
100 let parasitemiaText (nbTotal
: int, nbInfected
: int) : string =
105 let percent = 100. * (float nbInfected) / (float nbTotal)
106 sprintf
"%.1f %% (%d / %d)" percent nbInfected nbTotal
108 let updateCurrentImageInformation () =
109 match state.CurrentImage with
111 let parasitemiaStr = parasitemiaText (state.ImageParasitemia srcImg
)
112 txtImageInformation.Inlines.Clear()
113 txtImageInformation.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold))
114 txtImageInformation.Inlines.Add(parasitemiaStr)
115 txtImageInformation.Inlines.Add(Documents.LineBreak())
117 txtImageInformation.Inlines.Add(Documents.Run("Average erytrocyte diameter: ", FontWeight = FontWeights.Bold))
118 txtImageInformation.Inlines.Add(Documents.Run(string (Utils.roundInt
<| 2. * srcImg
.rbcRadius
)))
119 txtImageInformation.Inlines.Add(Documents.Run(" px"))
120 txtImageInformation.Inlines.Add(Documents.LineBreak())
122 txtImageInformation.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold))
123 txtImageInformation.Inlines.Add(Documents.Run(if srcImg
.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToLocalTime().ToString()))
126 let updateGlobalParasitemia () =
127 txtGlobalParasitemia.Text <- parasitemiaText state.GlobalParasitemia
129 let rec setAsInfected
(rbc: RBC) (infected
: bool) =
130 state.SetAsInfected rbc infected
131 canvasCurrentImage.Children
132 |> Seq.cast
<Views.RBCFrame>
135 if (frame
.Tag :?> RBC) = rbc
137 setRBCFrameStyle rbc frame
)
138 updateRBCFramesPreview
()
139 updateCurrentImageInformation ()
140 updateGlobalParasitemia ()
142 and RBCFrame (rbc: RBC) : Views.RBCFrame =
143 let f = RBCFrameFromExisting rbc (Views.RBCFrame())
144 f.menuRBCSetAsHealthy
.Click.AddHandler(fun obj args
-> setAsInfected
(f.Tag :?> RBC) false)
145 f.menuRBCSetAsInfected
.Click.AddHandler(fun obj args
-> setAsInfected
(f.Tag :?> RBC) true)
146 f.ContextMenuOpening.AddHandler(
148 if (f.Tag :?> RBC).infected
150 f.menuRBCSetAsHealthy
.Visibility <- Visibility.Visible
151 f.menuRBCSetAsInfected
.Visibility <- Visibility.Collapsed
153 f.menuRBCSetAsHealthy
.Visibility <- Visibility.Collapsed
154 f.menuRBCSetAsInfected
.Visibility <- Visibility.Visible)
156 f.ContextMenuClosing.AddHandler(fun obj args
-> if not
f.IsMouseOver then highlightRBCFrame f false )
157 f.MouseEnter.AddHandler(fun obj args
-> highlightRBCFrame f true)
158 f.MouseLeave.AddHandler(fun obj args
-> if not
f.grid
.ContextMenu.IsOpen then highlightRBCFrame f false)
161 and updateRBCFramesPreview
() =
162 match state.CurrentImage with
164 let mutable currentPreview = 0
165 for rbc in srcImg.rbcs
|> List.filter
(fun rbc -> displayHealthy || rbc.infected
) do
166 let previewInfected =
167 if currentPreview < stackRBC.Children.Count
169 RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame)
172 f.MouseLeftButtonUp.AddHandler(fun obj args
-> zoomToRBC (f.Tag :?> RBC))
173 stackRBC.Children.Add(f) |> ignore
176 currentPreview <- currentPreview + 1
178 previewInfected.Height <- stackRBC.ActualHeight
179 previewInfected.Width <- stackRBC.ActualHeight * rbc.size
.Width / rbc.size
.Height
180 previewInfected.border
.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img
rbc))
182 stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview)
185 let updateRBCFramesCurrent () =
186 match state.CurrentImage with
188 let mutable currentCanvas = 0
189 for rbc in srcImg.rbcs
do
191 if currentCanvas < canvasCurrentImage.Children.Count
193 RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame)
196 canvasCurrentImage.Children.Add(f) |> ignore
199 currentCanvas <- currentCanvas + 1
201 Canvas.SetLeft(frame, rbc.center
.X - rbc.size
.Width / 2.)
202 Canvas.SetTop(frame, rbc.center
.Y - rbc.size
.Height / 2.)
204 for i
in currentCanvas .. canvasCurrentImage.Children.Count - 1 do
205 canvasCurrentImage.Children.[i
].Visibility <- Visibility.Hidden
208 let saveCurrentDocument () =
209 if state.FilePath = ""
211 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension
, Filter = PiaZ.filter
);
212 let res = dialog.ShowDialog()
213 if res.HasValue && res.Value
215 state.FilePath <- dialog.FileName
220 // Ask the use to save the current document if neccessary.
221 let askSaveCurrent () =
222 if state.AlteredSinceLastSave
224 match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with
225 | MessageBoxResult.Yes -> saveCurrentDocument ()
228 let updateCurrentImage () =
229 match state.CurrentImage with
231 // Highlight the preview.
232 stackPreviews.Children
233 |> Seq.cast
<Views.ImageSourcePreview>
234 |> Seq.iter
(fun preview
-> preview
.border
.BorderThickness <- Thickness(if preview
.Tag = (srcImg :> Object) then 3. else 0.))
236 canvasCurrentImage.Height <- float srcImg.img
.Height
237 canvasCurrentImage.Width <- float srcImg.img
.Width
238 canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img
))
240 updateRBCFramesCurrent ()
241 updateRBCFramesPreview
()
242 updateCurrentImageInformation ()
244 stackRBC.Children.Clear()
245 canvasCurrentImage.Children.Clear()
246 canvasCurrentImage.Background <- Brushes.Black
248 let setCurrentImage (srcImg: SourceImage) =
249 if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg
251 state.CurrentImage <- Some srcImg
252 updateCurrentImage ()
254 let updateViewportPreview () =
255 for preview
in stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> do
256 let srcImg = preview
.Tag :?> SourceImage
257 if Some srcImg = state.CurrentImage then
258 preview
.viewport
.Visibility <- Visibility.Visible
260 let canvasWidth = canvasCurrentImage.ActualWidth * currentScale
261 let canvasHeight = canvasCurrentImage.ActualHeight * currentScale
262 let previewWidth = (preview
.ActualWidth - preview
.BorderThickness.Left - preview
.BorderThickness.Right)
263 let previewHeight = (preview
.ActualHeight - preview
.BorderThickness.Top - preview
.BorderThickness.Bottom)
265 let marginLeft = previewWidth * (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Left) / canvasWidth - 2.
266 let marginRight = previewWidth * (canvasWidth - (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Right) - scrollViewCurrentImage.ViewportWidth) / canvasWidth - 2.
267 let marginTop = previewHeight * (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Top) / canvasHeight - 2.
268 let marginBottom = previewHeight * (canvasHeight - (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Bottom) - scrollViewCurrentImage.ViewportHeight) / canvasHeight - 2.
270 preview
.viewport
.Margin <-
277 preview
.viewport
.Visibility <- Visibility.Hidden
279 let addPreview (srcImg: SourceImage) =
280 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
282 imgCtrl.menuRemoveImage
.Click.AddHandler(fun obj args
->
283 stackPreviews.Children.Remove(imgCtrl)
284 let srcImg = imgCtrl.Tag :?> SourceImage
285 let currentRemoved = Some srcImg = state.CurrentImage
286 state.RemoveSourceImage srcImg
290 stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> |> Seq.iter
(fun imgPreview
-> imgPreview
.txtImageNumber
.Text <- (imgPreview
.Tag :?> SourceImage).num
.ToString()))
292 imgCtrl.Tag <- srcImg
293 imgCtrl.txtImageNumber
.Text <- srcImg.num
.ToString()
295 let height = srcImg.img
.Height * width / srcImg.img
.Width
296 imgCtrl.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
297 stackPreviews.Children.Add(imgCtrl) |> ignore
299 // Zoom to a mouse position into the control 'imgCtrl'.
300 let zoomTo (mousePos
: Point) =
301 let canvasW = canvasCurrentImage.ActualWidth * currentScale
302 let canvasH = canvasCurrentImage.ActualHeight * currentScale
303 let centerX = (mousePos
.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW
304 let centerY = (mousePos
.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH
305 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
306 scrollViewCurrentImage.ScrollToVerticalOffset(centerY - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
308 imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args
->
309 setCurrentImage (state.SourceImages |> Seq.find
(fun srcImg -> (srcImg :> Object) = imgCtrl.Tag))
310 imgCtrl.UpdateLayout()
311 zoomTo (args
.GetPosition(imgCtrl))
312 imgCtrl.CaptureMouse() |> ignore
)
314 imgCtrl.MouseMove.AddHandler(fun obj args
->
315 if imgCtrl.IsMouseCaptured
317 zoomTo (args
.GetPosition(imgCtrl)))
319 imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args
->
320 if imgCtrl.IsMouseCaptured
322 imgCtrl.ReleaseMouseCapture())
325 let updatePreviews () =
326 stackPreviews.Children.Clear ()
327 for srcImg in state.SourceImages do
329 updateCurrentImage ()
332 txtPatient.Text <- state.PatientID
334 updateGlobalParasitemia ()
336 txtPatient.TextChanged.AddHandler(fun obj args
-> state.PatientID <- txtPatient.Text)
338 menuExit.Click.AddHandler(fun obj args
->
340 mainWindow.Root.Close())
342 menuSaveFile.Click.AddHandler(fun obj args
-> saveCurrentDocument ())
344 menuLoadFile.Click.AddHandler(fun obj args
->
345 // TODO: if current state not saved and not empty, ask to save it.
346 let dialog = OpenFileDialog(Filter = PiaZ.filter
)
347 let res = dialog.ShowDialog()
348 if res.HasValue && res.Value
351 state.FilePath <- dialog.FileName
355 menuNewFile.Click.AddHandler(fun obj args
->
360 menuAddSourceImage.Click.AddHandler(fun obj args
->
361 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff")
362 let res = dialog.ShowDialog()
363 if res.HasValue && res.Value
365 let srcImg = state.AddSourceImage(dialog.FileName)
367 updateGlobalParasitemia ()
368 if state.SourceImages.Count() = 1
370 updateCurrentImage ())
372 menuStartAnalysis.Click.AddHandler(fun obj args
->
373 if Analysis.showWindow
mainWindow.Root state defaultConfig
375 updateGlobalParasitemia ()
376 updateCurrentImage ())
378 menuHightlightRBC.Click.AddHandler(fun obj args
->
379 displayHealthy <- menuHightlightRBC.IsChecked
380 updateRBCFramesPreview
()
381 updateRBCFramesCurrent ())
383 // Zoom on the current image.
384 let adjustCurrentImageBorders (deltaX
: float) (deltaY
: float) =
385 borderCurrentImage.BorderThickness <-
387 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
388 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.,
389 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
390 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.)
392 canvasCurrentImage.SizeChanged.AddHandler(fun obj args
->
393 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
394 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
395 if deltaX > 0.5 || deltaY > 0.5
397 adjustCurrentImageBorders 0.0
0.0
398 // Center the view at the center of the image initialy.
399 scrollViewCurrentImage.UpdateLayout()
400 scrollViewCurrentImage.ScrollToHorizontalOffset(borderCurrentImage.ActualWidth / 2. - scrollViewCurrentImage.ViewportWidth / 2.)
401 scrollViewCurrentImage.ScrollToVerticalOffset(borderCurrentImage.ActualHeight / 2. - scrollViewCurrentImage.ViewportHeight / 2.))
403 scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args
->
404 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
405 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
406 adjustCurrentImageBorders deltaX deltaY
407 scrollViewCurrentImage.ScrollToHorizontalOffset(scrollViewCurrentImage.HorizontalOffset + deltaX / 8.)
408 scrollViewCurrentImage.ScrollToVerticalOffset(scrollViewCurrentImage.VerticalOffset + deltaY / 8.))
410 let mutable maxScale = 4.
411 let mutable minScale = 0.25
412 let currentImageScaleTransform = ScaleTransform()
413 canvasCurrentImage.LayoutTransform <- currentImageScaleTransform
414 borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args
->
415 let scaleFactor = if args
.Delta > 0 then 2.0 else 0.5
416 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale
418 let previousScale = currentScale
420 let newScale = currentScale * scaleFactor
421 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
422 let realScaleFactor = currentScale / previousScale
424 let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left
425 let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top
427 currentImageScaleTransform.ScaleX <- currentScale
428 currentImageScaleTransform.ScaleY <- currentScale
430 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
431 scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
433 args
.Handled <- true)
435 // Pan on the current image.
436 let mutable scrollStartPosition = Point(0., 0.)
437 let mutable scrollStartOffsetX = 0.
438 let mutable scrollStartOffsetY = 0.
439 borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args
->
440 scrollStartPosition <- args
.GetPosition(scrollViewCurrentImage)
441 scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset
442 scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset
443 borderCurrentImage.Cursor <- Input.Cursors.ScrollAll
444 borderCurrentImage.CaptureMouse() |> ignore
445 args
.Handled <- true)
447 borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args
->
448 if borderCurrentImage.IsMouseCaptured
450 let position = args
.GetPosition(scrollViewCurrentImage)
451 let deltaX = scrollStartPosition.X - position.X
452 let deltaY = scrollStartPosition.Y - position.Y
453 scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
454 scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
456 args
.Handled <- true)
458 borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args
->
459 if borderCurrentImage.IsMouseCaptured
461 borderCurrentImage.Cursor <- Input.Cursors.Arrow
462 borderCurrentImage.ReleaseMouseCapture()
463 args
.Handled <- true)
466 scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args
-> updateViewportPreview ())
468 mainWindow.Root.Show()