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