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 updateViewportPreview () =
130 for preview
in stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> do
131 let srcImg = preview
.Tag :?> SourceImage
132 if Some srcImg = state.CurrentImage then
133 preview
.viewport
.Visibility <- Visibility.Visible
135 let canvasWidth = canvasCurrentImage.ActualWidth * currentScale
136 let canvasHeight = canvasCurrentImage.ActualHeight * currentScale
137 let previewWidth = (preview
.ActualWidth - preview
.BorderThickness.Left - preview
.BorderThickness.Right)
138 let previewHeight = (preview
.ActualHeight - preview
.BorderThickness.Top - preview
.BorderThickness.Bottom)
140 let marginLeft = previewWidth * (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Left) / canvasWidth - 2.
141 let marginRight = previewWidth * (canvasWidth - (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Right) - scrollViewCurrentImage.ViewportWidth) / canvasWidth - 2.
142 let marginTop = previewHeight * (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Top) / canvasHeight - 2.
143 let marginBottom = previewHeight * (canvasHeight - (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Bottom) - scrollViewCurrentImage.ViewportHeight) / canvasHeight - 2.
145 preview
.viewport
.Margin <-
152 preview
.viewport
.Visibility <- Visibility.Hidden
154 let rec setAsInfected
(rbc: RBC) (infected
: bool) =
155 state.SetAsInfected rbc infected
156 canvasCurrentImage.Children
157 |> Seq.cast
<Views.RBCFrame>
160 if (frame
.Tag :?> RBC) = rbc
162 setRBCFrameStyle rbc frame
)
163 updateRBCFramesPreview
()
164 updateCurrentImageInformation ()
165 updateGlobalParasitemia ()
167 and RBCFrame (rbc: RBC) : Views.RBCFrame =
168 let frame = RBCFrameFromExisting rbc (Views.RBCFrame())
169 frame.SetValue(Panel.ZIndexProperty, Int32.MaxValue - rbc.num
) // To be sure the
170 frame.menuRBCSetAsHealthy
.Click.AddHandler(fun obj args
-> setAsInfected
(frame.Tag :?> RBC) false)
171 frame.menuRBCSetAsInfected
.Click.AddHandler(fun obj args
-> setAsInfected
(frame.Tag :?> RBC) true)
172 frame.ContextMenuOpening.AddHandler(
174 if (frame.Tag :?> RBC).infected
176 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Visible
177 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Collapsed
179 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Collapsed
180 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Visible)
182 frame.ContextMenuClosing.AddHandler(fun obj args
-> if not
frame.IsMouseOver then highlightRBCFrame frame false )
183 frame.MouseEnter.AddHandler(fun obj args
-> highlightRBCFrame frame true)
184 frame.MouseLeave.AddHandler(fun obj args
-> if not
frame.grid
.ContextMenu.IsOpen then highlightRBCFrame frame false)
187 and updateRBCFramesPreview
() =
188 match state.CurrentImage with
190 let mutable currentPreview = 0
191 for rbc in srcImg.rbcs
|> List.filter
(fun rbc -> displayHealthy || rbc.infected
) do
192 let previewInfected =
193 if currentPreview < stackRBC.Children.Count
195 RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame)
198 f.MouseLeftButtonUp.AddHandler(fun obj args
-> zoomToRBC (f.Tag :?> RBC))
199 stackRBC.Children.Add(f) |> ignore
202 currentPreview <- currentPreview + 1
204 previewInfected.Height <- stackRBC.ActualHeight
205 previewInfected.Width <- stackRBC.ActualHeight * rbc.size
.Width / rbc.size
.Height
206 previewInfected.border
.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img
rbc))
208 stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview)
211 updateViewportPreview ()
213 let updateRBCFramesCurrent () =
214 match state.CurrentImage with
216 let mutable currentCanvas = 0
217 for rbc in srcImg.rbcs
do
219 if currentCanvas < canvasCurrentImage.Children.Count
221 RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame)
224 f.Root.Opacity <- 0.7
225 canvasCurrentImage.Children.Add(f) |> ignore
228 currentCanvas <- currentCanvas + 1
230 Canvas.SetLeft(frame, rbc.center
.X - rbc.size
.Width / 2.)
231 Canvas.SetTop(frame, rbc.center
.Y - rbc.size
.Height / 2.)
233 for i
in currentCanvas .. canvasCurrentImage.Children.Count - 1 do
234 canvasCurrentImage.Children.[i
].Visibility <- Visibility.Hidden
237 let saveCurrentDocument () =
238 if state.FilePath = ""
240 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension
, Filter = PiaZ.filter
);
241 let res = dialog.ShowDialog()
242 if res.HasValue && res.Value
244 state.FilePath <- dialog.FileName
249 // Ask the use to save the current document if neccessary.
250 let askSaveCurrent () =
251 if state.AlteredSinceLastSave
253 match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with
254 | MessageBoxResult.Yes -> saveCurrentDocument ()
257 let updateCurrentImage () =
258 match state.CurrentImage with
260 // Highlight the preview.
261 stackPreviews.Children
262 |> Seq.cast
<Views.ImageSourcePreview>
263 |> Seq.iter
(fun preview
-> preview
.border
.BorderThickness <- Thickness(if preview
.Tag = (srcImg :> Object) then 3. else 0.))
265 canvasCurrentImage.Height <- float srcImg.img
.Height
266 canvasCurrentImage.Width <- float srcImg.img
.Width
267 canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img
))
269 updateRBCFramesCurrent ()
270 updateRBCFramesPreview
()
271 updateCurrentImageInformation ()
273 stackRBC.Children.Clear()
274 canvasCurrentImage.Children.Clear()
275 canvasCurrentImage.Background <- Brushes.Black
277 let setCurrentImage (srcImg: SourceImage) =
278 if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg
280 state.CurrentImage <- Some srcImg
281 updateCurrentImage ()
283 let addPreview (srcImg: SourceImage) =
284 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
286 imgCtrl.menuRemoveImage
.Click.AddHandler(fun obj args
->
287 stackPreviews.Children.Remove(imgCtrl)
288 let srcImg = imgCtrl.Tag :?> SourceImage
289 let currentRemoved = Some srcImg = state.CurrentImage
290 state.RemoveSourceImage srcImg
294 stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> |> Seq.iter
(fun imgPreview
-> imgPreview
.txtImageNumber
.Text <- (imgPreview
.Tag :?> SourceImage).num
.ToString()))
296 imgCtrl.Tag <- srcImg
297 imgCtrl.txtImageNumber
.Text <- srcImg.num
.ToString()
299 let height = srcImg.img
.Height * width / srcImg.img
.Width
300 imgCtrl.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
301 stackPreviews.Children.Add(imgCtrl) |> ignore
303 // Zoom to a mouse position into the control 'imgCtrl'.
304 let zoomTo (mousePos
: Point) =
305 let canvasW = canvasCurrentImage.ActualWidth * currentScale
306 let canvasH = canvasCurrentImage.ActualHeight * currentScale
307 let centerX = (mousePos
.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW
308 let centerY = (mousePos
.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH
309 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
310 scrollViewCurrentImage.ScrollToVerticalOffset(centerY - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
312 imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args
->
313 setCurrentImage (state.SourceImages |> Seq.find
(fun srcImg -> (srcImg :> Object) = imgCtrl.Tag))
314 imgCtrl.UpdateLayout()
315 zoomTo (args
.GetPosition(imgCtrl))
316 imgCtrl.CaptureMouse() |> ignore
)
318 imgCtrl.MouseMove.AddHandler(fun obj args
->
319 if imgCtrl.IsMouseCaptured
321 zoomTo (args
.GetPosition(imgCtrl)))
323 imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args
->
324 if imgCtrl.IsMouseCaptured
326 imgCtrl.ReleaseMouseCapture())
329 let updatePreviews () =
330 stackPreviews.Children.Clear ()
331 for srcImg in state.SourceImages do
333 updateCurrentImage ()
336 txtPatient.Text <- state.PatientID
338 updateGlobalParasitemia ()
340 txtPatient.TextChanged.AddHandler(fun obj args
-> state.PatientID <- txtPatient.Text)
342 menuExit.Click.AddHandler(fun obj args
->
344 mainWindow.Root.Close())
346 menuSaveFile.Click.AddHandler(fun obj args
-> saveCurrentDocument ())
348 menuLoadFile.Click.AddHandler(fun obj args
->
349 // TODO: if current state not saved and not empty, ask to save it.
350 let dialog = OpenFileDialog(Filter = PiaZ.filter
)
351 let res = dialog.ShowDialog()
352 if res.HasValue && res.Value
355 state.FilePath <- dialog.FileName
359 menuNewFile.Click.AddHandler(fun obj args
->
364 menuAddSourceImage.Click.AddHandler(fun obj args
->
365 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff")
366 let res = dialog.ShowDialog()
367 if res.HasValue && res.Value
369 let srcImg = state.AddSourceImage(dialog.FileName)
371 updateGlobalParasitemia ()
372 if state.SourceImages.Count() = 1
374 updateCurrentImage ())
376 menuStartAnalysis.Click.AddHandler(fun obj args
->
377 if Analysis.showWindow
mainWindow.Root state defaultConfig
379 updateGlobalParasitemia ()
380 updateCurrentImage ())
382 menuHightlightRBC.Click.AddHandler(fun obj args
->
383 displayHealthy <- menuHightlightRBC.IsChecked
384 updateRBCFramesPreview
()
385 updateRBCFramesCurrent ())
387 // Zoom on the current image.
388 let adjustCurrentImageBorders (deltaX
: float) (deltaY
: float) =
389 borderCurrentImage.BorderThickness <-
391 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
392 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.,
393 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
394 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.)
396 canvasCurrentImage.SizeChanged.AddHandler(fun obj args
->
397 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
398 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
399 if deltaX > 0.5 || deltaY > 0.5
401 adjustCurrentImageBorders 0.0
0.0
402 // Center the view at the center of the image initialy.
403 scrollViewCurrentImage.UpdateLayout()
404 scrollViewCurrentImage.ScrollToHorizontalOffset(borderCurrentImage.ActualWidth / 2. - scrollViewCurrentImage.ViewportWidth / 2.)
405 scrollViewCurrentImage.ScrollToVerticalOffset(borderCurrentImage.ActualHeight / 2. - scrollViewCurrentImage.ViewportHeight / 2.))
407 scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args
->
408 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
409 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
410 adjustCurrentImageBorders deltaX deltaY
411 scrollViewCurrentImage.ScrollToHorizontalOffset(scrollViewCurrentImage.HorizontalOffset + deltaX / 8.)
412 scrollViewCurrentImage.ScrollToVerticalOffset(scrollViewCurrentImage.VerticalOffset + deltaY / 8.))
414 let mutable maxScale = 4.
415 let mutable minScale = 0.25
416 let currentImageScaleTransform = ScaleTransform()
417 canvasCurrentImage.LayoutTransform <- currentImageScaleTransform
418 borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args
->
419 let scaleFactor = if args
.Delta > 0 then 2.0 else 0.5
420 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale
422 let previousScale = currentScale
424 let newScale = currentScale * scaleFactor
425 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
426 let realScaleFactor = currentScale / previousScale
428 let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left
429 let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top
431 currentImageScaleTransform.ScaleX <- currentScale
432 currentImageScaleTransform.ScaleY <- currentScale
434 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
435 scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
437 args
.Handled <- true)
439 // Pan on the current image.
440 let mutable scrollStartPosition = Point(0., 0.)
441 let mutable scrollStartOffsetX = 0.
442 let mutable scrollStartOffsetY = 0.
443 borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args
->
444 scrollStartPosition <- args
.GetPosition(scrollViewCurrentImage)
445 scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset
446 scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset
447 borderCurrentImage.Cursor <- Input.Cursors.ScrollAll
448 borderCurrentImage.CaptureMouse() |> ignore
449 args
.Handled <- true)
451 borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args
->
452 if borderCurrentImage.IsMouseCaptured
454 let position = args
.GetPosition(scrollViewCurrentImage)
455 let deltaX = scrollStartPosition.X - position.X
456 let deltaY = scrollStartPosition.Y - position.Y
457 scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
458 scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
460 args
.Handled <- true)
462 borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args
->
463 if borderCurrentImage.IsMouseCaptured
465 borderCurrentImage.Cursor <- Input.Cursors.Arrow
466 borderCurrentImage.ReleaseMouseCapture()
467 args
.Handled <- true)
470 scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args
-> updateViewportPreview ())
472 mainWindow.Root.Show()