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 FSharp.Collections.ParallelSeq
17 open Emgu.CV.Structure
22 let display (window
: Views.MainWindow) (img
: IImage) =
23 let imgControl = window
.Root.FindName("img") :?> Controls.Image
24 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img
)
26 let log (window
: Views.MainWindow) (mess
: string) =
27 let txtLog = window
.Root.FindName("txtLog") :?> Controls.TextBlock
28 txtLog.Text <- txtLog.Text + mess
+ "\n"
36 | CmdLine of Input * string
39 type Arguments = RunningMode * bool
41 let parseArgs (args
: string[]) : Arguments =
43 let output = Array.tryFindIndex
((=) "--output") args
46 match Array.tryFindIndex
((=) "--folder") args
, output with
47 | Some i
, Some i_output when i
< args
.Length - 2 && i_output < args
.Length - 2 ->
48 CmdLine ((Dir args
.[i
+1]), args
.[i_output + 1])
50 match Array.tryFindIndex
((=) "--file") args
, output with
51 | Some i
, Some i_output when i
< args
.Length - 2 && i_output < args
.Length - 2 ->
52 CmdLine ((File args.[i
+1]), args.[i_output + 1])
56 runningMode, Array.exists
((=) "--debug") args
61 match parseArgs args with
67 initialAreaOpen
= 2000
72 preFilterSigma
= 1.7 // 1.5
76 darkStainLevel
= 0.22 // Lower -> more sensitive. 0.3. Careful about illumination on the borders.
77 maxDarkStainRatio
= 0.1 // 10 %
79 infectionArea
= 0.012f // 1.2 %
80 infectionLevel
= 1.12 // Lower -> more sensitive.
82 stainArea
= 0.08f // 8 %
83 stainLevel
= 1.1 // Lower -> more sensitive.
84 maxStainRatio
= 0.12 // 12 %
86 standardDeviationMaxRatio
= 0.5 // 0.55
87 minimumCellArea
= 0.5f })
90 | CmdLine (input
, output) ->
93 config.Debug <- DebugOn output
95 Directory.CreateDirectory output |> ignore
97 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
98 Utils.log <- (fun m
-> logFile.WriteLine(m
))
99 Utils.log (sprintf
"=== New run : %A %A ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]"))
101 let files = match input
with
102 | File file
-> [ file
]
103 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
105 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
108 let images = seq
{ for file
in files -> Path.GetFileNameWithoutExtension(FileInfo(file
).Name), new Image<Bgr, byte
>(file
) }
110 let nbConcurrentTaskLimit = 4
111 let n = Environment.ProcessorCount
113 Utils.logTime
"Whole analyze" (fun () ->
116 |> PSeq.map
(fun (id
, img
) -> id
, ImageAnalysis.doAnalysis
img id
(config.Copy()))
117 |> PSeq.withDegreeOfParallelism
(if n > nbConcurrentTaskLimit then nbConcurrentTaskLimit else n)
119 for id
, cells
in results do
120 let total, infected
= Utils.countCells
cells
121 fprintf
resultFile "File: %s %d %d %.2f\n" id
total infected (100. * (float infected) / (float total)))
123 //Utils.log (sprintf "== File: %A" file)
125 //| :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
129 let app = new Application()
130 let mainWindow = Views.MainWindow()
134 config.Debug <- DebugOn "."
136 Utils.log <- (fun m
-> log mainWindow m
)
138 //display mainWindow img
139 mainWindow.Root.Show()