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 -> 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
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
)
30 logWithName "Starting analysis ..."
32 use green = img
.Item(1)
33 let greenFloat = green.Convert<Gray, float32
>()
34 let filteredGreen = gaussianFilter
greenFloat config.LPFStandardDeviation
36 logWithName (sprintf
"Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
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)
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
)
49 logWithName (sprintf
"Found erytrocyte diameter: %A" config.RBCRadius)
53 let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
54 if secondAreaOpening > initialAreaOpening
56 logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF
filteredGreen secondAreaOpening)
58 let parasites, filteredGreenWhitoutStain
= ParasitesMarker.find
filteredGreen config
59 //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
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
)
66 let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find
edges xGradient yGradient
config)
70 let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses)
74 let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells
prunedEllipses parasites filteredGreenWhitoutStain config)
78 logWithName "Analysis finished"
80 // Output pictures if debug flag is set.
81 match config.Debug with
83 let dirPath = System.IO.Path.Combine(output
, name
)
84 System.IO.Directory.CreateDirectory dirPath |> ignore
86 let buildFileName postfix
= System.IO.Path.Combine(dirPath, name
+ postfix
)
88 saveMat
(edges * 255.0) (buildFileName " - edges.png")
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")
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")
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")
102 let imgCells = img
.Copy()
103 drawCells
imgCells false cells
104 saveImg
imgCells (buildFileName " - cells.png")
106 let imgCells' = img.Copy()
107 drawCells imgCells' true cells
108 saveImg
imgCells' (buildFileName " - cells - full.png")
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")
115 saveImg filteredGreen (buildFileName " - filtered.png")
116 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
117 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
119 saveImg green (buildFileName " - green.png")
121 use blue = img.Item(0)
122 saveImg blue (buildFileName " - blue.png")
124 use red = img.Item(2)
125 saveImg red (buildFileName " - red.png")
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
137 let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
138 let nbImgs = List.length imgs
140 let reportProgressImg (id: string) (progress: int) =
141 progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore
142 report (progressPerAnalysis.Values.Sum() / nbImgs)
144 let n = Environment.ProcessorCount
147 |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p)))
148 |> PSeq.withDegreeOfParallelism n