* Add the possibility to set an RBC as healthy or infected
[master-thesis.git] / Parasitemia / Parasitemia / MainAnalysis.fs
1 module ImageAnalysis
2
3 open System
4 open System.Drawing
5
6 open FSharp.Collections.ParallelSeq
7
8 open Emgu.CV
9 open Emgu.CV.Structure
10
11 open Utils
12 open ImgTools
13 open Config
14 open Types
15
16
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)
21
22 logTime "areaOpen 1" (fun () -> ImgTools.areaOpenF filteredGreen config.Parameters.initialAreaOpen)
23
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
28
29 let secondAreaOpen = int <| config.RBCArea / 3.f
30
31 if secondAreaOpen > config.Parameters.initialAreaOpen
32 then
33 logTime "areaOpen 2" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpen)
34
35 let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config
36 //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
37
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))
40
41 let allEllipses, ellipses = logTime "Finding ellipses" (fun () ->
42 let matchingEllipses = Ellipse.find edges xGradient yGradient config
43 matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses)
44
45 let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config)
46
47 // Output pictures if debug flag is set.
48 match config.Debug with
49 | DebugOn output ->
50 let dirPath = System.IO.Path.Combine(output, name)
51 System.IO.Directory.CreateDirectory dirPath |> ignore
52
53 let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
54
55 saveMat (edges * 255.0) (buildFileName " - edges.png")
56
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")
60
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")
64
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")
68
69 let imgCells = img.Copy()
70 drawCells imgCells false cells
71 saveImg imgCells (buildFileName " - cells.png")
72
73 let imgCells' = img.Copy()
74 drawCells imgCells' true cells
75 saveImg imgCells' (buildFileName " - cells - full.png")
76
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")
81
82 saveImg filteredGreen (buildFileName " - filtered.png")
83 saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
84 //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
85
86 saveImg green (buildFileName " - green.png")
87
88 use blue = img.Item(0)
89 saveImg blue (buildFileName " - blue.png")
90
91 use red = img.Item(2)
92 saveImg red (buildFileName " - red.png")
93 | _ -> ()
94
95 cells
96
97
98 let doMultipleAnalysis (imgs: (string * Image<Bgr, byte>) list) (config : Config) : (string * Cell list) list =
99 let nbConcurrentTaskLimit = 4
100 let n = Environment.ProcessorCount
101
102 imgs
103 |> PSeq.map (fun (id, img) -> id, doAnalysis img id (config.Copy()))
104 |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)
105 |> PSeq.toList