* Add an exact method to compute an ellipse from three points and two tangents.
[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 // Create three binary markers :
16 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
17 // * 'Stain' corresponds to the stain around the parasites.
18 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
19 let findMa (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
20 // We use the filtered image to find the dark stain.
21 let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians filteredGreen)
22 let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
23 let darkStain = d_fg.Cmp(median_bg * float config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
24 darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan))
25 darkStain._And(fg)
26
27 let fgFloat = (fg / 255.0).Convert<Gray, float32>()
28 use greenWithoutBg = ImgTools.gaussianFilter green 1.0
29 greenWithoutBg.SetValue(Gray(0.0), fg.Not())
30
31 let findSmears (sigma: float) (level: float) : Image<Gray, byte> =
32 use greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma
33 use fgSmoothed = ImgTools.gaussianFilter fgFloat sigma
34 let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan)
35 smears._And(fg)
36 smears
37
38 let tmp = filteredGreen.Convert<Gray, byte>()
39
40 { darkStain = darkStain;
41 stain = findSmears 10. 0.9
42 infection = findSmears 2.2 0.87 },
43 tmp,
44 tmp
45
46 // Create three binary markers :
47 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
48 // * 'Stain' corresponds to the stain around the parasites.
49 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
50 let find (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, float32> =
51 use filteredGreenWithoutInfection = filteredGreen.Copy()
52 ImgTools.areaCloseF filteredGreenWithoutInfection (int config.RBCRadius.InfectionArea)
53
54 let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
55 ImgTools.areaCloseF filteredGreenWithoutStain (int config.RBCRadius.StainArea)
56
57 let darkStain =
58 // We use the filtered image to find the dark stain.
59 let _, mean_fg, mean_bg =
60 let hist = ImgTools.histogramImg filteredGreenWithoutInfection 300
61 ImgTools.otsu hist
62 filteredGreenWithoutInfection.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel + (float mean_fg), CvEnum.CmpType.LessThan)
63
64 let marker (img: Image<Gray, float32>) (closed: Image<Gray, float32>) (level: float) : Image<Gray, byte> =
65 let diff = img.Copy()
66 diff._Mul(level)
67 CvInvoke.Subtract(closed, diff, diff)
68 diff._ThresholdBinary(Gray(0.0), Gray(255.))
69 diff.Convert<Gray, byte>()
70
71 let infectionMarker = marker filteredGreen filteredGreenWithoutInfection (1. / config.Parameters.infectionSensitivity)
72 let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain (1. / config.Parameters.stainSensitivity)
73
74 // TODO: comprendre pourquoi des valeurs sont negatives!?!?
75 (*
76 let blackTopHat = filteredGreen.CopyBlank()
77 CvInvoke.Subtract(filteredGreenWithoutInfection, filteredGreen, blackTopHat)
78 ImgTools.saveImg (ImgTools.normalizeAndConvert blackTopHat) "BottomHat.png"
79 *)
80
81 { darkStain = darkStain
82 infection = infectionMarker
83 stain = stainMarker },
84 filteredGreenWithoutStain
85
86