Add a DPI calculator to help to find the correct image resolution.
[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 ratioAreaPaleCenter: float32 // The area of the second opening is 'ratioSecondAreaOpen' * mean RBC area. It's applied only if greater than 'initialAreaOpen'.
17
18 granulometryRange: float32 // The radius will be seeked from radius - granulometryRange * radius to radius + granulometryRange * radius.
19
20 minRbcRadius: float32 // Factor of the mean RBC radius.
21 maxRbcRadius: float32 // Factor of the mean RBC radius.
22
23 LPFStandardDeviationParasite: float<μm> // Sigma parameter of the gaussian to remove the high frequency noise.
24 LPFStandardDeviationRBC: float<μm>
25
26 // Ellipse.
27 factorNbPick: float // The number of computed ellipse per edge pixel.
28
29 // Parasites detection.
30 darkStainLevel: float // Lower -> more sensitive. Careful about illumination on the borders.
31 maxDarkStainRatio: float // When a cell must own less than this ratio to be a RBC.
32
33 parasiteRadiusRatio: float32 // The ratio of the parasite radius of the RBC radius.
34 minimumParasiteAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
35
36 cytoplasmSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
37
38 nucleusAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
39 infectionSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
40
41 standardDeviationMaxRatio: float // The standard deviation of the pixel values of a cell can't be greater than standardDeviationMaxRatio * global standard deviation
42 minimumCellAreaFactor: float32 } // Factor of the mean RBC area. A cell with an area below this will be rejected.
43
44 let defaultParameters = {
45 rbcDiameter = 7.5<μm>
46 resolution = 220.e3<ppi> // 220.e3<ppi> Correspond to 50X.
47
48 ratioAreaPaleCenter = 2.f / 5.f // The ratio between an RBC area and the area of the its pale center.
49
50 granulometryRange = 0.5f
51
52 minRbcRadius = -0.3f
53 maxRbcRadius = 0.3f
54
55 LPFStandardDeviationParasite = 0.15<μm>
56 LPFStandardDeviationRBC = 0.2<μm>
57
58 factorNbPick = 1.0
59
60 darkStainLevel = 0.25
61 maxDarkStainRatio = 0.1 // 10 %
62
63 parasiteRadiusRatio = 0.5f // 40 %
64
65 minimumParasiteAreaRatio = 0.02f // 2 %
66 cytoplasmSensitivity = 0.96
67
68 nucleusAreaRatio = 0.01f // 1.0 %
69 infectionSensitivity = 0.92
70
71 standardDeviationMaxRatio = 0.6
72 minimumCellAreaFactor = 0.4f }
73
74 type RBCRadius (radius: float32, parameters: Parameters) =
75 member this.Pixel = radius
76 member this.μm : float<μm> =
77 1.<px> * (float radius) / parameters.resolution |> inchToμm
78
79 member this.Min = radius + parameters.minRbcRadius * radius
80 member this.Max = radius + parameters.maxRbcRadius * radius
81
82 member this.Area = PI * radius ** 2.f
83 member this.MinArea = parameters.minimumCellAreaFactor * this.Area
84
85 member this.ParasiteRadius = parameters.parasiteRadiusRatio * radius
86
87 member this.NucleusArea = parameters.nucleusAreaRatio * this.Area
88 member this.MinimumParasiteArea = parameters.minimumParasiteAreaRatio * this.Area
89
90 override this.ToString() =
91 sprintf "%d px (%.1f μm)" (Utils.roundInt <| 2.f * radius) (2. * this.μm)
92
93 type Config (param: Parameters) =
94 let RBCadiusInPixels (rbcDiameter: float<μm>) (resolution: float<ppi>) : float32 =
95 let rbcRadiusInch: float<inch> = (μmToInch rbcDiameter) / 2.
96 let rbcRadiusPx: float<px> = resolution * rbcRadiusInch
97 float32 rbcRadiusPx
98
99 let mutable parameters: Parameters = param
100 let mutable rbcRadiusByResolution = RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, parameters)
101 let mutable rbcRadius = RBCRadius(0.f, parameters)
102
103 new () = Config(defaultParameters)
104
105 member this.Parameters
106 with get() = parameters
107 and set(param) =
108 parameters <- param
109 rbcRadiusByResolution <- RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, param)
110 rbcRadius <- RBCRadius(rbcRadius.Pixel, param)
111
112 member val Debug = DebugOff with get, set
113
114 member this.LPFStandardDeviationParasite =
115 let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationParasite) * parameters.resolution
116 float stdDeviation
117
118 member this.LPFStandardDeviationRBC =
119 let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationRBC) * parameters.resolution
120 float stdDeviation
121
122 member this.RBCRadiusByResolution = rbcRadiusByResolution
123 member this.RBCRadius = rbcRadius
124
125 member this.SetRBCRadius (radiusPixel: float32) =
126 rbcRadius <- RBCRadius(radiusPixel, parameters)
127
128 member this.Copy () =
129 this.MemberwiseClone() :?> Config
130