module ImageAnalysis open System open System.Drawing open FSharp.Collections.ParallelSeq open Emgu.CV open Emgu.CV.Structure open Utils open ImgTools open Config open Types 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) logTime "areaOpen 1" (fun () -> ImgTools.areaOpenF filteredGreen config.Parameters.initialAreaOpen) 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) // log (sprintf "r1: %A, r2: %A" r1 r2) config.RBCRadius <- r1 let secondAreaOpen = int <| config.RBCArea / 3.f if secondAreaOpen > config.Parameters.initialAreaOpen then logTime "areaOpen 2" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpen) 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 allEllipses, ellipses = logTime "Finding ellipses" (fun () -> let matchingEllipses = Ellipse.find edges xGradient yGradient config matchingEllipses.Ellipses, matchingEllipses.PrunedEllipses) let cells = logTime "Classifier" (fun () -> Classifier.findCells ellipses parasites filteredGreenWhitoutStain config) // 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) 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") let imgAllEllipses = img.Copy() drawEllipses imgAllEllipses allEllipses (Bgr(0.0, 240.0, 240.0)) 0.05 saveImg imgAllEllipses (buildFileName " - ellipses - all.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 saveImg imgCells (buildFileName " - cells.png") let imgCells' = img.Copy() drawCells imgCells' true cells saveImg imgCells' (buildFileName " - cells - full.png") let filteredGreenMaxima = gaussianFilter greenFloat config.Parameters.preFilterSigma 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 let doMultipleAnalysis (imgs: (string * Image) list) (config : Config) : (string * Cell list) list = let nbConcurrentTaskLimit = 4 let n = Environment.ProcessorCount imgs |> PSeq.map (fun (id, img) -> id, doAnalysis img id (config.Copy())) |> PSeq.withDegreeOfParallelism (if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n) |> PSeq.toList