86de4354d7e75dd83208e12737a6d68e2eeb4e1f
1
module Parasitemia.Main
6 open System.Windows.Media
7 open System.Windows.Markup
8 open System.Windows.Shapes
9 open System.Windows.Controls
11 open System.Diagnostics
14 open Emgu.CV.Structure
19 let display (window
: Views.MainWindow) (img
: IImage) =
20 let imgControl = window
.Root.FindName("img") :?> Controls.Image
21 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img
)
23 let log (window
: Views.MainWindow) (mess
: string) =
24 let txtLog = window
.Root.FindName("txtLog") :?> Controls.TextBlock
25 txtLog.Text <- txtLog.Text + mess
+ "\n"
33 | CmdLine of Input * string
36 type Arguments = RunningMode * bool
38 let parseArgs (args
: string[]) : Arguments =
40 let output = Array.tryFindIndex
((=) "--output") args
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])
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])
53 runningMode, Array.exists
((=) "--debug") args
58 match parseArgs args with
62 debug
= if debug
then DebugOn "." else DebugOff
66 // RBC size range in px at scale = 1.0.
72 factorNbPick
= 1.0 // 1.0
73 factorWindowSize
= 2.0
75 darkStainLevel
= 0.4 // Lower -> more sensitive.
79 stainSpreadRequired
= 3.0
83 infectionPixelsRequired
= 1
85 percentageOfFgValidCell
= 0.4
87 MaxDarkStainRatio = 0.1
89 minimumCellArea
= 1000. * scale ** 2. |> int
93 | CmdLine (input
, output) ->
94 let config = if debug
then { config with debug
= DebugOn output } else config
96 Directory.CreateDirectory output |> ignore
98 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
99 Utils.log <- (fun m
-> logFile.WriteLine(m
))
100 Utils.log (sprintf
"=== New run : %A %A ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]"))
102 let files = match input
with
103 | File file
-> [ file
]
104 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
106 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
110 use img = new Image<Bgr, byte
>(file
)
111 Utils.log (sprintf
"== File: %A" file
)
113 let cells = Utils.logTime
"Whole analyze" (fun () -> ImageAnalysis.doAnalysis
img (Path.GetFileNameWithoutExtension(FileInfo(file
).Name)) config)
114 let total, infected
= Utils.countCells
cells
115 fprintf
resultFile "File: %s %d %d %.2f\n" file
total infected
(100. * (float infected) / (float total))
117 | :? IOException as ex -> Utils.log (sprintf
"Unable to open the image '%A': %A" file
ex)
121 let app = new Application()
122 let mainWindow = Views.MainWindow()
124 Utils.log <- (fun m
-> log mainWindow m
)
126 //display mainWindow img
127 mainWindow.Root.Show()