Use two radius in the configuration, one computed with the image resolution and one...
[master-thesis.git] / Parasitemia / Parasitemia / Config.fs
index a1b06e2..0916408 100644 (file)
 
 open System
 
+open Const
+open UnitsOfMeasure
+
 type Debug =
     | DebugOff
     | DebugOn of string // Output directory.
 
 type Parameters = {
-    scale: float
+    rbcDiameter: float<μm>
+    resolution: float<ppi>
+
+    ratioAreaPaleCenter: float32 // The area of the second opening is 'ratioSecondAreaOpen' * mean RBC area. It's applied only if greater than 'initialAreaOpen'.
 
-    minRbcRadius: float
-    maxRbcRadius: float
+    granulometryRange: float32 // The radius will be seeked from radius - granulometryRange * radius to radius + granulometryRange * radius.
 
-    preFilterSigma: float
+    minRbcRadius: float32 // Factor of the mean RBC radius.
+    maxRbcRadius: float32 // Factor of the mean RBC radius.
+
+    LPFStandardDeviation: float<μm> // Sigma parameter of the gaussian to remove the high frequency noise.
 
     // Ellipse.
-    factorNbPick: float
-    factorWindowSize: float // factor of 'maxRBCSize'.
+    factorNbPick: float // The number of computed ellipse per edge pixel.
 
     // Parasites detection.
-    darkStainLevel: float
-
-    stainArea: float // Factor of a RBC area. 0.5 means the half of RBC area.
-    stainLevel: float // [0, 1]
+    darkStainLevel: float // Lower -> more sensitive. Careful about illumination on the borders.
+    maxDarkStainRatio: float // When a cell must own less than this ratio to be a RBC.
 
-    infectionArea: float // Factor of a RBC area. 0.5 means the half of RBC area.
-    infectionLevel: float // [0, 1]
-    parasitePixelsRequired: int
+    stainArea: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
+    stainSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
+    maxStainRatio: float // A cell must own less than this ratio to be a RBC.
 
-    maxDarkStainRatio: float
+    infectionArea: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
+    infectionSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
 
-    minimumCellArea: float // Factor of RBC area.
+    standardDeviationMaxRatio: float // The standard deviation of the pixel values of a cell can't be greater than standardDeviationMaxRatio * global standard deviation
+    minimumCellAreaFactor: float32 // Factor of the mean RBC area. A cell with an area below this will be rejected.
 }
 
+let defaultParameters = {
+    rbcDiameter = 8.<μm>
+    resolution = 200.e3<ppi> // Correspond to 50X.
+
+    ratioAreaPaleCenter = 1.f / 3.f // The ratio between an RBC area and the area of the its pale center.
+
+    granulometryRange = 0.5f
+
+    minRbcRadius = -0.3f
+    maxRbcRadius = 0.3f
+
+    LPFStandardDeviation = 0.2<μm> // 8.5e-6<inch>.
+
+    factorNbPick = 1.0
+
+    darkStainLevel = 0.25 // 0.3
+    maxDarkStainRatio = 0.1 // 10 %
+
+    infectionArea = 0.012f // 1.2 %
+    infectionSensitivity = 0.9
+
+    stainArea = 0.08f // 8 %
+    stainSensitivity = 0.9
+    maxStainRatio = 0.12 // 12 %
+
+    standardDeviationMaxRatio = 0.5 // 0.5
+    minimumCellAreaFactor = 0.4f }
+
+type RBCRadius (radius: float32, parameters: Parameters) =
+    member this.Pixel = radius
+    member this.μm : float<μm> =
+        1.<px> * (float radius) / parameters.resolution |> inchToμm
+
+    member this.Min = radius + parameters.minRbcRadius * radius
+    member this.Max = radius + parameters.maxRbcRadius * radius
+
+    member this.Area = PI * radius ** 2.f
+    member this.MinArea = parameters.minimumCellAreaFactor * radius
+
+    member this.InfectionArea = parameters.infectionArea * this.Area
+    member this.StainArea = parameters.stainArea * this.Area
+
+    override this.ToString() =
+        sprintf "%d px (%.1f μm)" (Utils.roundInt <| 2.f * radius) (2. * this.μm)
+
+
 type Config (param: Parameters) =
-    member this.Parameters = param
+    let RBCadiusInPixels (rbcDiameter: float<μm>) (resolution: float<ppi>) : float32 =
+        let rbcRadiusInch: float<inch> = (μmToInch rbcDiameter) / 2.
+        let rbcRadiusPx: float<px> = resolution * rbcRadiusInch
+        float32 rbcRadiusPx
+
+    let mutable parameters: Parameters = param
+    let mutable rbcRadiusByResolution = RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, parameters)
+    let mutable rbcRadius = RBCRadius(0.f, parameters)
+
+    new () = Config(defaultParameters)
+
+    member this.Parameters
+        with get() = parameters
+        and set(param) =
+            parameters <- param
+            rbcRadiusByResolution <- RBCRadius(RBCadiusInPixels parameters.rbcDiameter parameters.resolution, param)
+            rbcRadius <- RBCRadius(rbcRadius.Pixel, param)
+
     member val Debug = DebugOff with get, set
-    member val RBCRadius = 30. with get, set
 
-    member this.RBCMinRadius = this.RBCRadius + param.minRbcRadius * this.RBCRadius
-    member this.RBCMaxRadius = this.RBCRadius + param.maxRbcRadius * this.RBCRadius
+    member this.LPFStandardDeviation =
+        let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviation) * parameters.resolution
+        float stdDeviation
+
+    member this.RBCRadiusByResolution = rbcRadiusByResolution
+    member this.RBCRadius = rbcRadius
+
+    member this.SetRBCRadius (radiusPixel: float32) =
+        rbcRadius <- RBCRadius(radiusPixel, parameters)
 
-    member this.RBCMinArea = param.minimumCellArea * Math.PI * this.RBCRadius ** 2.0
+    member this.Copy () =
+        this.MemberwiseClone() :?> Config
 
-    member this.InfectionArea = param.infectionArea * Math.PI * this.RBCRadius ** 2.0
-    member this.StainArea = param.stainArea * Math.PI * this.RBCRadius ** 2.0