6 open FSharp.Collections.ParallelSeq
17 let doAnalysis (img
: Image<Bgr, byte
>) (name
: string) (config
: Config) : Cell list =
18 use green = img
.Item(1)
19 let greenFloat = green.Convert<Gray, float32
>()
20 let filteredGreen = gaussianFilter
greenFloat (float config.Parameters.preFilterSigma
)
22 logTime
"areaOpen 1" (fun () -> ImgTools.areaOpenF
filteredGreen config.Parameters.initialAreaOpen
)
24 let r1 = logTime
"Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing
(filteredGreen.Convert<Gray, byte
>()) (10, 80) 0.5 |> float32
)
25 // let r2 = logTime "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen (10, 80) |> float32)
26 // log (sprintf "r1: %A, r2: %A" r1 r2)
27 config.RBCRadius <- r1
29 let secondAreaOpen = int <| config.RBCArea / 3.f
31 if secondAreaOpen > config.Parameters.initialAreaOpen
33 logTime
"areaOpen 2" (fun () -> ImgTools.areaOpenF
filteredGreen secondAreaOpen)
35 let parasites, filteredGreenWhitoutStain
= ParasitesMarker.find
filteredGreen config
36 //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
38 let edges, xGradient
, yGradient
= logTime
"Finding edges" (fun () -> ImgTools.findEdges
filteredGreenWhitoutStain)
39 logTime
"Removing small connected components from thinning" (fun () -> removeArea
edges (config.RBCRadius ** 2.f
/ 50.f
|> int))
41 let allEllipses, ellipses
= logTime
"Finding ellipses" (fun () ->
42 let matchingEllipses = Ellipse.find
edges xGradient yGradient
config
43 matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses)
45 let cells = logTime
"Classifier" (fun () -> Classifier.findCells
ellipses parasites filteredGreenWhitoutStain config)
47 // Output pictures if debug flag is set.
48 match config.Debug with
50 let dirPath = System.IO.Path.Combine(output
, name
)
51 System.IO.Directory.CreateDirectory dirPath |> ignore
53 let buildFileName postfix
= System.IO.Path.Combine(dirPath, name
+ postfix
)
55 saveMat
(edges * 255.0) (buildFileName " - edges.png")
57 saveImg
parasites.darkStain
(buildFileName " - parasites - dark stain.png")
58 saveImg
parasites.stain
(buildFileName " - parasites - stain.png")
59 saveImg
parasites.infection
(buildFileName " - parasites - infection.png")
61 let imgAllEllipses = img
.Copy()
62 drawEllipses
imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05
63 saveImg
imgAllEllipses (buildFileName " - ellipses - all.png")
65 let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte
>()
66 drawEllipses
imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0
67 saveImg
imgEllipses (buildFileName " - ellipses.png")
69 let imgCells = img
.Copy()
70 drawCells
imgCells false cells
71 saveImg
imgCells (buildFileName " - cells.png")
73 let imgCells' = img.Copy()
74 drawCells imgCells' true cells
75 saveImg
imgCells' (buildFileName " - cells - full.png")
77 let filteredGreenMaxima = gaussianFilter greenFloat config.Parameters.preFilterSigma
78 for m in ImgTools.findMaxima filteredGreenMaxima do
79 ImgTools.drawPoints filteredGreenMaxima m 255.f
80 saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
82 saveImg filteredGreen (buildFileName " - filtered.png")
83 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
84 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
86 saveImg green (buildFileName " - green.png")
88 use blue = img.Item(0)
89 saveImg blue (buildFileName " - blue.png")
92 saveImg red (buildFileName " - red.png")
98 let doMultipleAnalysis (imgs: (string * Image<Bgr, byte>) list) (config : Config) : (string * Cell list) list =
99 let nbConcurrentTaskLimit = 4
100 let n = Environment.ProcessorCount
103 |> PSeq.map (fun (id, img) -> id, doAnalysis img id (config.Copy()))
104 |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)