Simplification of the parasite marker.
[master-thesis.git] / Parasitemia / Parasitemia / ParasitesMarker.fs
1 module ParasitesMarker
2
3 open System.Drawing
4 open System.Linq
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8 open Utils
9
10 type Result = {
11 darkStain: Image<Gray, byte>
12 infection: Image<Gray, byte>
13 stain: Image<Gray, byte> }
14
15
16 // Create three binary markers :
17 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
18 // * 'Stain' corresponds to the stain around the parasites.
19 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
20 let findMa (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
21
22 // We use the filtered image to find the dark stain.
23 let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians filteredGreen 1.0)
24 let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
25 let darkStain = d_fg.Cmp(median_bg * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
26 darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan))
27 darkStain._And(fg)
28
29 let fgFloat = (fg / 255.0).Convert<Gray, float32>()
30 use greenWithoutBg = ImgTools.gaussianFilter green 1.0
31 greenWithoutBg.SetValue(Gray(0.0), fg.Not())
32
33 let findSmears (sigma: float) (level: float) : Image<Gray, byte> =
34 use greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma
35 use fgSmoothed = ImgTools.gaussianFilter fgFloat sigma
36 let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan)
37 smears._And(fg)
38 smears
39
40 let tmp = filteredGreen.Convert<Gray, byte>()
41
42 { darkStain = darkStain;
43 stain = findSmears 10. 0.9
44 infection = findSmears 2.2 0.87 },
45 tmp,
46 tmp
47
48
49 // Create three binary markers :
50 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
51 // * 'Stain' corresponds to the stain around the parasites.
52 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
53 let find (filteredGreen: Image<Gray, byte>) (filteredGreenFloat: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
54
55 let filteredGreenWithoutInfection = filteredGreen.Copy()
56 ImgTools.areaClose filteredGreenWithoutInfection (int config.InfectionArea)
57
58 let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
59 ImgTools.areaClose filteredGreenWithoutStain (int config.StainArea)
60
61 // We use the filtered image to find the dark stain.
62 let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians (filteredGreenWithoutInfection.Convert<Gray, float32>()) 1.0) // FIXME: avoid converting again this in MainAnalysis
63 let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
64 let darkStain = d_fg.Cmp(median_bg * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
65 darkStain._And(filteredGreenWithoutInfection.Cmp(median_fg, CvEnum.CmpType.LessThan))
66
67 let marker (img: Image<Gray, byte>) (closed: Image<Gray, byte>) (level: float) : Image<Gray, byte> =
68 let diff = closed - (img * level)
69 diff._ThresholdBinary(Gray(0.0), Gray(255.))
70 diff
71
72 let infectionMarker = marker filteredGreen filteredGreenWithoutInfection config.Parameters.infectionLevel
73 let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain config.Parameters.stainLevel
74
75 { darkStain = darkStain
76 infection = infectionMarker
77 stain = stainMarker },
78 filteredGreenWithoutInfection,
79 filteredGreenWithoutStain
80
81
82
83
84
85
86
87