8aef25154d9429de7ff2c573fcfd6e94d9b245ba
1
module Parasitemia.GUI.Main
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.
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
24 // Utils.log <- (fun m -> log mainWindow m)
26 let colorRBCHealthy = Brushes.YellowGreen
27 let colorRBCInfected = Brushes.Red
29 let state = State.State()
30 let mutable currentScale = 1.
32 let exit: MenuItem = ctrl "menuExit"
33 let saveFile: MenuItem = ctrl "menuSave"
34 let loadFile: MenuItem = ctrl "menuOpen"
35 let newFile: MenuItem = ctrl "menuNew"
37 let addSourceImage: MenuItem = ctrl "menuAddSourceImage"
38 let txtPatient: TextBox = ctrl "txtPatient"
39 let txtGlobalParasitemia: TextBox = ctrl "txtGlobalParasitemia"
41 let butStartAnalysis: Button = ctrl "butStartAnalysis"
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"
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)))
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
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
74 setRBCFrameStyle rbc frame
75 frame
.border
.StrokeThickness <- 1.
76 frame
.lblRBCNumber
.Content <- rbc.num
79 let highlightRBCFrame (frame
: Views.RBCFrame) (highlight
: bool) =
80 let rbc = frame
.Tag :?> RBC
83 frame
.border
.StrokeThickness <- 3.
84 if not
rbc.infected
&& not
rbc.setManually
then frame
.Opacity <- 1.
86 frame
.border
.StrokeThickness <- 1.
87 if not
rbc.infected
&& not
rbc.setManually
then frame
.Opacity <- 0.
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)
93 let parasitemiaText (nbTotal
: int, nbInfected
: int) : string =
98 let percent = 100. * (float nbInfected) / (float nbTotal)
99 sprintf
"%.2f %% (%d / %d)" percent nbInfected nbTotal
101 let updateCurrentImageInformation () =
102 match state.CurrentImage with
104 let parasitemiaStr = parasitemiaText (state.ImageParasitemia srcImg
)
105 txtImageInformation.Text <- sprintf
"Parasitemia: %s" parasitemiaStr
108 let updateGlobalParasitemia () =
109 txtGlobalParasitemia.Text <- parasitemiaText state.GlobalParasitemia
111 let rec setAsInfected
(rbc: RBC) (infected
: bool) =
112 rbc.SetAsInfected infected
113 canvasCurrentImage.Children
114 |> Seq.cast
<Views.RBCFrame>
117 if (frame
.Tag :?> RBC) = rbc
119 setRBCFrameStyle rbc frame
)
120 updateRBCFramesPreview
()
121 updateCurrentImageInformation ()
122 updateGlobalParasitemia ()
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(
130 if (f.Tag :?> RBC).infected
132 f.menuRBCSetAsHealthy
.Visibility <- Visibility.Visible
133 f.menuRBCSetAsInfected
.Visibility <- Visibility.Collapsed
135 f.menuRBCSetAsHealthy
.Visibility <- Visibility.Collapsed
136 f.menuRBCSetAsInfected
.Visibility <- Visibility.Visible)
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)
143 and updateRBCFramesPreview
() =
144 match state.CurrentImage with
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
151 RBCFrameFromExisting rbc (stackRBC.Children.[currentPreview] :?> Views.RBCFrame)
154 f.MouseLeftButtonUp.AddHandler(fun obj args
-> zoomToRBC ((obj
:?> Views.RBCFrame).Tag :?> RBC))
155 stackRBC.Children.Add(f) |> ignore
158 currentPreview <- currentPreview + 1
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))
164 stackRBC.Children.RemoveRange(currentPreview, stackRBC.Children.Count - currentPreview)
167 let updateRBCFramesCurrent () =
168 match state.CurrentImage with
170 let mutable currentCanvas = 0
171 for rbc in srcImg
.rbcs
do
173 if currentCanvas < canvasCurrentImage.Children.Count
175 RBCFrameFromExisting rbc (canvasCurrentImage.Children.[currentCanvas] :?> Views.RBCFrame)
178 canvasCurrentImage.Children.Add(f) |> ignore
181 currentCanvas <- currentCanvas + 1
183 Canvas.SetLeft(frame, rbc.center
.X - rbc.size
.Width / 2.)
184 Canvas.SetTop(frame, rbc.center
.Y - rbc.size
.Height / 2.)
186 for i
in currentCanvas .. canvasCurrentImage.Children.Count - 1 do
187 canvasCurrentImage.Children.[i
].Visibility <- Visibility.Hidden
191 let synchronizeState () =
192 state.PatientID <- txtPatient.Text
194 let setCurrentImage (srcImg
: SourceImage) =
195 state.CurrentImage <- Some srcImg
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.))
202 canvasCurrentImage.Height <- float srcImg.img
.Height
203 canvasCurrentImage.Width <- float srcImg.img
.Width
204 canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img
))
206 updateRBCFramesCurrent ()
207 updateRBCFramesPreview
()
208 updateCurrentImageInformation ()
211 let addPreview (srcImg: SourceImage) =
212 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
213 imgCtrl.lblImageNumber
.Content <- srcImg.num
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)))
220 let updatePreviews () =
221 stackPreviews.Children.Clear ()
222 if state.SourceImages.Count() > 0
224 for srcImg in state.SourceImages do
226 match state.CurrentImage with
227 | Some srcImg -> setCurrentImage srcImg
231 txtPatient.Text <- state.PatientID
233 updateGlobalParasitemia ()
235 exit.Click.AddHandler(fun obj args
-> mainWindow.Root.Close())
236 saveFile.Click.AddHandler(fun obj args
->
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 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
255 state.FilePath <- dialog.FileName
259 newFile.Click.AddHandler(fun obj args
->
260 // TODO: if current state not saved and not empty, ask to save it.
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
269 let srcImg = state.AddSourceImage(dialog.FileName)
271 updateGlobalParasitemia ()
272 if state.SourceImages.Count() = 1
274 setCurrentImage srcImg)
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 ()
282 // Refresh current image.
283 match state.CurrentImage with
284 | Some srcImg -> setCurrentImage srcImg
287 // Zoom on the current image.
288 let adjustCurrentImageBorders (deltaX
: float) (deltaY
: float) =
289 borderCurrentImage.BorderThickness <-
291 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
292 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.,
293 (scrollViewCurrentImage.ViewportWidth + deltaX
) / 2.,
294 (scrollViewCurrentImage.ViewportHeight + deltaY
) / 2.)
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
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.))
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.))
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
322 let previousScale = currentScale
324 let newScale = currentScale * scaleFactor
325 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
326 let realScaleFactor = currentScale / previousScale
328 let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - borderCurrentImage.BorderThickness.Left
329 let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - borderCurrentImage.BorderThickness.Top
331 //canvasCurrentImage.Margin <- Thickness(canvasCurrentImage.Margin.Top * realScaleFactor)
332 currentImageScaleTransform.ScaleX <- currentScale
333 currentImageScaleTransform.ScaleY <- currentScale
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)
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)
351 borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args
->
352 if borderCurrentImage.IsMouseCaptured
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)
361 borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args
->
362 if borderCurrentImage.IsMouseCaptured
364 borderCurrentImage.Cursor <- Input.Cursors.Arrow
365 borderCurrentImage.ReleaseMouseCapture()
366 args
.Handled <- true)
368 // display mainWindow img
369 mainWindow.Root.Show()