17a032d81efc42a8c1435a0c2659e577364f9c03
[master-thesis.git] / Parasitemia / Parasitemia / ParasitesMarker2.fs
1 module ParasitesMarker2
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 infection: Image<Gray, byte>
11 stain: Image<Gray, byte> }
12
13 // Create three binary markers :
14 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
15 // * 'Stain' corresponds to the stain around the parasites.
16 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
17 let find (filteredGreen: Image<Gray, byte>) (filteredGreenFloat: Image<Gray, float32>) (kmediansResult: KMedians.Result) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
18
19 // We use the filtered image to find the dark stain.
20 let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResult
21 let darkStain = d_fg.Cmp(median_bg * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
22 darkStain._And(filteredGreenFloat.Cmp(median_fg, CvEnum.CmpType.LessThan))
23
24 let marker (area: int) (threshold: float) : Image<Gray, byte> * Image<Gray, byte> =
25 let closed = filteredGreen.Copy()
26 ImgTools.areaClose closed area
27 let diff = closed - filteredGreen
28
29 let min = ref [| 0. |]
30 let minLocation = ref <| [| Point() |]
31 let max = ref [| 0. |]
32 let maxLocation = ref <| [| Point() |]
33 diff.MinMax(min, max, minLocation, maxLocation)
34
35 let valueThreshold = if (!max).[0] * threshold < 0.1 * (median_bg - median_fg) then 0.1 * (median_bg - median_fg) else (!max).[0] * threshold
36
37 diff._ThresholdBinary(Gray(valueThreshold), Gray(255.))
38 diff, closed
39
40 let infectionMarker, filteredGreenWithoutInfection = marker (int config.InfectionArea) config.Parameters.infectionLevel
41 let stainMarker, filteredGreenWithoutStain = marker (int config.StainArea) config.Parameters.stainLevel
42
43 { darkStain = darkStain
44 infection = infectionMarker
45 stain = stainMarker },
46 filteredGreenWithoutInfection,
47 filteredGreenWithoutStain
48
49
50
51
52