module ParasitesMarker2 open System.Drawing open Emgu.CV open Emgu.CV.Structure type Result = { darkStain: Image infection: Image stain: Image } // 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) (filteredGreenFloat: Image) (kmediansResult: KMedians.Result) (config: Config.Config) : Result * Image * Image = // We use the filtered image to find the dark stain. let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResult let darkStain = d_fg.Cmp(median_bg * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan) darkStain._And(filteredGreenFloat.Cmp(median_fg, CvEnum.CmpType.LessThan)) let marker (area: int) (threshold: float) : Image * Image = let closed = filteredGreen.Copy() ImgTools.areaClose closed area let diff = closed - filteredGreen let min = ref [| 0. |] let minLocation = ref <| [| Point() |] let max = ref [| 0. |] let maxLocation = ref <| [| Point() |] diff.MinMax(min, max, minLocation, maxLocation) let valueThreshold = if (!max).[0] * threshold < 0.1 * (median_bg - median_fg) then 0.1 * (median_bg - median_fg) else (!max).[0] * threshold diff._ThresholdBinary(Gray(valueThreshold), Gray(255.)) diff, closed let infectionMarker, filteredGreenWithoutInfection = marker (int config.InfectionArea) config.Parameters.infectionLevel let stainMarker, filteredGreenWithoutStain = marker (int config.StainArea) config.Parameters.stainLevel { darkStain = darkStain infection = infectionMarker stain = stainMarker }, filteredGreenWithoutInfection, filteredGreenWithoutStain