Remove the parasite detection function from Ma.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ParasitesMarker.fs
1 module ParasitemiaCore.ParasitesMarker
2
3 open System.Drawing
4 open System.Linq
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Utils
10
11 type Result = {
12 darkStain: Image<Gray, byte> // Colored pixel, it's independent of the size of the areas. It corresponds to white cells, schizontes, gametocytes, throphozoites.
13 nucleus: Image<Gray, byte> // Parasite nucleus. It may contain some debris. It shouldn't contain thrombocytes or larger elements.
14 cytoplasm: Image<Gray, byte> } // Parasite cytoplasm.
15
16 let find (img: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, float32> * Image<Gray, float32> =
17
18 let imgFilteredParasite = ImgTools.gaussianFilter img config.LPFStandardDeviationParasite
19
20 let filteredGreenWithoutNucleus = imgFilteredParasite.Copy()
21 ImgTools.areaCloseF filteredGreenWithoutNucleus (roundInt config.RBCRadius.NucleusArea)
22
23 let darkStain =
24 // We use the filtered image to find the dark stain.
25 let _, mean_fg, mean_bg =
26 let hist = ImgTools.histogramImg filteredGreenWithoutNucleus 300
27 ImgTools.otsu hist
28 filteredGreenWithoutNucleus.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel + (float mean_fg), CvEnum.CmpType.LessThan)
29
30 let marker (img: Image<Gray, float32>) (closed: Image<Gray, float32>) (level: float) : Image<Gray, byte> =
31 let diff = img.Copy()
32 diff._Mul(level)
33 CvInvoke.Subtract(closed, diff, diff)
34 diff._ThresholdBinary(Gray(0.0), Gray(255.))
35 diff.Convert<Gray, byte>()
36
37 let nucleusMarker = marker imgFilteredParasite filteredGreenWithoutNucleus (1. / config.Parameters.infectionSensitivity)
38
39 let filteredGreenWithoutCytoplasm = imgFilteredParasite.CopyBlank()
40 let kernelSize =
41 let size = roundInt (config.RBCRadius.Pixel / 5.f)
42 if size % 2 = 0 then size + 1 else size
43 use kernel =
44 if kernelSize <= 3
45 then
46 CvInvoke.GetStructuringElement(CvEnum.ElementShape.Rectangle, Size(3, 3), Point(-1, -1))
47 else
48 CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(kernelSize, kernelSize), Point(-1, -1))
49
50 CvInvoke.MorphologyEx(imgFilteredParasite, filteredGreenWithoutCytoplasm, CvEnum.MorphOp.Close, kernel, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar())
51 let cytoplasmMarker = marker imgFilteredParasite filteredGreenWithoutCytoplasm (1. / config.Parameters.cytoplasmSensitivity)
52
53 { darkStain = darkStain
54 nucleus = nucleusMarker
55 cytoplasm = cytoplasmMarker },
56 filteredGreenWithoutCytoplasm,
57 filteredGreenWithoutNucleus
58
59