Add an option to change the brightness of the highlight box color (healthy/infected...
[master-thesis.git] / Parasitemia / ParasitemiaUI / State.fs
1 module ParasitemiaUI.State
2
3 open System
4 open System.Collections.Generic
5 open System.Windows
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Types
11
12 type State () =
13 let sourceImages = List<SourceImage>()
14 let mutable alteredSinceLastSave = false
15 let mutable patientID = ""
16
17 member this.AlteredSinceLastSave = alteredSinceLastSave
18 member val CurrentImage: SourceImage option = None with get, set
19 member val FilePath: string = "" with get, set
20
21 member this.PatientID
22 with get () : string = patientID
23 and set id =
24 if id <> patientID
25 then
26 alteredSinceLastSave <- true
27 patientID <- id
28
29 member this.ImageParasitemia (srcImg: SourceImage) : int * int =
30 List.length srcImg.rbcs,
31 srcImg.rbcs |> List.fold (fun nbInfected rbc -> if rbc.infected then nbInfected + 1 else nbInfected) 0
32
33 member this.ImageNbAltered (srcImg: SourceImage) : int * int =
34 List.length srcImg.rbcs,
35 srcImg.rbcs |> List.fold (fun nbAltered rbc -> if rbc.setManually then nbAltered + 1 else nbAltered) 0
36
37 member this.GlobalParasitemia : int * int =
38 sourceImages
39 |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
40 let nb, nbInfected = this.ImageParasitemia srcImg
41 nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
42
43
44 member this.SetAsInfected (rbc: RBC) (infected: bool) =
45 if infected <> rbc.infected
46 then
47 alteredSinceLastSave <- true
48 rbc.infected <- infected
49 rbc.setManually <- not rbc.setManually
50
51 /// <summary>
52 /// Save the current state. 'FilePath' must have been defined.
53 /// </summary>
54 /// <exception cref="System.IOException">If the file cannot be saved</exception>
55 member this.Save () =
56 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images = List.ofSeq sourceImages }
57 PiaZ.save this.FilePath data
58 alteredSinceLastSave <- false
59
60 /// <summary>
61 /// Load the current state. 'FilePath' must have been defined.
62 /// </summary>
63 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
64 member this.Load () =
65 let data = PiaZ.load this.FilePath
66 this.PatientID <- data.patientID
67 sourceImages.Clear()
68 sourceImages.InsertRange(0, data.images)
69 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
70 alteredSinceLastSave <- false
71
72 /// <summary>
73 /// </summary>
74 /// <exception cref="System.IOException">If the image cannot be read</exception>
75 member this.AddSourceImage (filePath: string) (defaultConfig: ParasitemiaCore.Config.Config) : SourceImage =
76 let srcImg =
77 { num = sourceImages.Count + 1
78 config = defaultConfig.Copy()
79 dateLastAnalysis = DateTime(0L)
80 rbcs = []
81 img = new Image<Bgr, byte>(filePath)
82 healthyRBCBrightness = 1.f
83 infectedRBCBrightness = 1.f }
84
85 sourceImages.Add(srcImg)
86 if sourceImages.Count = 1
87 then this.CurrentImage <- Some sourceImages.[0]
88 alteredSinceLastSave <- true
89 srcImg
90
91 member this.RemoveSourceImage (srcImg: SourceImage) =
92 let isCurrent =
93 match this.CurrentImage with
94 | Some srcImg' -> srcImg = srcImg'
95 | _ -> false
96
97 if sourceImages.Remove(srcImg)
98 then
99 alteredSinceLastSave <- true
100 if isCurrent
101 then
102 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
103 // Re-numbered the images.
104 sourceImages |> Seq.iteri (fun i srcImg -> srcImg.num <- i + 1)
105
106 member this.SetResult (imgNum: int) (cells: ParasitemiaCore.Types.Cell list) =
107 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
108
109 let w = sourceImage.img.Width
110 let h = sourceImage.img.Height
111
112 sourceImage.dateLastAnalysis <- DateTime.UtcNow
113
114 // To match with previously manually altered RBC.
115 let manuallyAlteredPreviousRBCS = sourceImage.rbcs |> List.filter (fun rbc -> rbc.setManually)
116 let tolerance = (float sourceImage.config.RBCRadius.Pixel) * 0.5 // +/-.
117 let getPreviousRBC (center: Point) : RBC option =
118 manuallyAlteredPreviousRBCS |> List.tryFind (fun rbc -> rbc.center.X > center.X - tolerance && rbc.center.X < center.X + tolerance &&
119 rbc.center.Y > center.Y - tolerance && rbc.center.Y < center.Y + tolerance)
120
121 sourceImage.rbcs <- cells
122 |> List.filter (fun cell -> match cell.cellClass with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
123 |> List.sortByDescending (fun cell -> cell.infectedArea, (w - cell.center.X) + (h - cell.center.Y))
124 |> List.mapi (fun i cell ->
125 let center = Point(float cell.center.X, float cell.center.Y)
126 let infected, setManually =
127 match getPreviousRBC center with
128 | Some rbc -> rbc.infected, true
129 | _ -> cell.cellClass = ParasitemiaCore.Types.InfectedRBC, false
130
131 { num = i + 1
132 infected = infected
133 setManually = setManually
134 center = center
135 size = Size(float cell.elements.Width, float cell.elements.Height)
136 infectedArea = cell.infectedArea })
137
138 alteredSinceLastSave <- true
139
140 member this.SourceImages : SourceImage seq =
141 sourceImages :> SourceImage seq
142
143 member this.Reset () =
144 this.PatientID <- ""
145 this.FilePath <- ""
146 this.CurrentImage <- None
147 sourceImages.Clear()
148 alteredSinceLastSave <- false