fd341ac5c44d22445229deba12fbe614dab61ab7
[master-thesis.git] / Parasitemia / Parasitemia / MainAnalysis.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
15 let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) : Cell list =
16
17 use scaledImg = if config.Parameters.scale = 1.0 then img else img.Resize(config.Parameters.scale, CvEnum.Inter.Area)
18
19 use green = scaledImg.Item(1)
20
21 let greenFloat = green.Convert<Gray, float32>()
22
23 let filteredGreen = gaussianFilter green config.Parameters.preFilterSigma
24
25 (*let maximaImg = filteredGreen.Copy()
26 let maxima = logTime "maxima" (fun () -> ImgTools.findMaxima maximaImg)
27 for m in maxima do
28 ImgTools.drawPoints maximaImg m 255uy
29
30 let greenOpen1 = filteredGreen.Copy()
31 logTime "areaOpen1" (fun () -> ImgTools.areaOpen greenOpen1 2000)*)
32
33 let initialAreaOpen = 2000
34 logTime "areaOpen 1" (fun () -> ImgTools.areaOpen filteredGreen 2000)
35
36 config.RBCRadius <- Granulometry.findRadius filteredGreen (10, 100) 0.5 |> float
37
38 let secondAreaOpen = int <| config.RBCArea / 3.
39 if secondAreaOpen > initialAreaOpen
40 then
41 logTime "areaOpen 2" (fun () -> ImgTools.areaOpen filteredGreen secondAreaOpen)
42
43 let filteredGreenFloat = filteredGreen.Convert<Gray, float32>() // Is it neccessary?
44
45 let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker2.find filteredGreen filteredGreenFloat config
46 let filteredGreenWhitoutInfectionFloat = filteredGreenWhitoutInfection.Convert<Gray, float32>()
47 let filteredGreenWhitoutStainFloat = filteredGreenWhitoutStain.Convert<Gray, float32>()
48
49 use sobelKernel =
50 new ConvolutionKernelF(array2D [[ 1.0f; 0.0f; -1.0f ]
51 [ 2.0f; 0.0f; -2.0f ]
52 [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
53
54 use xEdges = filteredGreenWhitoutStainFloat.Convolution(sobelKernel).Convert<Gray, float>()
55 use yEdges = filteredGreenWhitoutStainFloat.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
56
57 let xEdgesData = xEdges.Data
58 let yEdgesData = yEdges.Data
59 for r in 0..xEdges.Rows-1 do
60 xEdgesData.[r, 0, 0] <- 0.0
61 xEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
62 yEdgesData.[r, 0, 0] <- 0.0
63 yEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
64
65 for c in 0..xEdges.Cols-1 do
66 xEdgesData.[0, c, 0] <- 0.0
67 xEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
68 yEdgesData.[0, c, 0] <- 0.0
69 yEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
70
71 use magnitudes = new Matrix<float>(xEdges.Size)
72 CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
73
74 let min = ref 0.0
75 let minLocation = ref <| Point()
76 let max = ref 0.0
77 let maxLocation = ref <| Point()
78 magnitudes.MinMax(min, max, minLocation, maxLocation)
79
80 use magnitudesByte = ((magnitudes / !max) * 255.0).Convert<byte>() // Otsu from OpenCV only support 'byte'.
81 use edges = new Matrix<byte>(xEdges.Size)
82 CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore
83
84 logTime "Finding edges" (fun() -> thin edges)
85 logTime "Removing small connected components from thinning" (fun () -> removeArea edges 12)
86
87 let allEllipses, ellipses = logTime "Finding ellipses" (fun () ->
88 let matchingEllipses = Ellipse.find edges xEdges yEdges config
89 matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses )
90
91 let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config)
92
93 // Output pictures if debug flag is set.
94 match config.Debug with
95 | DebugOn output ->
96 let dirPath = System.IO.Path.Combine(output, name)
97 System.IO.Directory.CreateDirectory dirPath |> ignore
98
99 let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
100
101 saveMat (edges * 255.0) (buildFileName " - edges.png")
102
103 saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
104 saveImg parasites.stain (buildFileName " - parasites - stain.png")
105 saveImg parasites.infection (buildFileName " - parasites - infection.png")
106
107 let imgAllEllipses = img.Copy()
108 drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.1
109 saveImg imgAllEllipses (buildFileName " - ellipses - all.png")
110
111 let imgEllipses = img.Copy()
112 drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0
113 saveImg imgEllipses (buildFileName " - ellipses.png")
114
115 // saveImg (kmediansResults.fg * 255.0) (buildFileName " - foreground.png")
116
117 let imgCells = img.Copy()
118 drawCells imgCells false cells
119 saveImg imgCells (buildFileName " - cells.png")
120
121 let imgCells' = img.Copy()
122 drawCells imgCells' true cells
123 saveImg imgCells' (buildFileName " - cells - full.png")
124
125 let filteredGreenMaxima = gaussianFilter green config.Parameters.preFilterSigma
126 for m in ImgTools.findMaxima filteredGreenMaxima do
127 ImgTools.drawPoints filteredGreenMaxima m 255uy
128 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
129
130 saveImg filteredGreen (buildFileName " - filtered.png")
131 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
132 saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
133
134 (*saveImg parasitesMarker (buildFileName " - parasites (area closing).png")
135 saveImg stainMarker (buildFileName " - stain (area closing).png")*)
136
137 saveImg green (buildFileName " - green.png")
138
139 use blue = scaledImg.Item(0)
140 saveImg blue (buildFileName " - blue.png")
141
142 use red = scaledImg.Item(2)
143 saveImg red (buildFileName " - red.png")
144 | _ -> ()
145
146 cells