b0af4cc198ec565ab2d5634b35bcd25631bc98ec
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 img
.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point((if x < 0 then 0 else x), (if y < 0 then 0 else y)),
65 System.Drawing.Size((if x + w >= img
.Width then img
.Width - x else w),
66 (if y + h >= img
.Height then img
.Height - y else h))))
68 let setRBCFrameStyle (rbc
: RBC) (frame
: Views.RBCFrame) =
69 frame
.Opacity <- if displayHealthy || rbc.setManually
|| rbc.infected
then 1. else 0.
70 let color = if rbc.infected
then colorRBCInfected else colorRBCHealthy
71 frame
.manuallyAdded
.Visibility <- if rbc.setManually
then Visibility.Visible else Visibility.Hidden
72 frame
.manuallyAdded
.Fill <- color
73 frame
.border
.Stroke <- color
75 let RBCFrameFromExisting (rbc: RBC) (frame
: Views.RBCFrame) : Views.RBCFrame =
76 frame
.Visibility <- Visibility.Visible
77 frame
.Height <- rbc.size
.Height
78 frame
.Width <- rbc.size
.Width
80 setRBCFrameStyle rbc frame
81 frame
.border
.StrokeThickness <- 1.
82 frame
.txtRBCNumber
.Text <- rbc.num
.ToString()
85 let highlightRBCFrame (frame
: Views.RBCFrame) (highlight
: bool) =
86 let rbc = frame
.Tag :?> RBC
89 frame
.border
.StrokeThickness <- 3.
90 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 1.
92 frame
.border
.StrokeThickness <- 1.
93 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 0.
95 let zoomToRBC (rbc: RBC) =
96 scrollViewCurrentImage.ScrollToHorizontalOffset(rbc.center
.X * currentScale - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
97 scrollViewCurrentImage.ScrollToVerticalOffset(rbc.center
.Y * currentScale - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
99 let parasitemiaText (nbTotal
: int, nbInfected
: int) : string =
104 let percent = 100. * (float nbInfected) / (float nbTotal)
105 sprintf
"%.1f %% (%d / %d)" percent nbInfected nbTotal
107 let updateCurrentImageInformation () =
108 match state.CurrentImage with
110 let parasitemiaStr = parasitemiaText (state.ImageParasitemia srcImg
)
111 txtImageInformation.Inlines.Clear()
112 txtImageInformation.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold))
113 txtImageInformation.Inlines.Add(parasitemiaStr)
114 txtImageInformation.Inlines.Add(Documents.LineBreak())
116 txtImageInformation.Inlines.Add(Documents.Run("Average erytrocyte diameter: ", FontWeight = FontWeights.Bold))
117 txtImageInformation.Inlines.Add(Documents.Run(srcImg
.config
.FormattedRadius))
118 txtImageInformation.Inlines.Add(Documents.LineBreak())
120 txtImageInformation.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold))
121 txtImageInformation.Inlines.Add(Documents.Run(if srcImg
.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToLocalTime().ToString()))
124 let updateGlobalParasitemia () =
125 txtGlobalParasitemia.Text <- parasitemiaText state.GlobalParasitemia
127 let updateViewportPreview () =
128 for preview
in stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> do
129 let srcImg = preview
.Tag :?> SourceImage
130 if Some srcImg = state.CurrentImage then
131 preview
.viewport
.Visibility <- Visibility.Visible
133 let canvasWidth = canvasCurrentImage.ActualWidth * currentScale
134 let canvasHeight = canvasCurrentImage.ActualHeight * currentScale
135 let previewWidth = (preview
.ActualWidth - preview
.BorderThickness.Left - preview
.BorderThickness.Right)
136 let previewHeight = (preview
.ActualHeight - preview
.BorderThickness.Top - preview
.BorderThickness.Bottom)
138 let marginLeft = previewWidth * (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Left) / canvasWidth - 2.
139 let marginRight = previewWidth * (canvasWidth - (scrollViewCurrentImage.HorizontalOffset - borderCurrentImage.BorderThickness.Right) - scrollViewCurrentImage.ViewportWidth) / canvasWidth - 2.
140 let marginTop = previewHeight * (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Top) / canvasHeight - 2.
141 let marginBottom = previewHeight * (canvasHeight - (scrollViewCurrentImage.VerticalOffset - borderCurrentImage.BorderThickness.Bottom) - scrollViewCurrentImage.ViewportHeight) / canvasHeight - 2.
143 preview
.viewport
.Margin <-
150 preview
.viewport
.Visibility <- Visibility.Hidden
152 let rec setAsInfected
(rbc: RBC) (infected
: bool) =
153 state.SetAsInfected rbc infected
154 canvasCurrentImage.Children
155 |> Seq.cast
<Views.RBCFrame>
158 if (frame
.Tag :?> RBC) = rbc
160 setRBCFrameStyle rbc frame
)
161 updateRBCFramesPreview
()
162 updateCurrentImageInformation ()
163 updateGlobalParasitemia ()
165 and RBCFrame (rbc: RBC) : Views.RBCFrame =
166 let frame = RBCFrameFromExisting rbc (Views.RBCFrame())
167 frame.SetValue(Panel.ZIndexProperty, Int32.MaxValue - rbc.num
) // To be sure the
168 frame.menuRBCSetAsHealthy
.Click.AddHandler(fun obj args
-> setAsInfected
(frame.Tag :?> RBC) false)
169 frame.menuRBCSetAsInfected
.Click.AddHandler(fun obj args
-> setAsInfected
(frame.Tag :?> RBC) true)
170 frame.ContextMenuOpening.AddHandler(
172 if (frame.Tag :?> RBC).infected
174 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Visible
175 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Collapsed
177 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Collapsed
178 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Visible)
180 frame.ContextMenuClosing.AddHandler(fun obj args
-> if not
frame.IsMouseOver then highlightRBCFrame frame false )
181 frame.MouseEnter.AddHandler(fun obj args
-> highlightRBCFrame frame true)
182 frame.MouseLeave.AddHandler(fun obj args
-> if not
frame.grid
.ContextMenu.IsOpen then highlightRBCFrame frame false)
185 and updateRBCFramesPreview
() =
186 match state.CurrentImage with
188 let mutable currentPreview = 0
189 for rbc in srcImg.rbcs
|> List.filter
(fun rbc -> displayHealthy || rbc.infected
) do
190 let previewInfected =
191 if currentPreview < stackRBC.Children.Count
193 RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame)
196 f.MouseLeftButtonUp.AddHandler(fun obj args
-> zoomToRBC (f.Tag :?> RBC))
197 stackRBC.Children.Add(f) |> ignore
200 currentPreview <- currentPreview + 1
202 previewInfected.Height <- stackRBC.ActualHeight
203 previewInfected.Width <- stackRBC.ActualHeight * rbc.size
.Width / rbc.size
.Height
204 previewInfected.border
.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img
rbc))
206 stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview)
209 updateViewportPreview ()
211 let updateRBCFramesCurrent () =
212 match state.CurrentImage with
214 let mutable currentCanvas = 0
215 for rbc in srcImg.rbcs
do
217 if currentCanvas < canvasCurrentImage.Children.Count
219 RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame)
222 f.Root.Opacity <- 0.7
223 canvasCurrentImage.Children.Add(f) |> ignore
226 currentCanvas <- currentCanvas + 1
228 Canvas.SetLeft(frame, rbc.center
.X - rbc.size
.Width / 2.)
229 Canvas.SetTop(frame, rbc.center
.Y - rbc.size
.Height / 2.)
231 for i
in currentCanvas .. canvasCurrentImage.Children.Count - 1 do
232 canvasCurrentImage.Children.[i
].Visibility <- Visibility.Hidden
235 let saveCurrentDocument () =
236 if state.FilePath = ""
238 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension
, Filter = PiaZ.filter
);
239 let res = dialog.ShowDialog()
240 if res.HasValue && res.Value
242 state.FilePath <- dialog.FileName
247 // Ask the use to save the current document if neccessary.
248 let askSaveCurrent () =
249 if state.AlteredSinceLastSave
251 match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with
252 | MessageBoxResult.Yes -> saveCurrentDocument ()
255 let updateCurrentImage () =
256 match state.CurrentImage with
258 // Highlight the preview.
259 stackPreviews.Children
260 |> Seq.cast
<Views.ImageSourcePreview>
261 |> Seq.iter
(fun preview
-> preview
.border
.BorderThickness <- Thickness(if preview
.Tag = (srcImg :> Object) then 3. else 0.))
263 canvasCurrentImage.Height <- float srcImg.img
.Height
264 canvasCurrentImage.Width <- float srcImg.img
.Width
265 canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img
))
267 updateRBCFramesCurrent ()
268 updateRBCFramesPreview
()
269 updateCurrentImageInformation ()
271 stackRBC.Children.Clear()
272 canvasCurrentImage.Children.Clear()
273 canvasCurrentImage.Background <- Brushes.Black
275 let setCurrentImage (srcImg: SourceImage) =
276 if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg
278 state.CurrentImage <- Some srcImg
279 updateCurrentImage ()
281 let addPreview (srcImg: SourceImage) =
282 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
284 imgCtrl.menuRemoveImage
.Click.AddHandler(fun obj args
->
285 stackPreviews.Children.Remove(imgCtrl)
286 let srcImg = imgCtrl.Tag :?> SourceImage
287 let currentRemoved = Some srcImg = state.CurrentImage
288 state.RemoveSourceImage srcImg
292 stackPreviews.Children |> Seq.cast
<Views.ImageSourcePreview> |> Seq.iter
(fun imgPreview
-> imgPreview
.txtImageNumber
.Text <- (imgPreview
.Tag :?> SourceImage).num
.ToString()))
294 imgCtrl.Tag <- srcImg
295 imgCtrl.txtImageNumber
.Text <- srcImg.num
.ToString()
297 let height = srcImg.img
.Height * width / srcImg.img
.Width
298 imgCtrl.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
299 stackPreviews.Children.Add(imgCtrl) |> ignore
301 // Zoom to a mouse position into the control 'imgCtrl'.
302 let zoomTo (mousePos
: Point) =
303 let canvasW = canvasCurrentImage.ActualWidth * currentScale
304 let canvasH = canvasCurrentImage.ActualHeight * currentScale
305 let centerX = (mousePos
.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW
306 let centerY = (mousePos
.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH
307 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
308 scrollViewCurrentImage.ScrollToVerticalOffset(centerY - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
310 imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args
->
311 setCurrentImage (state.SourceImages |> Seq.find
(fun srcImg -> (srcImg :> Object) = imgCtrl.Tag))
312 imgCtrl.UpdateLayout()
313 zoomTo (args
.GetPosition(imgCtrl))
314 imgCtrl.CaptureMouse() |> ignore
)
316 imgCtrl.MouseMove.AddHandler(fun obj args
->
317 if imgCtrl.IsMouseCaptured
319 zoomTo (args
.GetPosition(imgCtrl)))
321 imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args
->
322 if imgCtrl.IsMouseCaptured
324 imgCtrl.ReleaseMouseCapture())
326 let updatePreviews () =
327 stackPreviews.Children.Clear ()
328 for srcImg in state.SourceImages do
330 updateCurrentImage ()
333 txtPatient.Text <- state.PatientID
335 updateGlobalParasitemia ()
337 txtPatient.LostFocus.AddHandler(fun obj args
-> state.PatientID <- txtPatient.Text)
339 menuExit.Click.AddHandler(fun obj args
->
341 mainWindow.Root.Close())
343 menuSaveFile.Click.AddHandler(fun obj args
-> saveCurrentDocument ())
345 menuLoadFile.Click.AddHandler(fun obj args
->
346 // TODO: if current state not saved and not empty, ask to save it.
347 let dialog = OpenFileDialog(Filter = PiaZ.filter
)
348 let res = dialog.ShowDialog()
349 if res.HasValue && res.Value
352 state.FilePath <- dialog.FileName
356 menuNewFile.Click.AddHandler(fun obj args
->
361 menuAddSourceImage.Click.AddHandler(fun obj args
->
362 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff")
363 let res = dialog.ShowDialog()
364 if res.HasValue && res.Value
366 let srcImg = state.AddSourceImage dialog.FileName defaultConfig
368 updateGlobalParasitemia ()
369 if state.SourceImages.Count() = 1
371 updateCurrentImage ())
373 menuStartAnalysis.Click.AddHandler(fun obj args
->
374 if Analysis.showWindow
mainWindow.Root state
376 updateGlobalParasitemia ()
377 updateCurrentImage ())
379 menuHightlightRBC.Click.AddHandler(fun obj args
->
380 displayHealthy <- menuHightlightRBC.IsChecked
381 updateRBCFramesPreview
()
382 updateRBCFramesCurrent ())
384 // Zoom on the current image.
385 let adjustCurrentImageBorders (deltaX
: float) (deltaY
: float) =
386 borderCurrentImage.BorderThickness <-
388 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
389 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.,
390 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
391 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.)
393 canvasCurrentImage.SizeChanged.AddHandler(fun obj args
->
394 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
395 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
396 if deltaX > 0.5 || deltaY > 0.5
398 adjustCurrentImageBorders 0.0
0.0
399 // Center the view at the center of the image initialy.
400 scrollViewCurrentImage.UpdateLayout()
401 scrollViewCurrentImage.ScrollToHorizontalOffset(borderCurrentImage.ActualWidth / 2. - scrollViewCurrentImage.ViewportWidth / 2.)
402 scrollViewCurrentImage.ScrollToVerticalOffset(borderCurrentImage.ActualHeight / 2. - scrollViewCurrentImage.ViewportHeight / 2.))
404 scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args
->
405 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
406 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
407 adjustCurrentImageBorders deltaX deltaY
408 scrollViewCurrentImage.ScrollToHorizontalOffset(scrollViewCurrentImage.HorizontalOffset + deltaX / 8.)
409 scrollViewCurrentImage.ScrollToVerticalOffset(scrollViewCurrentImage.VerticalOffset + deltaY / 8.))
411 let mutable maxScale = 4.
412 let mutable minScale = 0.25
413 let currentImageScaleTransform = ScaleTransform()
414 canvasCurrentImage.LayoutTransform <- currentImageScaleTransform
415 borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args
->
416 let scaleFactor = if args
.Delta > 0 then 2.0 else 0.5
417 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale
419 let previousScale = currentScale
421 let newScale = currentScale * scaleFactor
422 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
423 let realScaleFactor = currentScale / previousScale
425 let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left
426 let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top
428 currentImageScaleTransform.ScaleX <- currentScale
429 currentImageScaleTransform.ScaleY <- currentScale
431 scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
432 scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
434 args
.Handled <- true)
436 // Pan on the current image.
437 let mutable scrollStartPosition = Point(0., 0.)
438 let mutable scrollStartOffsetX = 0.
439 let mutable scrollStartOffsetY = 0.
440 borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args
->
441 scrollStartPosition <- args
.GetPosition(scrollViewCurrentImage)
442 scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset
443 scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset
444 borderCurrentImage.Cursor <- Input.Cursors.ScrollAll
445 borderCurrentImage.CaptureMouse() |> ignore
446 args
.Handled <- true)
448 borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args
->
449 if borderCurrentImage.IsMouseCaptured
451 let position = args
.GetPosition(scrollViewCurrentImage)
452 let deltaX = scrollStartPosition.X - position.X
453 let deltaY = scrollStartPosition.Y - position.Y
454 scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
455 scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
457 args
.Handled <- true)
459 borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args
->
460 if borderCurrentImage.IsMouseCaptured
462 borderCurrentImage.Cursor <- Input.Cursors.Arrow
463 borderCurrentImage.ReleaseMouseCapture()
464 args
.Handled <- true)
467 scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args
-> updateViewportPreview ())
469 mainWindow.Root.Show()