-module Parasitemia.GUI
+module Parasitemia.GUI.Main
open System.IO
+open System.Linq
open System.Windows
open System.Windows.Media
open System.Windows.Markup
open System.Windows.Shapes
open System.Windows.Controls
-open System.Drawing
open System.Diagnostics
open Microsoft.Win32 // For the common dialogs.
open Emgu.CV.WPF
open Config
+open Types
let run (defaultConfig: Config) =
let app = new Application()
// Utils.log <- (fun m -> log mainWindow m)
+ let currentImageMargin = 0.1 // 10 %.
+
let state = State.State()
let exit: MenuItem = ctrl "menuExit"
let newFile: MenuItem = ctrl "menuNew"
let addSourceImage: MenuItem = ctrl "menuAddSourceImage"
-
let txtPatient: TextBox = ctrl "txtPatient"
-
let stackPreviews: StackPanel = ctrl "stackPreviews"
-
let butStartAnalysis: Button = ctrl "butStartAnalysis"
+ let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage"
+ let borderCurrentImage: Border = ctrl "borderCurrentImage"
+ let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage"
+
+ // Initializations.
+
+ // Operations.
let synchronizeState () =
state.PatientID <- txtPatient.Text
+ let setCurrentImage (srcImg: SourceImage) =
+ canvasCurrentImage.Height <- float srcImg.img.Height
+ canvasCurrentImage.Width <- float srcImg.img.Width
+ canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img))
+ (*for rbc in srcImg.rbcs do
+ let rectangle = Rectangle(Height = float rbc.size.Height, Width = float rbc.size.Width, )*)
+
+ let addPreview (srcImg: SourceImage) =
+ let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
+ imgCtrl.lblImageNumber.Content <- srcImg.num
+ let width = 200
+ let height = srcImg.img.Height * width / srcImg.img.Width
+ imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
+ stackPreviews.Children.Add(imgCtrl) |> ignore
+
let updatePreviews () =
stackPreviews.Children.Clear ()
- state.ImagesSource |> Seq.iteri (fun i imgSrc ->
- let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
- imgCtrl.lblImageNumber.Content <- i + 1
- let width = 200
- let height = imgSrc.img.Height * width / imgSrc.img.Width
- imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(imgSrc.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
- stackPreviews.Children.Add(imgCtrl) |> ignore )
+ for srcImg in state.SourceImages do
+ addPreview srcImg
let updateGUI () =
txtPatient.Text <- state.PatientID
let res = dialog.ShowDialog()
if res.HasValue && res.Value
then
- state.AddSourceImage(dialog.FileName)
- updatePreviews ())
+ let srcImg = state.AddSourceImage(dialog.FileName)
+ addPreview srcImg
+ if state.SourceImages.Count() = 1
+ then
+ setCurrentImage srcImg)
butStartAnalysis.Click.AddHandler(fun obj args -> ())
+ // Zoom on the current image.
+ let adjustCurrentImageMargins () =
+ borderCurrentImage.BorderThickness <-
+ Thickness(scrollViewCurrentImage.ViewportWidth / 2., scrollViewCurrentImage.ViewportHeight / 2., scrollViewCurrentImage.ViewportWidth / 2., scrollViewCurrentImage.ViewportHeight / 2.)
+
+ scrollViewCurrentImage.Loaded.AddHandler(fun obj args -> adjustCurrentImageMargins ())
+ scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args -> adjustCurrentImageMargins ())
+
+ let mutable currentScale = 1.
+ let mutable maxScale = 5.
+ let mutable minScale = 0.1
+ let currentImageScaleTransform = ScaleTransform()
+ borderCurrentImage.LayoutTransform <- currentImageScaleTransform
+ borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args ->
+ let scaleFactor = if args.Delta > 0 then 2.0 else 0.5
+ if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale
+ then
+ let previousScale = currentScale
+ currentScale <-
+ let newScale = currentScale * scaleFactor
+ if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
+ let realScaleFactor = currentScale / previousScale
+
+ let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - canvasCurrentImage.Margin.Left
+ let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - canvasCurrentImage.Margin.Top
+
+ canvasCurrentImage.Margin <- Thickness(canvasCurrentImage.Margin.Top * realScaleFactor)
+ currentImageScaleTransform.ScaleX <- currentScale
+ currentImageScaleTransform.ScaleY <- currentScale
+
+ scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + canvasCurrentImage.Margin.Left)
+ scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + canvasCurrentImage.Margin.Top)
+ args.Handled <- true)
+
+ // Pan on the current image.
+ let mutable scrollStartPosition = Point(0., 0.)
+ let mutable scrollStartOffsetX = 0.
+ let mutable scrollStartOffsetY = 0.
+ borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args ->
+ scrollStartPosition <- args.GetPosition(scrollViewCurrentImage)
+ scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset
+ scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset
+ borderCurrentImage.Cursor <- Input.Cursors.ScrollAll
+ borderCurrentImage.CaptureMouse() |> ignore
+ args.Handled <- true)
+
+ borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args ->
+ if borderCurrentImage.IsMouseCaptured
+ then
+ let position = args.GetPosition(scrollViewCurrentImage)
+ let deltaX = scrollStartPosition.X - position.X
+ let deltaY = scrollStartPosition.Y - position.Y
+ scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
+ scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
+ args.Handled <- true)
+
+ borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args ->
+ if borderCurrentImage.IsMouseCaptured
+ then
+ borderCurrentImage.Cursor <- Input.Cursors.Arrow
+ borderCurrentImage.ReleaseMouseCapture()
+ args.Handled <- true)
+
+
(*let txtPatient: Controls.TextBox = ctrl "txtPatient"
txtPatient.TextChanged.AddHandler(fun obj args ->
state.PatientID <- txtPatient.Text)*)
<TextBox x:Name="txtPatient" Grid.Column="1" Margin="3, 3, 10, 3" TextWrapping="Wrap" VerticalAlignment="Center" />
<TextBox x:Name="txtGlobalParasitemia" Grid.Column="1" Grid.Row="2" Margin="3, 3, 10, 3" TextWrapping="Wrap" VerticalAlignment="Center" IsReadOnly="True" />
</Grid>
- <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Margin="3">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <ScrollViewer x:Name="scrollPreviews" VerticalScrollBarVisibility="Auto" Margin="0,0,0,1" Grid.Row="1" >
+ <Grid Grid.Row="1">
+ <Grid.RowDefinitions>
+ <RowDefinition/>
+ <RowDefinition Height="Auto" />
+ </Grid.RowDefinitions>
+ <Border BorderBrush="Black" BorderThickness="1" Margin="3" Grid.Row="0" >
+ <ScrollViewer x:Name="scrollPreviews" VerticalScrollBarVisibility="Auto" >
<StackPanel x:Name="stackPreviews" />
</ScrollViewer>
- <Button x:Name="butStartAnalysis" Content="Start analysis" Grid.Row="1"/>
- </Grid>
- </Border>
+ </Border>
+ <Button x:Name="butStartAnalysis" Content="Start analysis" Grid.Row="1" Margin="3"/>
+ </Grid>
+ <Grid Grid.Column="2" Grid.Row="2">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="100"/>
+ <RowDefinition/>
+ <RowDefinition Height="60"/>
+ </Grid.RowDefinitions>
+ <ScrollViewer x:Name="scrollViewCurrentImage" Grid.Row="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Background="Black" >
+ <Border x:Name="borderCurrentImage" BorderBrush="Transparent">
+ <Canvas x:Name="canvasCurrentImage" Height="100" Width="100" />
+ </Border>
+ </ScrollViewer>
+ </Grid>
</Grid>
</DockPanel>
</Window>
\ No newline at end of file
-module State
+module Parasitemia.GUI.State
open System.Collections.Generic
open Emgu.CV
open Emgu.CV.Structure
-type ImageSource = {
- cells: List<Types.Cell>
- img: Image<Bgr, byte> }
+open Types
type State () =
- let imagesSource = List<ImageSource>()
+ let sourceImages = List<SourceImage>()
+ let mutable currentImage = -1
member val FilePath: string = "" with get, set
member val PatientID: string = "" with get, set
let data = Pia.load this.FilePath
this.PatientID <- data.patientID
- member this.AddSourceImage (filePath: string) =
- imagesSource.Add({ cells = List<Types.Cell>(); img = new Image<Bgr, byte>(filePath) })
+ member this.AddSourceImage (filePath: string) : SourceImage =
+ let srcImg = { num = sourceImages.Count + 1; rbcs = []; img = new Image<Bgr, byte>(filePath) }
+ sourceImages.Add(srcImg)
+ if sourceImages.Count = 1
+ then
+ currentImage <- 1
+ srcImg
- member x.ImagesSource : ImageSource seq =
- imagesSource :> ImageSource seq
+ member x.SourceImages : SourceImage seq =
+ sourceImages :> SourceImage seq
member this.Reset () =
this.PatientID <- ""
- imagesSource.Clear()
\ No newline at end of file
+ sourceImages.Clear()
\ No newline at end of file