X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FMainAnalysis.fs;h=8f595141c638a57ca6dd727a18bb9b1d2c282fe7;hp=c9e94224c67e957a14d3c3503732990188ba9d02;hb=05be8164d308447b916544ae3ce4211500dfd8da;hpb=5b68d946369f998865d2dd330fd3b374b2b9a0ad diff --git a/Parasitemia/Parasitemia/MainAnalysis.fs b/Parasitemia/Parasitemia/MainAnalysis.fs index c9e9422..8f59514 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,84 +14,87 @@ 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 -let doAnalysis (img: Image) (name: string) (config: Config) : Cell list = + logWithName "Starting analysis ..." - let imgFloat = img.Convert() - use scaledImg = if config.scale = 1.0 then imgFloat else imgFloat.Resize(config.scale, CvEnum.Inter.Area) + use green = img.Item(1) + let greenFloat = green.Convert() + let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation - use green = scaledImg.Item(1) + logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution) - use filteredGreen = (gaussianFilter green config.doGSigma1) - config.doGLowFreqPercentageReduction * (gaussianFilter green config.doGSigma2) + 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 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)) + report 8 - use xEdges = filteredGreen.Convolution(sobelKernel).Convert() - use yEdges = filteredGreen.Convolution(sobelKernel.Transpose()).Convert() + 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) + // log (sprintf "r1: %A, r2: %A" r1 r2) - 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 + logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius) - 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 + report 24 - use magnitudes = new Matrix(xEdges.Size) - CvInvoke.CartToPolar(xEdges, yEdges, magnitudes, new Mat()) // Compute the magnitudes (without angles). + let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter + if secondAreaOpening > initialAreaOpening + then + logTimeWithName "Area opening number two" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening) - 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 parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config + //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config - 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 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) - logTime "Finding edges" (fun() -> thin edges) - logTime "Removing small connected components from thinning" (fun () -> removeArea edges 12) + let allEllipses, ellipses = logTimeWithName "Finding ellipses" (fun () -> + let matchingEllipses = Ellipse.find edges xGradient yGradient config + matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses) - (* - let kmediansResults = KMedians.kmedians filteredGreen 1.0 + report 80 - let parasites = ParasitesMarker.find green filteredGreen kmediansResults config + let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config) - let radiusRange = config.scale * config.minRBCSize, config.scale * config.maxRBCSize - let windowSize = roundInt (1.6 * (snd radiusRange)) - let factorNbPick = 1.5 - let ellipses = logTime "Finding ellipses" (fun () -> - Ellipse.find edges xEdges yEdges radiusRange windowSize factorNbPick) + report 100 - let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites kmediansResults.fg config) - *) - let cells = [] + 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") + saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png") saveImg parasites.stain (buildFileName " - parasites - stain.png") saveImg parasites.infection (buildFileName " - parasites - infection.png") - let imgEllipses = img.Copy() - drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) - saveImg imgEllipses (buildFileName " - ellipses.png") + let imgAllEllipses = img.Copy() + drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05 + saveImg imgAllEllipses (buildFileName " - ellipses - all.png") - saveImg (kmediansResults.fg * 255.0) (buildFileName " - foreground.png") + let imgEllipses = filteredGreenWhitoutStain.Convert() + drawEllipses imgEllipses ellipses (Bgr(0.0, 240.0, 240.0)) 1.0 + saveImg imgEllipses (buildFileName " - ellipses.png") let imgCells = img.Copy() drawCells imgCells false cells @@ -96,7 +102,46 @@ let doAnalysis (img: Image) (name: string) (config: Config) : Cell li let imgCells' = img.Copy() drawCells imgCells' true cells - saveImg imgCells' (buildFileName " - cells - full.png")*) + 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 nbConcurrentTaskLimit = 4 // To reduce the memory taken. + let n = Environment.ProcessorCount + + imgs + |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p))) + |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n) + |> PSeq.toList