1
module ParasitemiaCore.Analysis
7 open FSharp.Collections.ParallelSeq
10 open Emgu.CV.Structure
19 let doAnalysis (img
: Image<Bgr, byte
>) (name
: string) (config
: Config) (reportProgress
: (int -> bool) option) : Cell list option =
20 // To report the progress of this function from 0 to 100.
21 let reportWithVal (percent
: int) (value
: 'a) : 'a
option =
22 match reportProgress
with
29 let report (percent
: int) : unit option =
30 reportWithVal percent ()
32 let inline buildLogWithName
(text
: string) = sprintf
"(%s) %s" name
text
33 let logWithName mess
= Log.User(buildLogWithName
mess)
34 let inline logTimeWithName
(text: string) (f
: unit -> 'a option) : 'a
option = Log.LogWithTime((buildLogWithName
text), Severity.USER, f
)
39 logWithName "Starting analysis ..."
41 use green = img
.Item(1)
42 let greenFloat = green.Convert<Gray, float32
>()
43 let filteredGreen = gaussianFilter
greenFloat config.LPFStandardDeviation
45 logWithName (sprintf
"Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
47 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.
48 do! logTimeWithName
"First area opening" (fun () -> ImgTools.areaOpenF
filteredGreen initialAreaOpening; report 10)
53 let delta = config.Parameters.granulometryRange
* config.RBCRadiusByResolution.Pixel
54 int <| config.RBCRadiusByResolution.Pixel - delta, int <| config.RBCRadiusByResolution.Pixel + delta
55 //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert<Gray, byte>()) range 1.0 |> float32)
56 let! radius = logTimeWithName
"Granulometry (area)" (fun() -> reportWithVal 10 (Granulometry.findRadiusByAreaClosing
filteredGreen range |> float32
))
57 config.SetRBCRadius <| radius
59 logWithName (sprintf
"Found erytrocyte diameter: %A" config.RBCRadius)
64 let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
65 if secondAreaOpening > initialAreaOpening
67 logTimeWithName
"Second area opening" (fun () -> ImgTools.areaOpenF
filteredGreen secondAreaOpening; report 30)
71 let parasites, filteredGreenWhitoutStain
= ParasitesMarker.find
filteredGreen config
72 //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
74 let! edges
, xGradient
, yGradient
= logTimeWithName
"Finding edges" (fun () ->
75 let edges, xGradient
, yGradient
= ImgTools.findEdges
filteredGreenWhitoutStain
76 removeArea
edges (config.RBCRadius.Pixel ** 2.f
/ 50.f
|> int)
77 reportWithVal 40 (edges, xGradient
, yGradient
))
79 let! matchingEllipses
= logTimeWithName
"Finding ellipses" (fun () -> reportWithVal 60 (Ellipse.find
edges xGradient yGradient
config))
81 let! prunedEllipses
= logTimeWithName
"Ellipses pruning" (fun () -> reportWithVal 80 (matchingEllipses
.PrunedEllipses))
83 let! cells
= logTimeWithName
"Classifier" (fun () -> reportWithVal 100 (Classifier.findCells
prunedEllipses parasites filteredGreenWhitoutStain config))
85 logWithName "Analysis finished"
88 // Output pictures if debug flag is set.
89 match config.Debug with
91 let dirPath = System.IO.Path.Combine(output
, name
)
92 System.IO.Directory.CreateDirectory dirPath |> ignore
94 let buildFileName postfix
= System.IO.Path.Combine(dirPath, name
+ postfix
)
96 saveMat
(edges * 255.0) (buildFileName " - edges.png")
98 saveImg
parasites.darkStain
(buildFileName " - parasites - dark stain.png")
99 saveImg
parasites.stain
(buildFileName " - parasites - stain.png")
100 saveImg
parasites.infection
(buildFileName " - parasites - infection.png")
102 let imgAllEllipses = img
.Copy()
103 drawEllipses
imgAllEllipses matchingEllipses
.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04
104 saveImg
imgAllEllipses (buildFileName " - ellipses - all.png")
106 let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte
>()
107 drawEllipses
imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0
108 saveImg
imgEllipses (buildFileName " - ellipses.png")
110 let imgCells = img
.Copy()
111 drawCells
imgCells false cells
112 saveImg
imgCells (buildFileName " - cells.png")
114 let imgCells' = img.Copy()
115 drawCells imgCells' true cells
116 saveImg
imgCells' (buildFileName " - cells - full.png")
118 let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation
119 for m in ImgTools.findMaxima filteredGreenMaxima do
120 ImgTools.drawPoints filteredGreenMaxima m 255.f
121 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
123 saveImg filteredGreen (buildFileName " - filtered.png")
124 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
125 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
127 saveImg green (buildFileName " - green.png")
129 use blue = img.Item(0)
130 saveImg blue (buildFileName " - blue.png")
132 use red = img.Item(2)
133 saveImg red (buildFileName " - red.png")
139 /// Do multiple analyses on the same time. The number of concurrent process depends if the number of the core.
141 /// <param name="imgs"></param>
142 /// <param name="reportProgress">An optional function to report progress. The process is aborted if the returned value is false</param>
143 let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (reportProgress: (int -> bool) option) : (string * Cell list) list option =
144 let report (percent: int) : bool =
145 match reportProgress with
146 | Some f -> f percent
149 let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
150 let nbImgs = List.length imgs
152 let reportProgressImg (id: string) (progress: int) =
153 progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore
154 report (progressPerAnalysis.Values.Sum() / nbImgs)
156 let n = Environment.ProcessorCount
161 fun (id, config, img) ->
162 match doAnalysis img id config (Some (fun p -> reportProgressImg id p)) with
163 | Some result -> Some (id, result)
165 |> PSeq.withDegreeOfParallelism n
168 // If one of the analyses has been aborted we return 'None'.
169 if List.length results <> List.length imgs