open Config
open Types
-let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportProgress: (int -> unit) option) : Cell list =
+let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportProgress: (int -> bool) option) : Cell list option =
// To report the progress of this function from 0 to 100.
- let inline report (percent: int) =
+ let reportWithVal (percent: int) (value: 'a) : 'a option =
match reportProgress with
- | Some f -> f percent
- | _ -> ()
+ | Some f ->
+ if f percent
+ then Some value
+ else None
+ | _ -> Some value
+
+ let report (percent: int) : unit option =
+ reportWithVal percent ()
let inline buildLogWithName (text: string) = sprintf "(%s) %s" name text
let logWithName mess = Log.User(buildLogWithName mess)
- let inline logTimeWithName (text: string) (f: unit -> 'a) : 'a = Log.LogWithTime((buildLogWithName text), Severity.USER, f)
-
- logWithName "Starting analysis ..."
+ let inline logTimeWithName (text: string) (f: unit -> 'a option) : 'a option = Log.LogWithTime((buildLogWithName text), Severity.USER, f)
- use green = img.Item(1)
- let greenFloat = green.Convert<Gray, float32>()
- let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation
+ maybe {
+ do! report 0
- logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
+ logWithName "Starting analysis ..."
- 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 "First area opening" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening)
+ use green = img.Item(1)
+ let greenFloat = green.Convert<Gray, float32>()
+ let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation
- report 10
+ logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
- 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)
+ 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.
+ do! logTimeWithName "First area opening" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening; report 10)
- logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius)
+ //do! report 10
- report 20
+ 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)
+ let! radius = logTimeWithName "Granulometry (area)" (fun() -> reportWithVal 10 (Granulometry.findRadiusByAreaClosing filteredGreen range |> float32))
+ config.SetRBCRadius <| radius
- let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
- if secondAreaOpening > initialAreaOpening
- then
- logTimeWithName "Second area opening" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening)
+ logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius)
- let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config
- //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
+ do! report 20
- 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)
+ do!
+ let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
+ if secondAreaOpening > initialAreaOpening
+ then
+ logTimeWithName "Second area opening" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening; report 30)
+ else
+ report 30
- let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find edges xGradient yGradient config)
+ let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config
+ //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config
- report 60
+ 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)
+ reportWithVal 40 (edges, xGradient, yGradient))
- let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses)
+ let! matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> reportWithVal 60 (Ellipse.find edges xGradient yGradient config))
- report 80
+ let! prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> reportWithVal 80 (matchingEllipses.PrunedEllipses))
- let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config)
+ let! cells = logTimeWithName "Classifier" (fun () -> reportWithVal 100 (Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config))
- report 100
+ logWithName "Analysis finished"
- logWithName "Analysis finished"
+ do
+ // Output pictures if debug flag is set.
+ match config.Debug with
+ | DebugOn output ->
+ let dirPath = System.IO.Path.Combine(output, name)
+ System.IO.Directory.CreateDirectory dirPath |> ignore
- // Output pictures if debug flag is set.
- match config.Debug with
- | DebugOn output ->
- let dirPath = System.IO.Path.Combine(output, name)
- System.IO.Directory.CreateDirectory dirPath |> ignore
+ let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
- let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix)
+ saveMat (edges * 255.0) (buildFileName " - edges.png")
- saveMat (edges * 255.0) (buildFileName " - edges.png")
+ saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
+ saveImg parasites.stain (buildFileName " - parasites - stain.png")
+ saveImg parasites.infection (buildFileName " - parasites - infection.png")
- saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
- saveImg parasites.stain (buildFileName " - parasites - stain.png")
- saveImg parasites.infection (buildFileName " - parasites - infection.png")
+ let imgAllEllipses = img.Copy()
+ drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04
+ saveImg imgAllEllipses (buildFileName " - ellipses - all.png")
- let imgAllEllipses = img.Copy()
- drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04
- saveImg imgAllEllipses (buildFileName " - ellipses - all.png")
+ let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte>()
+ drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0
+ saveImg imgEllipses (buildFileName " - ellipses.png")
- let imgEllipses = filteredGreenWhitoutStain.Convert<Bgr, byte>()
- drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0
- saveImg imgEllipses (buildFileName " - ellipses.png")
+ let imgCells = img.Copy()
+ drawCells imgCells false cells
+ saveImg imgCells (buildFileName " - cells.png")
- let imgCells = img.Copy()
- drawCells imgCells false cells
- saveImg imgCells (buildFileName " - cells.png")
+ let imgCells' = img.Copy()
+ drawCells imgCells' true cells
+ saveImg imgCells' (buildFileName " - cells - full.png")
- 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")
- 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 filteredGreen (buildFileName " - filtered.png")
- saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png")
- //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png")
+ saveImg green (buildFileName " - green.png")
- saveImg green (buildFileName " - green.png")
+ use blue = img.Item(0)
+ saveImg blue (buildFileName " - blue.png")
- use blue = img.Item(0)
- saveImg blue (buildFileName " - blue.png")
+ use red = img.Item(2)
+ saveImg red (buildFileName " - red.png")
+ | _ -> ()
- use red = img.Item(2)
- saveImg red (buildFileName " - red.png")
- | _ -> ()
+ return cells }
- 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) =
+/// <summary>
+/// Do multiple analyses on the same time. The number of concurrent process depends if the number of the core.
+/// </summary>
+/// <param name="imgs"></param>
+/// <param name="reportProgress">An optional function to report progress. The process is aborted if the returned value is false</param>
+let doMultipleAnalysis (imgs: (string * Config * Image<Bgr, byte>) list) (reportProgress: (int -> bool) option) : (string * Cell list) list option =
+ let report (percent: int) : bool =
match reportProgress with
| Some f -> f percent
- | _ -> ()
+ | _ -> true
let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary<string, int>()
let nbImgs = List.length imgs
let n = Environment.ProcessorCount
- imgs
- |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p)))
- |> PSeq.withDegreeOfParallelism n
- |> PSeq.toList
+ let results =
+ 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)
+ |> PSeq.withDegreeOfParallelism n
+ |> PSeq.toList
+
+ // If one of the analyses has been aborted we return 'None'.
+ if List.length results <> List.length imgs
+ then None
+ else Some results
+