X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FMainAnalysis.fs;h=53429b234dd8589ea3423d149e0ed17cb386deaa;hb=81d1b86719a1ebaf649c1de4c1364603155a53e1;hp=7f55c7ca324449f65f26b1616e124db941b88d0c;hpb=4147bfad3c85dcf4fe7d291abf7d356379d88b4f;p=master-thesis.git diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index 7f55c7c..53429b2 100644 --- a/Parasitemia/Parasitemia/MainAnalysis.fs +++ b/Parasitemia/Parasitemia/MainAnalysis.fs @@ -1,6 +1,7 @@ module ImageAnalysis open System +open System.Linq open System.Drawing open FSharp.Collections.ParallelSeq @@ -20,42 +21,60 @@ let doAnalysis (img: Image) (name: string) (config: Config) (reportPr | 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 + + logWithName "Starting analysis ..." + use green = img.Item(1) let greenFloat = green.Convert() let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation - let initialAreaOpening = int<| config.RBCArea * config.Parameters.ratioAreaPaleCenter - logTime "Area opening number one" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening) - report 8 + logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution) + + 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) + + report 10 let range = - let delta = config.Parameters.granulometryRange * config.RBCRadius - int <| config.RBCRadius - delta, int <| config.RBCRadius + delta - //let r1 = logTime "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert()) range 1.0 |> float32) - let r2 = logTime "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32) - // log (sprintf "r1: %A, r2: %A" r1 r2) - config.RBCRadius <- r2 - report 24 - - let secondAreaOpening = int <| config.RBCArea * config.Parameters.ratioAreaPaleCenter + 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()) range 1.0 |> float32) + config.SetRBCRadius <| logTimeWithName "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32) + + logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius) + + report 20 + + let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter if secondAreaOpening > initialAreaOpening then - logTime "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening) + logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening) let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config - let edges, xGradient, yGradient = logTime "Finding edges" (fun () -> ImgTools.findEdges filteredGreenWhitoutStain) - logTime "Removing small connected components from thinning" (fun () -> removeArea edges (config.RBCRadius ** 2.f / 50.f |> int)) + 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) + + let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find edges xGradient yGradient config) + + report 60 + + let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses) - let allEllipses, ellipses = logTime "Finding ellipses" (fun () -> - let matchingEllipses = Ellipse.find edges xGradient yGradient config - matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses) report 80 - let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config) + let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config) + report 100 + logWithName "Analysis finished" + // Output pictures if debug flag is set. match config.Debug with | DebugOn output -> @@ -71,11 +90,11 @@ let doAnalysis (img: Image) (name: string) (config: Config) (reportPr saveImg parasites.infection (buildFileName " - parasites - infection.png") let imgAllEllipses = img.Copy() - drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05 + drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04 saveImg imgAllEllipses (buildFileName " - ellipses - all.png") let imgEllipses = filteredGreenWhitoutStain.Convert() - drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0 + drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0 saveImg imgEllipses (buildFileName " - ellipses.png") let imgCells = img.Copy() @@ -106,28 +125,23 @@ let doAnalysis (img: Image) (name: string) (config: Config) (reportPr cells -// ID * cell radius * cell list. -let doMultipleAnalysis (imgs: (string * Image) list) (config : Config) (reportProgress: (int -> unit) option) : (string * float * Cell list) list = +// ID * cell list. +let doMultipleAnalysis (imgs: (string * Config * Image) list) (reportProgress: (int -> unit) option) : (string * Cell list) list = let inline report (percent: int) = match reportProgress with | Some f -> f percent | _ -> () - let monitor = Object() - let mutable total = 0 + let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary() let nbImgs = List.length imgs - let reportProgressImg = - (fun progress -> - lock monitor (fun () -> total <- total + progress) - report (total / nbImgs)) - let nbConcurrentTaskLimit = 4 + let reportProgressImg (id: string) (progress: int) = + progressPerAnalysis.AddOrUpdate(id, progress, (fun _ _ -> progress)) |> ignore + report (progressPerAnalysis.Values.Sum() / nbImgs) + let n = Environment.ProcessorCount imgs - |> PSeq.map (fun (id, img) -> - let localConfig = config.Copy() - let cells = doAnalysis img id localConfig (Some reportProgressImg) - id, float localConfig.RBCRadius, cells) - |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n) + |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p))) + |> PSeq.withDegreeOfParallelism n |> PSeq.toList