73f9fe1da720cc9fe8bb215c823ebee636f87ee5
1
module ParasitemiaCore.ParasitesMarker
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 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 = 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))
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())
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)
39 let tmp = filteredGreen.Convert<Gray, byte
>()
41 { darkStain = darkStain;
42 stain = findSmears 10. 0.9
43 infection
= findSmears 2.2
0.87 },
47 // Create three binary markers :
48 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
49 // * 'Stain' corresponds to the stain around the parasites.
50 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
51 let find (img
: Image<Gray, float32
>) (config: Config.Config) : Result * Image<Gray, float32
> * Image<Gray, float32
> =
53 let imgFilteredInfection = ImgTools.gaussianFilter img
config.LPFStandardDeviationParasite
54 let filteredGreenWithoutInfection = imgFilteredInfection.Copy()
55 ImgTools.areaCloseF
filteredGreenWithoutInfection (roundInt
config.RBCRadius.NucleusArea)
58 let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
59 ImgTools.areaCloseF filteredGreenWithoutStain (int config.RBCRadius.StainArea) *)
62 // We use the filtered image to find the dark stain.
63 let _, mean_fg
, mean_bg
=
64 let hist = ImgTools.histogramImg
filteredGreenWithoutInfection 300
66 filteredGreenWithoutInfection.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel
+ (float mean_fg), CvEnum.CmpType.LessThan)
68 let marker (img
: Image<Gray, float32
>) (closed
: Image<Gray, float32
>) (level
: float) : Image<Gray, byte
> =
71 CvInvoke.Subtract(closed
, diff, diff)
72 diff._ThresholdBinary
(Gray(0.0), Gray(255.))
73 diff.Convert<Gray, byte
>()
75 let infectionMarker = marker imgFilteredInfection filteredGreenWithoutInfection (1. / config.Parameters.infectionSensitivity
)
77 let imgFilteredStain = ImgTools.gaussianFilter img
config.LPFStandardDeviationStain
78 let areaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
79 //ImgTools.areaOpenF imgFilteredStain areaOpening
81 let filteredGreenWithoutStain = imgFilteredStain.CopyBlank()
83 let size = roundInt
(config.RBCRadius.Pixel / 5.f
)
84 if size % 2 = 0 then size + 1 else size
88 CvInvoke.GetStructuringElement(CvEnum.ElementShape.Rectangle, Size(3, 3), Point(-1, -1))
90 CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(kernelSize, kernelSize), Point(-1, -1))
92 CvInvoke.MorphologyEx(imgFilteredStain, filteredGreenWithoutStain, CvEnum.MorphOp.Close, kernel, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar())
93 let stainMarker = marker (*filteredGreenWithoutInfection*) imgFilteredStain filteredGreenWithoutStain (1. / config.Parameters.cytoplasmSensitivity
)
96 (*let blackTopHat = filteredGreenWithoutStain.CopyBlank()
97 CvInvoke.Subtract(filteredGreenWithoutStain, imgFilteredStain, blackTopHat)
98 ImgTools.saveImg blackTopHat "blackTopHat.png"*)
100 { darkStain = darkStain
101 infection
= infectionMarker
102 stain = stainMarker },
103 filteredGreenWithoutStain,
104 filteredGreenWithoutInfection