X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FMainAnalysis.fs;h=7f55c7ca324449f65f26b1616e124db941b88d0c;hb=4147bfad3c85dcf4fe7d291abf7d356379d88b4f;hp=043eb739cd6dcaba1805a62f7e43f885f4373685;hpb=3ddaf64dc5ba6a7066a279ad75b9a1ee72194639;p=master-thesis.git diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index 043eb73..7f55c7c 100644 --- a/Parasitemia/Parasitemia/MainAnalysis.fs +++ b/Parasitemia/Parasitemia/MainAnalysis.fs @@ -3,6 +3,8 @@ open System open System.Drawing +open FSharp.Collections.ParallelSeq + open Emgu.CV open Emgu.CV.Structure @@ -11,24 +13,34 @@ open ImgTools open Config open Types +let doAnalysis (img: Image) (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 doAnalysis (img: Image) (name: string) (config: Config) : Cell list = use green = img.Item(1) let greenFloat = green.Convert() - let filteredGreen = gaussianFilter greenFloat (float config.Parameters.preFilterSigma) + let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation - logTime "areaOpen 1" (fun () -> ImgTools.areaOpenF filteredGreen config.Parameters.initialAreaOpen) + let initialAreaOpening = int<| config.RBCArea * config.Parameters.ratioAreaPaleCenter + logTime "Area opening number one" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening) + report 8 - let r1 = logTime "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert()) (10, 80) 0.5 |> float32) - // let r2 = logTime "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen (10, 80) |> float32) + 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 <- r1 - - let secondAreaOpen = int <| config.RBCArea / 3.f + config.RBCRadius <- r2 + report 24 - if secondAreaOpen > config.Parameters.initialAreaOpen + let secondAreaOpening = int <| config.RBCArea * config.Parameters.ratioAreaPaleCenter + if secondAreaOpening > initialAreaOpening then - logTime "areaOpen 2" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpen) + logTime "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 @@ -39,8 +51,10 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li 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) + report 100 // Output pictures if debug flag is set. match config.Debug with @@ -72,7 +86,7 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li drawCells imgCells' true cells saveImg imgCells' (buildFileName " - cells - full.png") - let filteredGreenMaxima = gaussianFilter greenFloat config.Parameters.preFilterSigma + 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") @@ -91,3 +105,29 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li | _ -> () cells + +// ID * cell radius * cell list. +let doMultipleAnalysis (imgs: (string * Image) list) (config : Config) (reportProgress: (int -> unit) option) : (string * float * Cell list) list = + let inline report (percent: int) = + match reportProgress with + | Some f -> f percent + | _ -> () + + let monitor = Object() + let mutable total = 0 + let nbImgs = List.length imgs + let reportProgressImg = + (fun progress -> + lock monitor (fun () -> total <- total + progress) + report (total / nbImgs)) + + let nbConcurrentTaskLimit = 4 + 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.toList