open System
open System.IO
-open System.Windows
-open System.Windows.Media
-open System.Windows.Markup
-open System.Windows.Shapes
-open System.Windows.Controls
-open System.Drawing
-open System.Diagnostics
+open System.Threading
open Emgu.CV
open Emgu.CV.Structure
-open Emgu.CV.WPF
open Config
-let display (window : Views.MainWindow) (img : IImage) =
- let imgControl = window.Root.FindName("img") :?> Controls.Image
- imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img)
-
-let log (window : Views.MainWindow) (mess : string) =
- let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock
- txtLog.Text <- txtLog.Text + mess + "\n"
-
-
type Input =
| File of string
| Dir of string
type RunningMode =
- | CmdLine of Input * string
- | Window
+ | CmdLine of Input * string // A file or a directory to process and the output directory.
+ | Window of string option // An optional path to a file to open can be given in window mode.
type Arguments = RunningMode * bool
| Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
CmdLine ((File args.[i+1]), args.[i_output + 1])
|_ ->
- Window
+ Window (if args.Length > 0 && not (args.[0].StartsWith("--")) then Some args.[0] else None)
runningMode, Array.exists ((=) "--debug") args
-
[<EntryPoint>]
+[<STAThread()>]
let main args =
- match parseArgs args with
- | mode, debug ->
- let scale = 1.
- let config = {
- debug = if debug then DebugOn "." else DebugOff
-
- scale = scale
-
- // RBC size range in px at scale = 1.0.
- minRBCSize = 20.
- maxRBCSize = 42.
-
- doGSigma1 = 1.5
- doGSigma2 = 30.
- doGLowFreqPercentageReduction = 0.75
-
- factorNbPick = 1.0
- factorWindowSize = 2.0
- darkStainLevel = 0.4 // Lower -> more sensitive.
+ let e = Ellipse.ellipse2 -11.4 -7.8 -0.169811 -23.75 0.8 -3.885714 -19. 1.5
- stainSigma = 10.
- stainLevel = 0.9
- stainSpreadRequired = 3.0
-
- infectionSigma = 2.2
- infectionLevel = 0.85
- infectionPixelsRequired = 1
-
- percentageOfFgValidCell = 0.4
-
- MaxDarkStainRatio = 0.1
-
- minimumCellArea = 1000. * scale ** 2. |> int
- maxOffcenter = 0.5 }
+ match parseArgs args with
+ | mode, debug ->
+ let config = Config(defaultParameters)
match mode with
| CmdLine (input, output) ->
- let config = if debug then { config with debug = DebugOn output } else config
+ if debug
+ then
+ config.Debug <- DebugOn output
Directory.CreateDirectory output |> ignore
use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
- for file in files do
- try
- use img = new Image<Bgr, byte>(file)
- Utils.log (sprintf "== File: %A" file)
+ //try
+ let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), config.Copy(), new Image<Bgr, byte>(file) ]
+
- let cells = Utils.logTime "Whole analyze" (fun () -> ImageAnalysis.doAnalysis img (Path.GetFileNameWithoutExtension(FileInfo(file).Name)) config)
+ Utils.logTime "Whole analyze" (fun () ->
+ let results = ImageAnalysis.doMultipleAnalysis images None
+
+ for id, cells in results do
+ let config = images |> List.pick (fun (id', config', _) -> if id' = id then Some config' else None)
let total, infected = Utils.countCells cells
- fprintf resultFile "File: %s %d %d %.2f\n" file total infected (100. * (float infected) / (float total))
- with
- | :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
+ fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius)
+
+ //Utils.log (sprintf "== File: %A" file)
+ //with
+ //| :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
0
- | Window ->
- let app = new Application()
- let mainWindow = Views.MainWindow()
+ | Window fileToOpen ->
+ (*let display (window : Views.MainWindow) (img : IImage) =
+ let imgControl = window.Root.FindName("img") :?> Controls.Image
+ imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img)
- Utils.log <- (fun m -> log mainWindow m)
+ let log (window : Views.MainWindow) (mess : string) =
+ let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock
+ txtLog.Text <- txtLog.Text + mess + "\n"*)
- //display mainWindow img
- mainWindow.Root.Show()
- app.Run()
+ if debug then config.Debug <- DebugOn "."
+ GUI.Main.run config fileToOpen