44da9011d3b17ab2a458bf030ff2938aeb5466d8
[master-thesis.git] / Parasitemia / Parasitemia / ParasitesMarker.fs
1 module ParasitesMarker
2
3 open System.Drawing
4
5 open Emgu.CV
6 open Emgu.CV.Structure
7
8 type Result = {
9 darkStain: Image<Gray, byte>
10 stain: Image<Gray, byte>
11 infection: Image<Gray, byte> }
12
13 let find (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result =
14
15 // We use the filtered image to find the dark stain.
16 let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = KMedians.kmedians filteredGreen 1.0
17 let darkStain = d_fg.Cmp(median_bg * config.darkStainLevel, CvEnum.CmpType.GreaterThan)
18 darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan))
19 darkStain._And(fg)
20
21 let fgFloat = (fg / 255.0).Convert<Gray, float32>()
22 let greenWithoutBg = green.Copy()
23 greenWithoutBg.SetValue(Gray(0.0), fg.Not())
24
25 let findSmears (sigma: float) (level: float) : Image<Gray, byte> =
26 let greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma
27 let fgSmoothed = ImgTools.gaussianFilter fgFloat sigma
28
29 let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan)
30 smears._And(fg)
31 smears
32
33 { darkStain = darkStain;
34 stain = findSmears config.stainSigma config.stainLevel
35 infection = findSmears config.infectionSigma config.infectionLevel }
36
37
38
39