Project the colors to have the best contrast for RBCs and parasites analyze.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Config.fs
1 module ParasitemiaCore.Config
2
3 open System
4
5 open Const
6 open UnitsOfMeasure
7
8 type Debug =
9 | DebugOff
10 | DebugOn of string // Output directory.
11
12 type Parameters = {
13 rbcDiameter: float<μm>
14 resolution: float<ppi>
15
16 colorContribution_BG_RBC: float * float * float // (R, G, B).
17 colorContribution_RBC_parasite: float * float * float // (R, G, B).
18
19 ratioAreaPaleCenter: float32 // The area of the second opening is 'ratioSecondAreaOpen' * mean RBC area. It's applied only if greater than 'initialAreaOpen'.
20
21 granulometryRange: float32 // The radius will be seeked from radius - granulometryRange * radius to radius + granulometryRange * radius.
22
23 minRbcRadius: float32 // Factor of the mean RBC radius.
24 maxRbcRadius: float32 // Factor of the mean RBC radius.
25
26 LPFStandardDeviationParasite: float<μm> // Sigma parameter of the gaussian to remove the high frequency noise.
27 LPFStandardDeviationStain: float<μm>
28 LPFStandardDeviationRBC: float<μm>
29
30 // Ellipse.
31 factorNbPick: float // The number of computed ellipse per edge pixel.
32
33 // Parasites detection.
34 darkStainLevel: float // Lower -> more sensitive. Careful about illumination on the borders.
35 maxDarkStainRatio: float // When a cell must own less than this ratio to be a RBC.
36
37 parasiteRadiusRatio: float32 // The ratio of the parasite radius of the RBC radius.
38
39 minimumParasiteAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
40 cytoplasmSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
41
42 nucleusAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
43 infectionSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
44
45 standardDeviationMaxRatio: float // The standard deviation of the pixel values of a cell can't be greater than standardDeviationMaxRatio * global standard deviation
46 minimumCellAreaFactor: float32 // Factor of the mean RBC area. A cell with an area below this will be rejected.
47 }
48
49 let defaultParameters = {
50 rbcDiameter = 8.<μm>
51 resolution = 220.e3<ppi> // 220.e3<ppi> Correspond to 50X.
52
53 colorContribution_BG_RBC = (* 0., 1., 0. *) 0.16, 0.44, 0.4
54 colorContribution_RBC_parasite = (* 1., 0., 0. *) 0.54, 0.41, 0.05
55
56 ratioAreaPaleCenter = 2.f / 5.f // The ratio between an RBC area and the area of the its pale center.
57
58 granulometryRange = 0.5f
59
60 minRbcRadius = -0.3f
61 maxRbcRadius = 0.3f
62
63 LPFStandardDeviationParasite = 0.15<μm>
64 LPFStandardDeviationStain = 0.15<μm> // 0.12
65 LPFStandardDeviationRBC = 0.2<μm> // 8.5e-6<inch>. // 0.2<μm>
66
67 factorNbPick = 1.0
68
69 darkStainLevel = 0.25 // 0.3
70 maxDarkStainRatio = 0.1 // 10 %
71
72 parasiteRadiusRatio = 0.5f // 40 %
73
74 minimumParasiteAreaRatio = 0.02f // 2 %
75 cytoplasmSensitivity = 0.96 // 1) 0.91, 2) 0.92
76
77 nucleusAreaRatio = 0.01f // 1.0 %
78 infectionSensitivity = 0.9 // 1) 0.93, 2) 0.94
79
80 standardDeviationMaxRatio = 0.5 // 0.5
81 minimumCellAreaFactor = 0.4f }
82
83 type RBCRadius (radius: float32, parameters: Parameters) =
84 member this.Pixel = radius
85 member this.μm : float<μm> =
86 1.<px> * (float radius) / parameters.resolution |> inchToμm
87
88 member this.Min = radius + parameters.minRbcRadius * radius
89 member this.Max = radius + parameters.maxRbcRadius * radius
90
91 member this.Area = PI * radius ** 2.f
92 member this.MinArea = parameters.minimumCellAreaFactor * this.Area
93
94 member this.ParasiteRadius = parameters.parasiteRadiusRatio * radius
95
96 member this.NucleusArea = parameters.nucleusAreaRatio * this.Area
97 member this.MinimumParasiteArea = parameters.minimumParasiteAreaRatio * this.Area
98
99 override this.ToString() =
100 sprintf "%d px (%.1f μm)" (Utils.roundInt <| 2.f * radius) (2. * this.μm)
101
102 type Config (param: Parameters) =
103 let RBCadiusInPixels (rbcDiameter: float<μm>) (resolution: float<ppi>) : float32 =
104 let rbcRadiusInch: float<inch> = (μmToInch rbcDiameter) / 2.
105 let rbcRadiusPx: float<px> = resolution * rbcRadiusInch
106 float32 rbcRadiusPx
107
108 let mutable parameters: Parameters = param
109 let mutable rbcRadiusByResolution = RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, parameters)
110 let mutable rbcRadius = RBCRadius(0.f, parameters)
111
112 new () = Config(defaultParameters)
113
114 member this.Parameters
115 with get() = parameters
116 and set(param) =
117 parameters <- param
118 rbcRadiusByResolution <- RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, param)
119 rbcRadius <- RBCRadius(rbcRadius.Pixel, param)
120
121 member val Debug = DebugOff with get, set
122
123 member this.LPFStandardDeviationParasite =
124 let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationParasite) * parameters.resolution
125 float stdDeviation
126
127 member this.LPFStandardDeviationStain =
128 let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationStain) * parameters.resolution
129 float stdDeviation
130
131 member this.LPFStandardDeviationRBC =
132 let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationRBC) * parameters.resolution
133 float stdDeviation
134
135 member this.RBCRadiusByResolution = rbcRadiusByResolution
136 member this.RBCRadius = rbcRadius
137
138 member this.SetRBCRadius (radiusPixel: float32) =
139 rbcRadius <- RBCRadius(radiusPixel, parameters)
140
141 member this.Copy () =
142 this.MemberwiseClone() :?> Config
143