7 open FSharp.Collections.ParallelSeq
10 open Emgu.CV.Structure
17 let doAnalysis (img
: Image<Bgr, byte
>) (name
: string) (config
: Config) (reportProgress
: (int -> unit) option) : Cell list =
18 // To report the progress of this function from 0 to 100.
19 let inline report
(percent
: int) =
20 match reportProgress
with
24 let inline buildLogWithName
(text
: string) = sprintf
"(%s) %s" name
text
25 let logWithName = buildLogWithName
>> log
26 let inline logTimeWithName
(text: string) (f
: unit -> 'a) : 'a
= logTime
(buildLogWithName
text) f
28 logWithName "Starting analysis ..."
30 use green = img
.Item(1)
31 let greenFloat = green.Convert<Gray, float32
>()
32 let filteredGreen = gaussianFilter
greenFloat config.LPFStandardDeviation
34 logWithName (sprintf
"Nominal erytrocyte diameter: %s" config.FormattedRadius)
36 let initialAreaOpening = int<| config.RBCArea * 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.
37 logTimeWithName
"Area opening number one" (fun () -> ImgTools.areaOpenF
filteredGreen initialAreaOpening)
42 let delta = config.Parameters.granulometryRange
* config.RBCRadius
43 int <| config.RBCRadius - delta, int <| config.RBCRadius + delta
44 //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert<Gray, byte>()) range 1.0 |> float32)
45 let r2 = logTimeWithName
"Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing
filteredGreen range |> float32
)
46 // log (sprintf "r1: %A, r2: %A" r1 r2)
47 config.RBCRadius <- r2
49 logWithName (sprintf
"Found erytrocyte diameter: %s" config.FormattedRadius)
53 let secondAreaOpening = int <| config.RBCArea * 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 ** 2.f
/ 50.f
|> int)
64 edges, xGradient
, yGradient
)
66 let allEllipses, ellipses
= logTimeWithName
"Finding ellipses" (fun () ->
67 let matchingEllipses = Ellipse.find
edges xGradient yGradient
config
68 matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses)
72 let cells = logTimeWithName
"Classifier" (fun () -> Classifier.findCells
ellipses parasites filteredGreenWhitoutStain config)
76 logWithName "Analysis finished"
78 // Output pictures if debug flag is set.
79 match config.Debug with
81 let dirPath = System.IO.Path.Combine(output
, name
)
82 System.IO.Directory.CreateDirectory dirPath |> ignore
84 let buildFileName postfix
= System.IO.Path.Combine(dirPath, name
+ postfix
)
86 saveMat
(edges * 255.0) (buildFileName " - edges.png")
88 saveImg
parasites.darkStain
(buildFileName " - parasites - dark stain.png")
89 saveImg
parasites.stain
(buildFileName " - parasites - stain.png")
90 saveImg
parasites.infection
(buildFileName " - parasites - infection.png")
92 let imgAllEllipses = img
.Copy()
93 drawEllipses
imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05
94 saveImg
imgAllEllipses (buildFileName " - ellipses - all.png")
96 let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte
>()
97 drawEllipses
imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0
98 saveImg
imgEllipses (buildFileName " - ellipses.png")
100 let imgCells = img
.Copy()
101 drawCells
imgCells false cells
102 saveImg
imgCells (buildFileName " - cells.png")
104 let imgCells' = img.Copy()
105 drawCells imgCells' true cells
106 saveImg
imgCells' (buildFileName " - cells - full.png")
108 let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation
109 for m in ImgTools.findMaxima filteredGreenMaxima do
110 ImgTools.drawPoints filteredGreenMaxima m 255.f
111 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
113 saveImg filteredGreen (buildFileName " - filtered.png")
114 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
115 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
117 saveImg green (buildFileName " - green.png")
119 use blue = img.Item(0)
120 saveImg blue (buildFileName " - blue.png")
122 use red = img.Item(2)
123 saveImg red (buildFileName " - red.png")
129 let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (reportProgress: (int -> unit) option) : (string * Cell list) list =
130 let inline report (percent: int) =
131 match reportProgress with
132 | Some f -> f percent
135 let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
136 let nbImgs = List.length imgs
138 let reportProgressImg (id: string) (progress: int) =
139 progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore
140 report (progressPerAnalysis.Values.Sum() / nbImgs)
142 let nbConcurrentTaskLimit = 4
143 let n = Environment.ProcessorCount
146 |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p)))
147 |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)