44de2e335f3f2245f077dbda2e3d3cdcd0995bb9
[master-thesis.git] / Parasitemia / Parasitemia / GUI / State.fs
1 module Parasitemia.GUI.State
2
3 open System.Collections.Generic
4 open System.Windows
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Types
10
11 type State () =
12 let sourceImages = List<SourceImage>()
13
14 member val CurrentImage: SourceImage option = None with get, set
15 member val FilePath: string = "" with get, set
16 member val PatientID: string = "" with get, set
17
18 member this.ImageParasitemia (srcImg: SourceImage) : int * int =
19 List.length srcImg.rbcs,
20 srcImg.rbcs |> List.fold (fun nbInfected rbc -> if rbc.infected then nbInfected + 1 else nbInfected) 0
21
22 member this.GlobalParasitemia : int * int =
23 sourceImages
24 |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
25 let nb, nbInfected = this.ImageParasitemia srcImg
26 nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
27
28 member this.Save () =
29 let data = { PiaZ.sources = List.ofSeq sourceImages; PiaZ.patientID = this.PatientID }
30 PiaZ.save this.FilePath data
31
32 member this.Load () =
33 let data = PiaZ.load this.FilePath
34 this.PatientID <- data.patientID
35 sourceImages.Clear()
36 sourceImages.InsertRange(0, data.sources)
37 if sourceImages.Count > 0
38 then this.CurrentImage <- Some sourceImages.[0]
39
40 member this.AddSourceImage (filePath: string) : SourceImage =
41 let srcImg = { num = sourceImages.Count + 1; rbcs = []; img = new Image<Bgr, byte>(filePath) }
42 sourceImages.Add(srcImg)
43 if sourceImages.Count = 1
44 then this.CurrentImage <- Some sourceImages.[0]
45 srcImg
46
47 member this.SetResult (imgNum: int) (cells: Cell list) =
48 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
49 let w = sourceImage.img.Width
50 let h = sourceImage.img.Height
51 sourceImage.rbcs <- cells
52 |> List.filter (fun cell -> match cell.cellClass with HealthyRBC | InfectedRBC -> true | _ -> false )
53 |> List.sortByDescending (fun cell -> cell.infectedArea, (w - cell.center.X) + (h - cell.center.Y))
54 |> List.mapi (fun i cell ->
55 { num = i + 1
56 infected = cell.cellClass = InfectedRBC
57 setManually = false
58 center = Point(float cell.center.X, float cell.center.Y)
59 size = Size(float cell.elements.Width, float cell.elements.Height)
60 infectedArea = cell.infectedArea })
61
62 member this.SourceImages : SourceImage seq =
63 sourceImages :> SourceImage seq
64
65 member this.Reset () =
66 this.PatientID <- ""
67 sourceImages.Clear()