X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FMainAnalysis.fs;h=53429b234dd8589ea3423d149e0ed17cb386deaa;hb=81d1b86719a1ebaf649c1de4c1364603155a53e1;hp=3f35fb7ce4cbf03748306078f579d82c619f2437;hpb=8cf3b0a302943312c588690b4e4c90af17b3e87a;p=master-thesis.git diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index 3f35fb7..53429b2 100644 --- a/Parasitemia/Parasitemia/MainAnalysis.fs +++ b/Parasitemia/Parasitemia/MainAnalysis.fs @@ -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,75 @@ 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 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 ..." -let doAnalysis (img: Image) (name: string) (config: Config) : Cell list = + use green = img.Item(1) + let greenFloat = green.Convert() + let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation - let imgFloat = img.Convert() - use scaledImg = if config.scale = 1.0 then imgFloat else imgFloat.Resize(config.scale, CvEnum.Inter.Area) + logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution) - use green = scaledImg.Item(1) + 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 filteredGreen = (gaussianFilter green config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter green config.doGSigma2) + report 10 - 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)) + 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()) range 1.0 |> float32) + config.SetRBCRadius <| logTimeWithName "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32) - use xEdges = filteredGreen.Convolution(sobelKernel).Convert() - use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert() + logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius) - 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 + report 20 - 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 + let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter + if secondAreaOpening > initialAreaOpening + then + logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening) - use magnitudes = new Matrix(xEdges.Size) - CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles). + let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config + //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config - 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 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) - use magnitudesByte = ((magnitudes / !max) * 255.0).Convert() // Otsu from OpenCV only support 'byte'. - use edges = new Matrix(xEdges.Size) - CvInvoke.Threshold(magnitudesByte, edges, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) |> ignore + let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find edges xGradient yGradient config) - logTime "Finding edges" (fun() -> thin edges) - logTime "Removing small connected components from thinning" (fun () -> removeArea edges 12) + report 60 - let kmediansResults = logTime "Finding foreground (k-medians)" (fun () -> KMedians.kmedians filteredGreen 1.0) + let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses) - let parasites = ParasitesMarker.find green filteredGreen kmediansResults config + report 80 - let allEllipses, ellipses = logTime "Finding ellipses" (fun () -> - let matchingEllipses = Ellipse.find edges xEdges yEdges config - matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses ) + let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config) - let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites kmediansResults.fg config) + report 100 + + 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 +90,13 @@ let doAnalysis (img: Image) (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 matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04 saveImg imgAllEllipses (buildFileName " - ellipses - all.png") - let imgEllipses = img.Copy() - drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0 + let imgEllipses = filteredGreenWhitoutStain.Convert() + drawEllipses imgEllipses prunedEllipses (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 +104,44 @@ let doAnalysis (img: Image) (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) 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() + let nbImgs = List.length imgs + + 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, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p))) + |> PSeq.withDegreeOfParallelism n + |> PSeq.toList