42645b4899f6a6ae55d4d7a02bb027d2dca1a65d
1
module ParasitesMarker2
12 darkStain
: Image<Gray, byte
>
13 infection
: Image<Gray, byte
>
14 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 find (filteredGreen
: Image<Gray, byte
>) (filteredGreenFloat
: Image<Gray, float32
>) (config
: Config.Config) : Result * Image<Gray, byte
> * Image<Gray, byte
> =
22 let filteredGreenWithoutInfection = filteredGreen
.Copy()
23 ImgTools.areaClose
filteredGreenWithoutInfection (int config.InfectionArea)
25 let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
26 ImgTools.areaClose
filteredGreenWithoutStain (int config.StainArea)
28 // We use the filtered image to find the dark stain.
29 let kmediansResults = logTime
"Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians
(filteredGreenWithoutInfection.Convert<Gray, float32
>()) 1.0) // FIXME: avoid converting again this in MainAnalysis
30 let { KMedians.fg
= fg
; KMedians.median_bg
= median_bg
; KMedians.median_fg
= median_fg
; KMedians.d_fg
= d_fg
} = kmediansResults
31 let darkStain = d_fg
.Cmp(median_bg
* config.Parameters.darkStainLevel
, CvEnum.CmpType.GreaterThan)
32 darkStain._And
(filteredGreenWithoutInfection.Cmp(median_fg
, CvEnum.CmpType.LessThan))
34 let marker (img
: Image<Gray, byte
>) (closed
: Image<Gray, byte
>) (level
: float) : Image<Gray, byte
> =
35 let diff = closed
- img
37 let min = ref [| 0. |]
38 let minLocation = ref <| [| Point() |]
39 let max = ref [| 0. |]
40 let maxLocation = ref <| [| Point() |]
41 diff.MinMax(min, max, minLocation, maxLocation)
44 let threshold = 0.2 * max
45 let diff' = diff - threshold
47 let m = MathNet.Numerics.Statistics.StreamingStatistics.Mean(seq {
48 for m in ImgTools.findMaxima diff' do
50 yield
diff'.Data.[p.Y, p.X, 0] |> float }) + threshold
52 let valueThreshold = m * level
54 diff._ThresholdBinary(Gray(valueThreshold), Gray(255.))
57 let infectionMarker = marker filteredGreen filteredGreenWithoutInfection config.Parameters.infectionLevel
58 let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain config.Parameters.stainLevel
60 { darkStain = darkStain
61 infection = infectionMarker
62 stain = stainMarker },
63 filteredGreenWithoutInfection,
64 filteredGreenWithoutStain