GUI (work in progress..)
authorGreg Burri <greg.burri@gmail.com>
Fri, 8 Jan 2016 20:08:00 +0000 (21:08 +0100)
committerGreg Burri <greg.burri@gmail.com>
Fri, 8 Jan 2016 20:08:00 +0000 (21:08 +0100)
Parasitemia/Parasitemia/GUI/GUI.fs
Parasitemia/Parasitemia/GUI/ImageSourcePreview.xaml.fs
Parasitemia/Parasitemia/GUI/MainWindow.xaml
Parasitemia/Parasitemia/GUI/MainWindow.xaml.fs
Parasitemia/Parasitemia/GUI/NumericUpDown.xaml.fs
Parasitemia/Parasitemia/GUI/Pia.fs
Parasitemia/Parasitemia/GUI/State.fs
Parasitemia/Parasitemia/GUI/Types.fs [new file with mode: 0644]
Parasitemia/Parasitemia/Parasitemia.fsproj
Parasitemia/Parasitemia/Program.fs

index 155c1d1..6f11379 100644 (file)
@@ -1,12 +1,12 @@
-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.
 
@@ -15,6 +15,7 @@ open Microsoft.Win32 // For the common dialogs.
 open Emgu.CV.WPF
 
 open Config
+open Types
 
 let run (defaultConfig: Config) =
     let app = new Application()
@@ -24,6 +25,8 @@ let run (defaultConfig: Config) =
 
     // Utils.log <- (fun m -> log mainWindow m)
 
+    let currentImageMargin = 0.1 // 10 %.
+
     let state = State.State()
 
     let exit: MenuItem = ctrl "menuExit"
@@ -32,25 +35,39 @@ let run (defaultConfig: Config) =
     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
@@ -90,11 +107,78 @@ let run (defaultConfig: Config) =
         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)*)
index b92e9af..425d436 100644 (file)
@@ -1,4 +1,4 @@
-namespace Parasitemia.Views
+namespace Parasitemia.GUI.Views
 
 open System
 open System.Windows
index 2892330..9b710e5 100644 (file)
             <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
index 62c35a4..4e327e0 100644 (file)
@@ -1,4 +1,4 @@
-namespace Parasitemia.Views
+namespace Parasitemia.GUI.Views
 
 open FsXaml
 
index 6f91924..574ffd6 100644 (file)
@@ -1,4 +1,4 @@
-namespace Parasitemia.Views
+namespace Parasitemia.GUI.Views
 
 open System
 open System.Windows
index 9ddd063..432bad9 100644 (file)
@@ -1,5 +1,5 @@
 // ParasitemIA file format.
-module Pia
+module Parasitemia.GUI.Pia
 
 open System.Drawing
 open System.IO
@@ -10,31 +10,30 @@ open FSharp.Data
 open Emgu.CV
 open Emgu.CV.Structure
 
+open Types
+
 let extension = ".pia"
 let filter = "PIA|*.pia"
 
-type RBC = {
-    infected: bool
-    addedManually: bool
-    removed: bool
-
-    center: Point
-    size: Size
-    stainArea: int }
-
-type ImageSource = {
-    img: Image<Bgr, byte>
-    rbcs: RBC list }
-
 type FileData = {
-    sources: ImageSource list
+    sources: SourceImage list
     patientID: string }
 
 // The json type associated to a source image.
 type JSONSourceImage = JsonProvider<"""
     {
         "rbcs": [
-            { "infected": true, "addedManually": false, "removed": false, "posX" : 42, "posY" : 42, "width" : 10, "height" : 10, "stainArea" : 10 }
+            {
+                "num": 1,
+                "infected": true,
+                "addedManually": false,
+                "removed": false,
+                "posX" : 42,
+                "posY" : 42,
+                "width" : 10,
+                "height" : 10,
+                "stainArea" : 10
+            }
         ]
     }
 """>
index b5136c2..b35b331 100644 (file)
@@ -1,16 +1,15 @@
-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
@@ -23,12 +22,17 @@ type State () =
         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
diff --git a/Parasitemia/Parasitemia/GUI/Types.fs b/Parasitemia/Parasitemia/GUI/Types.fs
new file mode 100644 (file)
index 0000000..4f1260f
--- /dev/null
@@ -0,0 +1,22 @@
+module Parasitemia.GUI.Types
+
+open System.Drawing
+
+open Emgu.CV
+open Emgu.CV.Structure
+
+type RBC = {
+    num: int
+
+    infected: bool
+    addedManually: bool
+    removed: bool
+
+    center: Point
+    size: Size
+    stainArea: int }
+
+type SourceImage = {
+    num: int
+    img: Image<Bgr, byte>
+    rbcs: RBC list }
\ No newline at end of file
index 840a8f8..223be2c 100644 (file)
@@ -96,6 +96,7 @@
     <Compile Include="GUI\ImageSourcePreview.xaml.fs" />
     <Resource Include="GUI\MainWindow.xaml" />
     <Compile Include="GUI\MainWindow.xaml.fs" />
+    <Compile Include="GUI\Types.fs" />
     <Compile Include="GUI\Pia.fs" />
     <Compile Include="GUI\State.fs" />
     <Compile Include="GUI\GUI.fs" />
index f0e2748..bb61f4f 100644 (file)
@@ -118,4 +118,4 @@ let main args =
                txtLog.Text <- txtLog.Text + mess + "\n"*)
 
             if debug then config.Debug <- DebugOn "."
-            GUI.run config
+            GUI.Main.run config