-module ParasitesMarker2
-
-open System.Drawing
-open System.Linq
-
-open Emgu.CV
-open Emgu.CV.Structure
-
-open Utils
-
-type Result = {
- darkStain: Image<Gray, byte>
- infection: Image<Gray, byte>
- stain: Image<Gray, byte> }
-
-// Create three binary markers :
-// * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
-// * 'Stain' corresponds to the stain around the parasites.
-// * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
-let find (filteredGreen: Image<Gray, byte>) (filteredGreenFloat: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
-
- let filteredGreenWithoutInfection = filteredGreen.Copy()
- ImgTools.areaClose filteredGreenWithoutInfection (int config.InfectionArea)
-
- let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
- ImgTools.areaClose filteredGreenWithoutStain (int config.StainArea)
-
- // We use the filtered image to find the dark stain.
- let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians (filteredGreenWithoutInfection.Convert<Gray, float32>()) 1.0) // FIXME: avoid converting again this in MainAnalysis
- let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
- let darkStain = d_fg.Cmp(median_bg * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
- darkStain._And(filteredGreenWithoutInfection.Cmp(median_fg, CvEnum.CmpType.LessThan))
-
- let marker (img: Image<Gray, byte>) (closed: Image<Gray, byte>) (level: float) : Image<Gray, byte> =
- let diff = closed - img
-
- let min = ref [| 0. |]
- let minLocation = ref <| [| Point() |]
- let max = ref [| 0. |]
- let maxLocation = ref <| [| Point() |]
- diff.MinMax(min, max, minLocation, maxLocation)
- let max = (!max).[0]
-
- let threshold = 0.2 * max
- let diff' = diff - threshold
-
- let m = MathNet.Numerics.Statistics.StreamingStatistics.Mean(seq {
- for m in ImgTools.findMaxima diff' do
- let p = m.First()
- yield diff'.Data.[p.Y, p.X, 0] |> float }) + threshold
-
- let valueThreshold = m * level
-
- diff._ThresholdBinary(Gray(valueThreshold), Gray(255.))
- diff
-
- let infectionMarker = marker filteredGreen filteredGreenWithoutInfection config.Parameters.infectionLevel
- let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain config.Parameters.stainLevel
-
- { darkStain = darkStain
- infection = infectionMarker
- stain = stainMarker },
- filteredGreenWithoutInfection,
- filteredGreenWithoutStain
-
-
-
-
-