1
module ParasitesMarker
11 darkStain
: Image<Gray, byte
>
12 infection
: Image<Gray, byte
>
13 stain
: Image<Gray, byte
> }
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
> =
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))
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())
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)
40 let tmp = filteredGreen.Convert<Gray, byte
>()
42 { darkStain = darkStain;
43 stain = findSmears 10. 0.9
44 infection
= findSmears 2.2
0.87 },
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
> =
55 let filteredGreenWithoutInfection = filteredGreen.Copy()
56 ImgTools.areaClose
filteredGreenWithoutInfection (int config.InfectionArea)
58 let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
59 ImgTools.areaClose
filteredGreenWithoutStain (int config.StainArea)
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))
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.))
72 let infectionMarker = marker filteredGreen filteredGreenWithoutInfection config.Parameters.infectionLevel
73 let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain config.Parameters.stainLevel
75 { darkStain = darkStain
76 infection
= infectionMarker
77 stain = stainMarker },
78 filteredGreenWithoutInfection,
79 filteredGreenWithoutStain