Use two radius in the configuration, one computed with the image resolution and one...
[master-thesis.git] / Parasitemia / Parasitemia / MainAnalysis.fs
index 3f35fb7..8f59514 100644 (file)
@@ -1,8 +1,11 @@
 module ImageAnalysis
 
 open System
+open System.Linq
 open System.Drawing
 
+open FSharp.Collections.ParallelSeq
+
 open Emgu.CV
 open Emgu.CV.Structure
 
@@ -11,68 +14,74 @@ open ImgTools
 open Config
 open Types
 
+let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportProgress: (int -> unit) option) : Cell list =
+    // To report the progress of this function from 0 to 100.
+    let inline report (percent: int) =
+        match reportProgress with
+        | Some f -> f percent
+        | _ -> ()
+
+    let inline buildLogWithName (text: string) = sprintf "(%s) %s" name text
+    let logWithName = buildLogWithName >> log
+    let inline logTimeWithName (text: string) (f: unit -> 'a) : 'a = logTime (buildLogWithName text) f
 
-let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) : Cell list =
+    logWithName "Starting analysis ..."
 
-    let imgFloat = img.Convert<Bgr, float32>()
-    use scaledImg = if config.scale = 1.0 then imgFloat else imgFloat.Resize(config.scale, CvEnum.Inter.Area)
+    use green = img.Item(1)
+    let greenFloat = green.Convert<Gray, float32>()
+    let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation
 
-    use green = scaledImg.Item(1)
+    logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
 
-    use filteredGreen = (gaussianFilter green config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter green config.doGSigma2)
+    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.
+    logTimeWithName "Area opening number one" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening)
 
-    use sobelKernel =
-        new ConvolutionKernelF(array2D [[ 1.0f; 0.0f; -1.0f ]
-                                        [ 2.0f; 0.0f; -2.0f ]
-                                        [ 1.0f; 0.0f; -1.0f ]], Point(0, 0))
+    report 8
 
-    use xEdges = filteredGreen.Convolution(sobelKernel).Convert<Gray, float>()
-    use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert<Gray, float>()
+    let range =
+        let delta = config.Parameters.granulometryRange * config.RBCRadiusByResolution.Pixel
+        int <| config.RBCRadiusByResolution.Pixel - delta, int <| config.RBCRadiusByResolution.Pixel + delta
+    //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert<Gray, byte>()) range 1.0 |> float32)
+    config.SetRBCRadius <| logTimeWithName "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32)
+    // log (sprintf "r1: %A, r2: %A" r1 r2)
 
-    let xEdgesData = xEdges.Data
-    let yEdgesData = yEdges.Data
-    for r in 0..xEdges.Rows-1 do
-        xEdgesData.[r, 0, 0] <- 0.0
-        xEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
-        yEdgesData.[r, 0, 0] <- 0.0
-        yEdgesData.[r, xEdges.Cols-1, 0] <- 0.0
+    logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius)
 
-    for c in 0..xEdges.Cols-1 do
-        xEdgesData.[0, c, 0] <- 0.0
-        xEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
-        yEdgesData.[0, c, 0] <- 0.0
-        yEdgesData.[xEdges.Rows-1, c, 0] <- 0.0
+    report 24
 
-    use magnitudes = new Matrix<float>(xEdges.Size)
-    CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles).
+    let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
+    if secondAreaOpening > initialAreaOpening
+    then
+        logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening)
 
-    let min = ref 0.0
-    let minLocation = ref <| Point()
-    let max = ref 0.0
-    let maxLocation = ref <| Point()
-    magnitudes.MinMax(min, max, minLocation, maxLocation)
+    let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config
+    //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
 
-    use magnitudesByte = ((magnitudes / !max) * 255.0).Convert<byte>() // Otsu from OpenCV only support 'byte'.
-    use edges = new Matrix<byte>(xEdges.Size)
-    CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore
+    let edges, xGradient, yGradient = logTimeWithName "Finding edges" (fun () ->
+        let edges, xGradient, yGradient = ImgTools.findEdges filteredGreenWhitoutStain
+        removeArea edges (config.RBCRadius.Pixel ** 2.f / 50.f |> int)
+        edges, xGradient, yGradient)
 
-    logTime "Finding edges" (fun() -> thin edges)
-    logTime "Removing small connected components from thinning" (fun () -> removeArea edges 12)
+    let allEllipses, ellipses = logTimeWithName "Finding ellipses" (fun () ->
+        let matchingEllipses = Ellipse.find edges xGradient yGradient config
+        matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses)
 
-    let kmediansResults = logTime "Finding foreground (k-medians)" (fun () -> KMedians.kmedians filteredGreen 1.0)
+    report 80
 
-    let parasites = ParasitesMarker.find green filteredGreen kmediansResults config
+    let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config)
 
-    let allEllipses, ellipses = logTime "Finding ellipses" (fun () ->
-        let matchingEllipses = Ellipse.find edges xEdges yEdges config
-        matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses )
+    report 100
 
-    let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites kmediansResults.fg config)
+    logWithName "Analysis finished"
 
     // Output pictures if debug flag is set.
-    match config.debug with
+    match config.Debug with
     | DebugOn output ->
-        let buildFileName postfix = System.IO.Path.Combine(output, name + postfix)
+        let dirPath = System.IO.Path.Combine(output, name)
+        System.IO.Directory.CreateDirectory dirPath |> ignore
+
+        let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
+
         saveMat (edges * 255.0) (buildFileName " - edges.png")
 
         saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
@@ -80,15 +89,13 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) : Cell li
         saveImg parasites.infection (buildFileName " - parasites - infection.png")
 
         let imgAllEllipses = img.Copy()
-        drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.1
+        drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05
         saveImg imgAllEllipses (buildFileName " - ellipses - all.png")
 
-        let imgEllipses = img.Copy()
+        let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte>()
         drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0
         saveImg imgEllipses (buildFileName " - ellipses.png")
 
-        saveImg (kmediansResults.fg * 255.0) (buildFileName " - foreground.png")
-
         let imgCells = img.Copy()
         drawCells imgCells false cells
         saveImg imgCells (buildFileName " - cells.png")
@@ -96,6 +103,45 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) : Cell li
         let imgCells' = img.Copy()
         drawCells imgCells' true cells
         saveImg imgCells' (buildFileName " - cells - full.png")
+
+        let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation
+        for m in ImgTools.findMaxima filteredGreenMaxima do
+            ImgTools.drawPoints filteredGreenMaxima m 255.f
+        saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
+
+        saveImg filteredGreen (buildFileName " - filtered.png")
+        saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
+        //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
+
+        saveImg green (buildFileName " - green.png")
+
+        use blue = img.Item(0)
+        saveImg blue (buildFileName " - blue.png")
+
+        use red = img.Item(2)
+        saveImg red (buildFileName " - red.png")
     | _ -> ()
 
     cells
+
+// ID * cell list.
+let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (reportProgress: (int -> unit) option) : (string * Cell list) list =
+    let inline report (percent: int) =
+        match reportProgress with
+        | Some f -> f percent
+        | _ -> ()
+
+    let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
+    let nbImgs = List.length imgs
+
+    let reportProgressImg (id: string) (progress: int) =
+        progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore
+        report (progressPerAnalysis.Values.Sum() / nbImgs)
+
+    let nbConcurrentTaskLimit = 4 // To reduce the memory taken.
+    let n = Environment.ProcessorCount
+
+    imgs
+    |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p)))
+    |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)
+    |> PSeq.toList