* Remove ellipses with a too high standard variation.
[master-thesis.git] / Parasitemia / Parasitemia / Program.fs
1 module Parasitemia.Main
2
3 open System
4 open System.IO
5 open System.Windows
6 open System.Windows.Media
7 open System.Windows.Markup
8 open System.Windows.Shapes
9 open System.Windows.Controls
10 open System.Drawing
11 open System.Diagnostics
12
13 open Emgu.CV
14 open Emgu.CV.Structure
15 open Emgu.CV.WPF
16
17 open Config
18
19 let display (window : Views.MainWindow) (img : IImage) =
20 let imgControl = window.Root.FindName("img") :?> Controls.Image
21 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img)
22
23 let log (window : Views.MainWindow) (mess : string) =
24 let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock
25 txtLog.Text <- txtLog.Text + mess + "\n"
26
27
28 type Input =
29 | File of string
30 | Dir of string
31
32 type RunningMode =
33 | CmdLine of Input * string
34 | Window
35
36 type Arguments = RunningMode * bool
37
38 let parseArgs (args: string[]) : Arguments =
39
40 let output = Array.tryFindIndex ((=) "--output") args
41
42 let runningMode =
43 match Array.tryFindIndex ((=) "--folder") args, output with
44 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
45 CmdLine ((Dir args.[i+1]), args.[i_output + 1])
46 | _ ->
47 match Array.tryFindIndex ((=) "--file") args, output with
48 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
49 CmdLine ((File args.[i+1]), args.[i_output + 1])
50 |_ ->
51 Window
52
53 runningMode, Array.exists ((=) "--debug") args
54
55
56 [<EntryPoint>]
57 let main args =
58 match parseArgs args with
59 | mode, debug ->
60 let config =
61 Config(
62 { scale = 1.
63
64 minRbcRadius = -0.35
65 maxRbcRadius = 0.35
66
67 preFilterSigma = 1.5
68
69 factorNbPick = 1.0
70 factorWindowSize = 2.0
71
72 darkStainLevel = 0.4 // Lower -> more sensitive.
73
74 infectionArea = 0.015
75 infectionLevel = 0.2
76 parasitePixelsRequired = 1
77
78 stainArea = 0.06
79 stainLevel = 0.2
80
81 maxDarkStainRatio = 0.1
82
83 standardDeviationMaxRatio = 0.65
84 minimumCellArea = 0.5 })
85
86 match mode with
87 | CmdLine (input, output) ->
88 if debug
89 then
90 config.Debug <- DebugOn output
91
92 Directory.CreateDirectory output |> ignore
93
94 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
95 Utils.log <- (fun m -> logFile.WriteLine(m))
96 Utils.log (sprintf "=== New run : %A %A ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]"))
97
98 let files = match input with
99 | File file -> [ file ]
100 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
101
102 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
103
104 for file in files do
105 try
106 use img = new Image<Bgr, byte>(file)
107 Utils.log (sprintf "== File: %A" file)
108
109 let cells = Utils.logTime "Whole analyze" (fun () -> ImageAnalysis.doAnalysis img (Path.GetFileNameWithoutExtension(FileInfo(file).Name)) config)
110 let total, infected = Utils.countCells cells
111 fprintf resultFile "File: %s %d %d %.2f\n" file total infected (100. * (float infected) / (float total))
112 with
113 | :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
114 0
115
116 | Window ->
117 let app = new Application()
118 let mainWindow = Views.MainWindow()
119
120 if debug
121 then
122 config.Debug <- DebugOn "."
123
124 Utils.log <- (fun m -> log mainWindow m)
125
126 //display mainWindow img
127 mainWindow.Root.Show()
128 app.Run()