beginning of the classifier.
[master-thesis.git] / Parasitemia / Parasitemia / ImageAnalysis.fs
1 module ImageAnalysis
2
3 open System
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Utils
10 open ImgTools
11 open Config
12 open Types
13
14 (*type Result = {
15 RBCPositions : Point list
16 infectedRBCPositions : Point list
17 img: Image<Bgr, byte>
18 }*)
19
20 let doAnalysis (img: Image<Bgr, byte>) (config: Config) : Classifier.Cell list =
21
22 let imgFloat = img.Convert<Bgr, float32>()
23 use scaledImg = if config.scale = 1.0 then imgFloat else imgFloat.Resize(config.scale, CvEnum.Inter.Area)
24
25 (*use scaledImg =
26 if config.scale = 1.0
27 then
28 img
29 else
30 let m = new Mat()
31 CvInvoke.Resize(img, m, Size(roundInt (float img.Size.Width * config.scale), roundInt (float img.Size.Height * config.scale)))
32 m*)
33
34 use green = scaledImg.Item(1)
35
36 //use green = new Matrix<byte>(scaledImg.Size)
37 //CvInvoke.MixChannels(scaledImg, green, [| 1; 0 |])
38
39 //let greenMatrix = new Matrix<byte>(green.Height, green.Width, green.DataPointer)
40
41 //let test = greenMatrix.[10, 10]
42
43 use filteredGreen = (gaussianFilter green config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter green config.doGSigma2)
44
45 use sobelKernel =
46 new ConvolutionKernelF(array2D [[ 1.0f; 0.0f; -1.0f ]
47 [ 2.0f; 0.0f; -2.0f ]
48 [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
49
50 use xEdges = filteredGreen.Convolution(sobelKernel).Convert<Gray, float>()
51 use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
52
53 let xEdgesData = xEdges.Data
54 let yEdgesData = yEdges.Data
55 for r in 0..xEdges.Rows-1 do
56 xEdgesData.[r, 0, 0] <- 0.0
57 xEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
58 yEdgesData.[r, 0, 0] <- 0.0
59 yEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
60
61 for c in 0..xEdges.Cols-1 do
62 xEdgesData.[0, c, 0] <- 0.0
63 xEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
64 yEdgesData.[0, c, 0] <- 0.0
65 yEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
66
67 use magnitudes = new Matrix<float>(xEdges.Size)
68 CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
69
70 let min = ref 0.0
71 let minLocation = ref <| Point()
72 let max = ref 0.0
73 let maxLocation = ref <| Point()
74 magnitudes.MinMax(min, max, minLocation, maxLocation)
75
76 use magnitudesByte = ((magnitudes / !max) * 255.0).Convert<byte>() // Otsu from OpenCV only support 'byte'.
77 use edges = new Matrix<byte>(xEdges.Size)
78 CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore
79
80 logTime "Finding edges" (fun() ->
81 thin edges)
82
83 logTime "Removing small connected components" (fun () ->
84 removeArea edges 12)
85
86 saveMat (edges * 255.0) "edges.png"
87
88
89 let kmediansResults = KMedians.kmedians filteredGreen 1.0
90
91 let parasites = ParasitesMarker.find green filteredGreen kmediansResults config
92
93 saveImg parasites.darkStain "parasites_dark_stain.png"
94 saveImg parasites.stain "parasites_stain.png"
95 saveImg parasites.infection "parasites_infection.png"
96
97 let radiusRange = config.scale * 20.0, config.scale * 40.0
98 let windowSize = roundInt (1.6 * (snd radiusRange))
99 let factorNbPick = 1.5
100 let ellipses = logTime "Finding ellipses" (fun () ->
101 Ellipse.find edges xEdges yEdges radiusRange windowSize factorNbPick)
102
103 drawEllipses img ellipses (Bgr(0.0, 255.0, 255.0))
104 //saveImg img "ellipses.png"
105
106 Classifier.findCells ellipses parasites kmediansResults.fg
107
108 //
109
110 (*use imageHSV = scaledImage.Convert<Hsv, uint8>()
111 let H, S = match imageHSV.Split() with // Warning: H is from 0 to 179°.
112 | [| H; S; _|] -> H, S
113 | _ -> failwith "unable to split the HSV channels"
114
115 let hueShiftValue = 175
116 // Modulo operator doesn't exist on matrix thus we have to apply a function to every pixels.
117 let correctedH : Image<Gray, byte> = H.Convert(fun b _ _ ->
118 (255 - int(b) * 255 / 179 + hueShiftValue) % 256 |> byte
119 )
120
121 let correctedS : Image<Gray, byte> = S.Not()
122
123 let filteredH = correctedH.SmoothMedian(5)
124 let filteredS = correctedS.SmoothMedian(5)*)
125
126 //let greenChannel = scaledImage.Item(1)
127
128 //let filteredImage = (gaussianFilter greenChannel config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter greenChannel config.doGSigma2)
129
130 // let filteredImage = greenChannel.ThresholdAdaptive(Gray(255.), CvEnum.AdaptiveThresholdType.GaussianC, CvEnum.ThresholdType.Binary, 61, Gray(5.0))
131 // let thresholdedImage = filteredImage.CopyBlank()
132
133 // CvInvoke.Threshold(filteredImage, thresholdedImage, 0., 255., CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.BinaryInv) |> ignore
134
135 // filteredImage <|