module ParasitesMarker open System.Drawing open Emgu.CV open Emgu.CV.Structure type Result = { darkStain: Image stain: Image infection: 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 (green: Image) (filteredGreen: Image) (kmediansResult: KMedians.Result) (config: Config.Config) : Result = let green = ImgTools.gaussianFilter green 1.0 // 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.darkStainLevel, CvEnum.CmpType.GreaterThan) darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan)) darkStain._And(fg) let fgFloat = (fg / 255.0).Convert() let greenWithoutBg = green.Copy() greenWithoutBg.SetValue(Gray(0.0), fg.Not()) let findSmears (sigma: float) (level: float) : Image = let greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma let fgSmoothed = ImgTools.gaussianFilter fgFloat sigma let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan) smears._And(fg) smears { darkStain = darkStain; stain = findSmears config.stainSigma config.stainLevel infection = findSmears config.infectionSigma config.infectionLevel }