0faf54a9d61711e29442d7cb0d14e402090312c7
[master-thesis.git] / Parasitemia / ParasitemiaCore / MainAnalysis.fs
1 module ParasitemiaCore.Analysis
2
3 open System
4 open System.Linq
5 open System.Drawing
6
7 open FSharp.Collections.ParallelSeq
8
9 open Emgu.CV
10 open Emgu.CV.Structure
11
12 open Logger
13
14 open Utils
15 open ImgTools
16 open Config
17 open Types
18
19 let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportProgress: (int -> unit) option) : Cell list =
20 // To report the progress of this function from 0 to 100.
21 let inline report (percent: int) =
22 match reportProgress with
23 | Some f -> f percent
24 | _ -> ()
25
26 let inline buildLogWithName (text: string) = sprintf "(%s) %s" name text
27 let logWithName mess = Log.User(buildLogWithName mess)
28 let inline logTimeWithName (text: string) (f: unit -> 'a) : 'a = Log.LogWithTime((buildLogWithName text), Severity.USER, f)
29
30 logWithName "Starting analysis ..."
31
32 use green = img.Item(1)
33 let greenFloat = green.Convert<Gray, float32>()
34 let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation
35
36 logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
37
38 let initialAreaOpening = int <| config.RBCRadiusByResolution.Area * config.Parameters.ratioAreaPaleCenter * 1.2f // We do an area opening a little larger to avoid to do a second one in the case the radius found is near the initial one.
39 logTimeWithName "Area opening number one" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening)
40
41 report 10
42
43 let range =
44 let delta = config.Parameters.granulometryRange * config.RBCRadiusByResolution.Pixel
45 int <| config.RBCRadiusByResolution.Pixel - delta, int <| config.RBCRadiusByResolution.Pixel + delta
46 //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert<Gray, byte>()) range 1.0 |> float32)
47 config.SetRBCRadius <| logTimeWithName "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32)
48
49 logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius)
50
51 report 20
52
53 let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
54 if secondAreaOpening > initialAreaOpening
55 then
56 logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening)
57
58 let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config
59 //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
60
61 let edges, xGradient, yGradient = logTimeWithName "Finding edges" (fun () ->
62 let edges, xGradient, yGradient = ImgTools.findEdges filteredGreenWhitoutStain
63 removeArea edges (config.RBCRadius.Pixel ** 2.f / 50.f |> int)
64 edges, xGradient, yGradient)
65
66 let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find edges xGradient yGradient config)
67
68 report 60
69
70 let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses)
71
72 report 80
73
74 let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config)
75
76 report 100
77
78 logWithName "Analysis finished"
79
80 // Output pictures if debug flag is set.
81 match config.Debug with
82 | DebugOn output ->
83 let dirPath = System.IO.Path.Combine(output, name)
84 System.IO.Directory.CreateDirectory dirPath |> ignore
85
86 let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
87
88 saveMat (edges * 255.0) (buildFileName " - edges.png")
89
90 saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
91 saveImg parasites.stain (buildFileName " - parasites - stain.png")
92 saveImg parasites.infection (buildFileName " - parasites - infection.png")
93
94 let imgAllEllipses = img.Copy()
95 drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04
96 saveImg imgAllEllipses (buildFileName " - ellipses - all.png")
97
98 let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte>()
99 drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0
100 saveImg imgEllipses (buildFileName " - ellipses.png")
101
102 let imgCells = img.Copy()
103 drawCells imgCells false cells
104 saveImg imgCells (buildFileName " - cells.png")
105
106 let imgCells' = img.Copy()
107 drawCells imgCells' true cells
108 saveImg imgCells' (buildFileName " - cells - full.png")
109
110 let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation
111 for m in ImgTools.findMaxima filteredGreenMaxima do
112 ImgTools.drawPoints filteredGreenMaxima m 255.f
113 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
114
115 saveImg filteredGreen (buildFileName " - filtered.png")
116 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
117 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
118
119 saveImg green (buildFileName " - green.png")
120
121 use blue = img.Item(0)
122 saveImg blue (buildFileName " - blue.png")
123
124 use red = img.Item(2)
125 saveImg red (buildFileName " - red.png")
126 | _ -> ()
127
128 cells
129
130 // ID * cell list.
131 let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (reportProgress: (int -> unit) option) : (string * Cell list) list =
132 let inline report (percent: int) =
133 match reportProgress with
134 | Some f -> f percent
135 | _ -> ()
136
137 let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
138 let nbImgs = List.length imgs
139
140 let reportProgressImg (id: string) (progress: int) =
141 progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore
142 report (progressPerAnalysis.Values.Sum() / nbImgs)
143
144 let n = Environment.ProcessorCount
145
146 imgs
147 |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p)))
148 |> PSeq.withDegreeOfParallelism n
149 |> PSeq.toList