Doc + cleaning.
authorGreg Burri <greg.burri@gmail.com>
Sat, 30 Jan 2016 22:03:10 +0000 (23:03 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sat, 30 Jan 2016 22:03:10 +0000 (23:03 +0100)
Parasitemia/ParasitemiaCore/Analysis.fs
Parasitemia/ParasitemiaCore/Ellipse.fs
Parasitemia/ParasitemiaCore/ImgTools/Edges.fs
Parasitemia/ParasitemiaCore/ImgTools/ImgTools.fs
Parasitemia/ParasitemiaUI/Analysis.fs
Parasitemia/ParasitemiaUI/Program.fs

index c894fc4..e83977a 100644 (file)
@@ -46,6 +46,7 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportPr
     let logWithName mess = Log.User(buildLogWithName mess)
     let inline logTimeWithName (text: string) (f: unit -> 'a option) : 'a option = Log.LogWithTime((buildLogWithName text), Severity.USER, f)
 
+    // Monadic construction to be able to abort the progress when running.
     maybe {
         do! report 0
 
@@ -53,10 +54,10 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportPr
 
         use img_float = img.Convert<Bgr, float32>()
 
-        use img_RBC = img_float.[1] // mergeChannelsWithProjection img_float config.Parameters.averageColor_RBC config.Parameters.averageColor_BG 255.
+        use img_RBC = img_float.[1] // Green.
         use img_RBC_filtered = gaussianFilter img_RBC config.LPFStandardDeviationRBC
 
-        use img_parasites = img_float.[2] // mergeChannelsWithProjection img_float config.Parameters.averageColor_Parasite config.Parameters.averageColor_RBC 255.
+        use img_parasites = img_float.[2] // Red.
         use img_parasites_filtered = gaussianFilter img_parasites config.LPFStandardDeviationParasite
 
         logWithName (sprintf "Nominal erythrocyte diameter: %A" config.RBCRadiusByResolution)
@@ -137,10 +138,10 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportPr
                 Drawing.drawCells imgCells' true cells
                 IO.saveImg imgCells' (buildFileName " - cells - full.png")
 
-                let filteredGreenMaxima = gaussianFilter img_RBC config.LPFStandardDeviationRBC
-                for m in findMaxima filteredGreenMaxima do
-                    Drawing.drawPoints filteredGreenMaxima m 255.f
-                IO.saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png")
+                let filteredRBCMaxima = gaussianFilter img_RBC config.LPFStandardDeviationRBC
+                for m in findMaxima filteredRBCMaxima do
+                    Drawing.drawPoints filteredRBCMaxima m 255.f
+                IO.saveImg filteredRBCMaxima (buildFileName " - filtered - maxima.png")
 
                 IO.saveImg imgWhitoutParasite (buildFileName " - filtered closed stain.png")
                 IO.saveImg imgWithoutNucleus (buildFileName " - filtered closed infection.png")
@@ -182,9 +183,14 @@ let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (report
         imgs
         |> PSeq.choose (
             fun (id, config, img) ->
-                match doAnalysis img id config (Some (fun p -> reportProgressImg id p)) with
-                | Some result -> Some (id, result)
-                | None -> None)
+                try
+                    match doAnalysis img id config (Some (fun p -> reportProgressImg id p)) with
+                    | Some result -> Some (id, result)
+                    | None -> None
+                with
+                | ex ->
+                    Log.Error("Analysis {0} failed: {1}", id, ex)
+                    None)
         |> PSeq.withDegreeOfParallelism n
         |> PSeq.toList
 
index cf94be2..e95cf27 100644 (file)
@@ -181,7 +181,7 @@ let find (edges: Matrix<byte>)
                     if edgesData.[i, j] = 1uy
                     then currentElements.Add(Point(j, i))
 
-            if currentElements.Count >= 10
+            if currentElements.Count >= nbPickElementsMin
             then
                 let mutable nbOfPicks = (float currentElements.Count) * factorNbMaxPick |> int
                 let mutable nbOfValidPicks = (float currentElements.Count) * factorNbValidPick |> int
index a1b6033..3980120 100644 (file)
@@ -87,9 +87,6 @@ let find (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<f
 
     // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection).
 
-    IO.saveMat magnitudes "magnitudes.png"
-    IO.saveMat nms "nms.png"
-
     let edges = new Matrix<byte>(xGradient.Size)
     let edgesData = edges.Data
 
index 0510bc8..0949713 100644 (file)
@@ -6,6 +6,12 @@ open System.Drawing
 open Emgu.CV
 open Emgu.CV.Structure
 
+/// <summary>
+/// Normalize an image between 0 and 'upperLimit'.
+/// FIXME: use to many memory.
+/// </summary>
+/// <param name="img"></param>
+/// <param name="upperLimit"></param>
 let normalize (img: Image<Gray, float32>) (upperLimit: float) : Image<Gray, float32> =
     let min = ref [| 0.0 |]
     let minLocation = ref <| [| Point() |]
index 5bb3500..2ff0b1e 100644 (file)
@@ -128,17 +128,18 @@ let showWindow (parent: Window) (state: State.State) : bool =
                             | Some results ->
                                 for id, cells in results do
                                     state.SetResult (int id) cells
-
-                                win.Root.Dispatcher.Invoke(fun () ->
-                                    win.stackSourceImagesSelection.IsEnabled <- true
-                                    win.butStart.IsEnabled <- true
-                                    win.butClose.Content <- "Close"
-                                    updateSourceImages ())
-
                                 Logger.Log.User("All analyses terminated successfully")
                                 atLeastOneAnalysisPerformed <- true
                                 analysisPerformed <- true
-                            | None -> ())
+                            | None ->
+                                Logger.Log.User("Analysis aborted")
+
+                            win.Root.Dispatcher.Invoke(fun () ->
+                                win.progress.Value <- if maybeResults.IsSome then 100. else 0.
+                                win.stackSourceImagesSelection.IsEnabled <- true
+                                win.butStart.IsEnabled <- true
+                                win.butClose.Content <- "Close"
+                                updateSourceImages ()))
                 } |> Async.Start
         | _ -> ())
 
@@ -151,5 +152,6 @@ let showWindow (parent: Window) (state: State.State) : bool =
     lock monitor (fun () ->
         if not analysisPerformed
             then
+                // To cancel the current analysis if one is running on the next call to the progress function.
                 analysisCancelled <- true
         atLeastOneAnalysisPerformed)
index feab9ec..ce7cdd4 100644 (file)
@@ -96,6 +96,6 @@ let main args =
         result
 
     with
-    | _ as ex ->
+    | ex ->
         Log.Fatal("Error: {0}", ex)
         1
\ No newline at end of file