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